while or for loop

1 view (last 30 days)
Daryl Chua
Daryl Chua on 27 Oct 2019
Answered: Dinesh Yadav on 30 Oct 2019
Please help me with this loop where i to get the highest value of mv1 and then stop the loop. After stopping the loop it must display the highest value and where (x) it got the value from.
x = 0;
mv=0;
mv1=0.00000000000001;
x1=x;
x2=x-L1;
x3=x-(L1+L2);
x4=x-(L1+L2+L3);
x1(x1 < 0) = 0;
x2(x2 < 0) = 0;
x3(x3 < 0) = 0;
x4(x4 < 0) = 0;
while mv<mv1
mv=mv1;
mv1=(((Ra/6)*((x1).^3))+((P1/6)*((x2).^3))+((M/2)*((x3).^2))+((P2/6)*((x4).^3))-((w/24)*((x1).^4))+((w/24)*((x3).^4))+(c1*x))/(E*I/1000.^2);
x=x+0.01;
end
fprintf('The highest deflection of the beam is %.4f mm at x=%.2f\n',mv1,x);
  1 Comment
Ni2
Ni2 on 27 Oct 2019
What are L1, L2 and L3?

Sign in to comment.

Answers (1)

Dinesh Yadav
Dinesh Yadav on 30 Oct 2019
Assuming you know length of the sequence using for loop would be better as it would iterate over the entire sequence to find the maximum among the entire sequence. In this case here what if the point where the condition fails i.e. for some n-th iteration mv1<mv but after lets say k steps(i.e n+k overall steps) you have the value of mv1 thats a global maximum, the above while condition will fail to detect that. Therefore if you know the length of your sequence or the range over which you would like to find the maximum value I would recommend using for loop.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!