Function on separate intervals

23 ビュー (過去 30 日間)
Nader Mohamed
Nader Mohamed 2021 年 10 月 30 日
編集済み: David Hill 2021 年 10 月 30 日
I'm trying to write a function that has different values on different intervals:
from t0 to t1 needs to be 0
from t1to t2 needs to be a segment that goes linearly from 0 to t1
from t2 to tf needs to be t2
But when I try the code below I get a totally different plot and can't find the error(s).
t0 = 0;
t1 = 1;
t2 = 1.5;
tf = 3;
function z = funz(t,t0,t1,t2,tf)
zz = @(t) ((t2-t1)/(t2-t1))*t;
if (t<t1 & t>t0)
z = zz(0);
elseif (t<t2 & t>t1)
z = zz(t);
else
z = t1;
end
end

採用された回答

David Hill
David Hill 2021 年 10 月 30 日
編集済み: David Hill 2021 年 10 月 30 日
t=0:.01:3;%the start and stop of t defines t0 and tf
t1 = 1;
t2 = 1.5;
z=funz(t,t1,t2);
plot(t,z);
function z = funz(t,t1,t2)
z=zeros(size(t));
z(t>t1&t<t2)=(t(t>t1&t<t2)-t1)*t1/(t2-t1);
z(t>=t2)=t1;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by