绘制分段函数
Plotting piecewise function
我有一个微分解的解,但问题是我在不同的时间间隔有不同的解。
例如:
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 中可以吗?
您可以使用 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))
另一种让您定义各种函数特定视觉属性的方法是使用 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
我有一个微分解的解,但问题是我在不同的时间间隔有不同的解。
例如:
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 中可以吗?
您可以使用 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))
另一种让您定义各种函数特定视觉属性的方法是使用 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