For loop only outputting the final value.

13 ビュー (過去 30 日間)
Eris Rusnac
Eris Rusnac 2021 年 3 月 21 日
回答済み: Walter Roberson 2021 年 3 月 21 日
function y = q2(t)
if 0 > t;
y = t*sin(2*t)+sin(40+t);
elseif 0 <= t & t <= 100;
y = t*(t-1.6*sin(25*t));
elseif 100 < t;
y = 2*cos(t)+sin(2*t)*cos(40*t)+t;
end
end
t=linspace(0,3*pi,100)
for t=linspace(0,3*pi,100);
y = q2(t);
valueOfA(t) = y;
end

回答 (3 件)

Cris LaPierre
Cris LaPierre 2021 年 3 月 21 日
This is because you have not formatted your for loop counter correcty, and because your code replaces the value of y in each loop.
See the examples in the documentation page for capturing all the values. You might also find Ch 13 of MATLAB Onramp helpful.

Rafael Hernandez-Walls
Rafael Hernandez-Walls 2021 年 3 月 21 日
Try using something like this:
t=linspace(0,3*pi,100)
t=linspace(0,3*pi,100);
for k=1:length(t)
y = q2(t(k));
valueOfA(k) = y;
end
valueOfA

Walter Roberson
Walter Roberson 2021 年 3 月 21 日

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by