Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.5K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Use While loops to find root using false position method?

Posted 5 years ago

The purpose of the code is just to learn programming using While loops, not to solve the problem. The problem is to find the root for c (the drag coefficient ) using the False Position Method. I am not getting any output and don't know what is wrong.

Clear["Global`*"]
g = 9.81 ; m = 68.1 ; t = 10 ; v = 40 ; 
cl = 12 ; cu = 16 ; ea = 1 ; 
While[ ea > .0625 ,
 fcl = g m /cl (1 - E^(- cl/ m  t)) - v  ;
 fcu = g m /cu (1 - E^(- cu/ m  t)) - v  ;
 cr = (fcl cu - fcu cl)/(fcu - fcl) ;
 fcr = g m /cr (1 - E^(- cr/ m  t)) - v  ;
 oldcr = newcr;
 newcr = cr;
 If[ fcl fcr > 0, cl = cr, cu = cr ];
 ea = Abs[(newcr - oldcr)/newcr] ;
 i = i + 1 ;
 Print[cl, cu, cr, i, ea ] 
 ]
POSTED BY: Raymond Low
4 Replies
Posted 5 years ago

The evaluation process used by Mathematica can be confusing for some new users. Some variables can be used as symbols and possibly never assigned any value. Other uses may depend on having previously assigned some value before use.

You might imagine your program before you initialized the symbol i. The program is going step by step and everything seems fine. Then it gets to i= and it realizes it is going to assign something to i. So it records that i is having something assigned to it. But what should it assign? It looks at the i+1 on the right hand side and needs to determine what value that has. To do that it needs to look up the value of i. Well we have started the assignment to i so it uses that information. But the value of i is i+1. To find out that value it again needs to look up the value of i. To do that it starts that process all over again. Which needs to look up the value of i. Which again needs to look up the value of i. Perhaps you get the idea where this is going... No going anywhere.

If you then imagine the modified program that has i=1 early on. That initialization process completes without needing to look up the previous value of i to be able to finish that. And later when you have i=i+1 that will also complete in a single step because it uses the prior value already assigned to i.

Does that help explain some of this?

POSTED BY: Bill Nelson
Posted 5 years ago

Include i=1 before your While so that i has some initial value to later increment.

Change Print[cl, cu, cr, i, ea] to Print[{cl, cu, cr, i, ea}] to better see your result.

POSTED BY: Bill Nelson
Posted 5 years ago

Yes, that was very helpful. Thank you for taking the time to explain it Best Regards

POSTED BY: Raymond Low
Posted 5 years ago

Very good that did it, Thank you much Bill -- I gather some variables, but not all, must be initialized outside the loop. How would I know the difference as to which variables need to be initialized outside the loop to those who can be initialized within the loop? new code

Clear["Global`*"]
f[c_] := g m /c (1 - E^(- c/m  t)) - v  ;
g = 9.81 ; m = 68.1 ; t = 10 ; v = 40 ; 
cl = 12 ; cu = 16 ; ea = 1 ; i = 0 ; oldcr = cl ; newcr = cu ; 
While[ ea > .0625 ,
 fcl = f[c] /. c -> cl  ;
 fcu = f[c] /. c -> cu  ;
 cr = cu  - fcu ( cl - cu )/(fcl - fcu) ;
 fcr = f[c] /. c -> cr  ;
 oldcr = newcr;
 newcr = cr;
 If[ fcl fcr > 0, cl = cr, cu = cr ];
 ea = Abs[(newcr - oldcr)/newcr] ;
 i = i + 1 ;
 Print[{cl, cu, cr, i, ea}] 
 ]
POSTED BY: Raymond Low
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard