フィルターのクリア

Error plotting graph, with y behaving similar to step function

4 ビュー (過去 30 日間)
Samious
Samious 2014 年 5 月 26 日
コメント済み: Samious 2014 年 5 月 26 日
Hi, i want to plot a graph where y=t^2 when t>0 and y=5 when t<0 giving that t=[-5:0.1:5]
t=[-5:0.01:5]
for i=length(t)
if t<0
y(i)=5;
else if t>0
y(i)=t^2;
end
end
end
this doesnt seem to work, it gave me an error that y is undefined. What did I do wrong

採用された回答

Mischa Kim
Mischa Kim 2014 年 5 月 26 日
編集済み: Mischa Kim 2014 年 5 月 26 日
You could use something like
t = [-5:0.01:5];
y = (t.^2).*(t>0) + 5*(t<=0);
yy = 5*ones(numel(t),1);
yy(t>0) = t(t>0).^2;
plot(t,y,t,yy)
showing two different approaches. As for your code, try
t = [-5:0.01:5];
for i=1:length(t) % compute y for all t
if t(i)<0 % need to compare each component of t
y(i) = 5;
else
y(i) = t(i)^2; % assign y component-wise
end
end
plot(t,y)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by