How do I plot a function which depends on a changing variable?

2 ビュー (過去 30 日間)
Rowan Miller
Rowan Miller 2017 年 12 月 4 日
回答済み: Jim Hokanson 2017 年 12 月 5 日
So I have to plot the movement of a spring once let go for -2<t<20 given yo=5(distance the spring is stretched initially), w=5(angular frequency), k=0.1/s. It has to be done using a for loop and if statements.
I am new to matlab so I don't know what to do from here. Thank you.
Here's the code I have:
t = -2;
yo = 5;
w = 5;
k = 0.1;
for t = -2:20
if t < 0
y = -yo;
elseif t < 10
y = -yo*(cos(w*t));
else
y = -yo*(cos(w*t))*exp(-k*(t-10));
end
t = t + 0.1;
end

採用された回答

Jim Hokanson
Jim Hokanson 2017 年 12 月 5 日
You could use something like animatedline if you want to calculate a single point and add it to a graph. If you want to use a loop the more typical approach is to initialize t and y, then loop over t and update y.
t_all = -2:0.1:20;
y = zeros(1,length(t_all));
for i = 1:length(t_all)
t = t_all(i);
y(i) = ...
end
plot(t_all,y)

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by