绘制分段函数

时间:2017-08-01 10:57:22

标签: matlab plot matlab-figure

我有差异解决方案的解决方案,但问题是我在不同的时间间隔内有不同的解决方案。

例如:

x_1(t) when t belongs to [0,t_1]  
x_2(t) when t belongs to [t_1,t_2]  
x_3(t) when t belongs to [t_2,t_3]

现在我需要绘制这些图表,使它们看起来像是一个单一的函数,即在第一个图形x_1(t)之后直到t_1,我需要另一个图形x_2(t)和等等。

Matlab有可能吗?

1 个答案:

答案 0 :(得分:2)

您可以将plot与多个输入一起使用来完全绘制它们:

% 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))

enter image description here

另一种允许您定义所有类型的功能特定视觉属性的方法是使用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

enter image description here