while or for loop
조회 수: 1 (최근 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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!