MATLAB - Need help storing values of previous iterations in a while loop
3 次查看(过去 30 天)
显示 更早的评论
Ok. So I have a while loop that iterates for values of xcount and time. In the end, I need to plot all the xcount and time values. This should give me a simple harmonic plot. unfortunately, my while loop only stores the 'final' value for xcount and time. thus the plot produced is also wrong. Could someone please tell me where I have gone wrong. And how to fix it. Thanks
function [ X,t ] = spring( ks,rs,startx,d,m,c,tinc,atol,vtol )
X=startx;
t=0;
V=0;
a=0;
time = 0;
xcount=0;
F1=-ks(1)*(X-rs(1));
F2=ks(2)*(d-X-rs(2));
DampingF = c*V;
NetForce= F1 + F2 - DampingF;
a = (NetForce/m);
V = V + a*tinc;
t = t + tinc;
X = X + V*tinc;
xcount = [xcount X];
time = [time t];
while (abs(V) >= vtol) (abs(a) >= atol)
F1=-ks(1)*(X-rs(1));
F2=ks(2)*(d-X-rs(2));
DampingF = c*V;
NetForce= F1 + F2 - DampingF;
a =(NetForce/m);
V = V+(a*tinc);
X = X + V*tinc;
xcount = [0 X X];
t = t + tinc;
time = [0 tinc 2*tinc];
end
plot(xcount,time);
xlabel('x');
ylabel('Time');
end
0 个评论
采纳的回答
David Sanchez
2014-5-16
I think your while loop should go like this:
while (abs(V) >= vtol) && (abs(a) >= atol)
F1=-ks(1)*(X-rs(1));
F2=ks(2)*(d-X-rs(2));
DampingF = c*V;
NetForce= F1 + F2 - DampingF;
a =(NetForce/m);
V = V+(a*tinc);
X = X + V*tinc;
xcount = [xcount X];
t = t + tinc;
time = [time t];
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!