How to find the maximum value of the output values from a FOR loop
1 view (last 30 days)
Show older comments
Bhavika Koliyabandara
on 26 Oct 2014
Commented: Bhavika Koliyabandara
on 26 Oct 2014
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 Comments
Accepted Answer
Roger Stafford
on 26 Oct 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 Comments
Roger Stafford
on 26 Oct 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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!