Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

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

Asked by mohamed

mohamed (view profile)

on 26 Feb 2013
>> n=2;
>> 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



mohamed (view profile)



No products are associated with this question.

1 Answer

Answer by Walter Roberson

Walter Roberson (view profile)

on 26 Feb 2013
Accepted answer

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



mohamed (view profile)

on 27 Feb 2013

yes , i run the code in my mind and i want it to continue until the condition is satisfied (difference<=0.001)


bym (view profile)

on 28 Feb 2013

so the loop should continue while the difference is greater than .001?

Image Analyst

Image Analyst (view profile)

on 28 Feb 2013

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.

Walter Roberson

Walter Roberson (view profile)

Contact us