简体   繁体   中英

Plotting piecewise function

I have a solution to a differential solution but the issue is that I have different solutions in different intervals.

For examle:

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]

Now I need to plot these graphs so that they look like they have a single function ie just immediately after the first graph x_1(t) until t_1 , I need the other graph x_2(t) and so on.

Is it possible in Matlab?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM