Plotting piecewise function

被刻印的时光 ゝ 提交于 2019-12-02 04:46:53

You can use plot with multiple inputs to plot them altogether:

% the functions:
x_1 = @(t) 2.*t;
x_2 = @(t) 5.*t;
x_3 = @(t) 7.*t;
% the transition points:
t_1 = 30;
t_2 = 60;
t_3 = 90;
% plotting:
plot(0:t_1,x_1(0:t_1),t_1:t_2,x_2(t_1:t_2),t_2:t_3,x_3(t_2:t_3))

Another way, that lets you define all kind of function specific visual properties is to use hold:

f = @(t,a) a.*t;
t = 0:30:100;
m = 'os^'; % choosing different marker for each function
for k = 1:numel(t)-1
    plot(t(k):t(k+1),f(t(k):t(k+1),k),m(k))
    hold on
end
hold off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!