绘制分段函数 [英] Plotting piecewise function
本文介绍了绘制分段函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个差分解决方案,但问题是我在不同的时间间隔有不同的解决方案.
I have a solution to a differential solution but the issue is that I have different solutions in different intervals.
例如:
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)
,依此类推.
Now I need to plot these graphs so that they look like they have a single function i.e just immediately after the first graph x_1(t)
until t_1
, I need the other graph x_2(t)
and so on.
在Matlab中可以吗?
Is it possible in Matlab?
推荐答案
您可以使用 plot
,具有多个输入以将它们完全绘制在一起:
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))
让您定义各种特定于功能的视觉属性的另一种方法是使用 hold
:
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
这篇关于绘制分段函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文