2017-08-01 156 views
1

我有一個差分解決方案的解決方案,但問題是我有不同的時間間隔不同的解決方案。繪製分段函數

對於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] 

現在我需要繪製這些圖,讓他們看起來像他們的第一張圖後有一個單一的功能,即只需立即x_1(t)直到t_1,我需要其他圖形x_2(t)等上。

在Matlab中可能嗎?

+0

你有沒有試圖只繪製所有三個? – Yotam

回答

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

+0

或只是'plot([t1,t2,t3],[x1,x2,x3])'如果不需要不同的顏色/樣式 –