Filter löschen
Filter löschen

while or for loop

2 Ansichten (letzte 30 Tage)
Daryl Chua
Daryl Chua am 27 Okt. 2019
Beantwortet: Dinesh Yadav am 30 Okt. 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 Kommentar
Ni2
Ni2 am 27 Okt. 2019
What are L1, L2 and L3?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Dinesh Yadav
Dinesh Yadav am 30 Okt. 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.

Kategorien

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

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by