the value of y in the while loop doesn't vary from the previous ?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
>> 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
Walter Roberson
on 26 Feb 2013
0 votes
I suspect you will find that your while loop body is not executing at all.
7 Comments
mohamed
on 26 Feb 2013
so, are there any suggestions ?
Walter Roberson
on 26 Feb 2013
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.
mohamed
on 26 Feb 2013
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
Walter Roberson
on 26 Feb 2013
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)
bym
on 28 Feb 2013
so the loop should continue while the difference is greater than .001?
Image Analyst
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.
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)