Effacer les filtres
Effacer les filtres

while or for loop

2 vues (au cours des 30 derniers jours)
Daryl Chua
Daryl Chua le 27 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 commentaire
Ni2
Ni2 le 27 Oct 2019
What are L1, L2 and L3?

Connectez-vous pour commenter.

Réponses (1)

Dinesh Yadav
Dinesh Yadav le 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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by