How to find the maximum value of the output values from a FOR loop
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to find the maximum value from 100 output values which are generated from a FOR loop. Essentially what my loop does is from time 't' 99 to 100 at 0.01 intervals I take readings of displacement of a sine wave. Then I convert them all to positive values and then try to find the maximum of those values, which is basically the amplitude of the wave, which is my aim here. But MaxA gives me an answer which is way too higher than the amplitude. My code is shown below. Thank you for your time.
for t=99:0.01:100
sim('massspringdamper')
ys=interp1(t,x,t);
ys=abs(ys);
[MaxA]=max(ys);
end
0 个评论
采纳的回答
Roger Stafford
2014-10-26
You can do it this way:
maxA = -inf;
for t=99:0.01:100
sim('massspringdamper')
ys=interp1(t,x,t);
ys=abs(ys);
maxA = max(maxA,ys);
end
3 个评论
Roger Stafford
2014-10-26
You're only supposed to pay attention to maxA after leaving the for-loop. At that time it should have the maximum of all the ys values. You can't expect to learn what the maximum is until you have gone through all the ys values. At each step in the for-loop maxA will increase only if the current ys is greater than all the previous ones.
更多回答(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!