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

采纳的回答

Roger Stafford
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by