while or for loop

1 次查看(过去 30 天)
Daryl Chua
Daryl Chua 2019-10-27
回答: Dinesh Yadav 2019-10-30
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 个)

Dinesh Yadav
Dinesh Yadav 2019-10-30
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by