How to find the maximum value of the output values from a FOR loop

7 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 26 Okt. 2014
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 Kommentare
Roger Stafford
Roger Stafford am 26 Okt. 2014
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by