the value of y in the while loop doesn't vary from the previous ?

>> n=2;
>>t=520
>> r=5;
>> p=1000;
>> x=n*r*t/p;
a=1; , b=2;
>> y=n*r*t/(p+a*n^2/x^2))+n*b;
while abs(x-y)<= 0.001
x=y;
y=n*r*t/((p+a*n^2/x^2))+n*b;
end

 Accepted Answer

I suspect you will find that your while loop body is not executing at all.

7 Comments

so, are there any suggestions ?
Suppose x = 3 and y = 5, then do you want the loop to end or to continue? Check that your code does the right thing in that situation.
i want to get the value of y by iterations and i supposed x=3 and y=5 and the loop again doesn't work
Imagine for a moment that your x and y variables were different by (say) 148. Then in such a situation, would you want the loop to continue or to end? Now examine your code in your mind and decide what your code would do if x and y differed by 148. Do not just run the code, read it and work through what it does step by step.
yes , i run the code in my mind and i want it to continue until the condition is satisfied (difference<=0.001)
so the loop should continue while the difference is greater than .001?
It's not even satisfied the very first time! Like Walter tried to tell you, the first time it hits that line, x = 5.2, and y = 9.1992, so abs(x-y) = 3.992 and since this is not less than 0.001, your loop never even gets entered the very first time. If you learn how to use the debugger, or simply leave off semicolons, you will discover these kinds of things very very easily and quickly.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!