The Museum of HP Calculators

HP Forum Archive 15

 Newton's MethodMessage #1 Posted by John Bower on 8 Mar 2006, 1:05 p.m. I'm having a little trouble. My textbook requires that I preform Newton's method on my calculator. They show how to do it on a TI-84 or something like that but I have a HP 49g+. Can anyone take me throught the steps? Thanks a lot. John

 Re: Newton's MethodMessage #2 Posted by Vassilis Prevelakis on 8 Mar 2006, 2:47 p.m.,in response to message #1 by John Bower are you asking us to do your homework? **vp

 Re: Newton's MethodMessage #3 Posted by John on 8 Mar 2006, 3:42 p.m.,in response to message #2 by Vassilis Prevelakis No. I'm asking for help with working a calculator that I am unfamiliar with.

 Re: Newton's MethodMessage #4 Posted by Namir on 8 Mar 2006, 3:41 p.m.,in response to message #1 by John Bower The folloing code assumes that FX stores the function you want to solve: ```<< "TOLERANCE" "1E-8" INPUT OBJ-> "GUESS" "" INPUT OBJ-> -> T X << 0 0 0 0 -> H D F I << DO 'I' INCR DROP X ABS 1 + 0.01 * 'H' STO X FX 'F' STO X H + FX F - INV H * F * 'D' STO X D - 'X' STO UNTIL D ABS T < I 55 > OR END I "ITERS" ->TAG X "ROOT" ->TAG >> >> >> ``` The program prompts you to enter the root tolerance and a guess for the root. The program displays the number of iterations and the last refined guess for the root. If the iterations exceed 55, the program exits. Hope this will do the job. Namir PS: My consulting fees will be emailed to you. Shoudl email fail, Mr T will show up in person with the invoice. I strongly suggest paying him cash AND without delay! :-) Edited: 8 Mar 2006, 3:43 p.m.

 Re: Newton's MethodMessage #5 Posted by John on 8 Mar 2006, 3:50 p.m.,in response to message #4 by Namir Thanks for the response, but I don't really follow how to do it. John

 Re: Newton's MethodMessage #6 Posted by Namir on 8 Mar 2006, 4:41 p.m.,in response to message #5 by John The code uses the basic Newton algorithm: X(new) = X - f(X) / f'(X) The code approximates the derivative f'(X) using: f'(X) = (f(X+h) - f(X)) / h where h = 0.01 * (1 + |X|). This expresion insures that h is just above 0.01 when X very small. So the first equation becomes: X (new) = X - h * f(X)/(f(X+h) - f(X)) Since f(X) appears twice, it's better to save it to a variable (F in the program). The term f(X)/f'(X) is the guess refinement, stored in variable D in the program. Iteration stops when the absolute value of this refinement is less than the tolerance value (stored in variable T in the program). Now here comes your part of the homework. Reconcile the code with the above information. Edited: 8 Mar 2006, 10:33 p.m.

 Re: Newton's MethodMessage #7 Posted by Karl Schneider on 8 Mar 2006, 11:04 p.m.,in response to message #5 by John From Namir: ```<< "TOLERANCE" "1E-8" INPUT OBJ-> "GUESS" "" INPUT OBJ-> -> T X << 0 0 0 0 -> H D F I << DO 'I' INCR DROP X ABS 1 + 0.01 * 'H' STO X FX 'F' STO X H + FX F - INV H * F * 'D' STO X D - 'X' STO UNTIL D ABS T < I 55 > OR END I "ITERS" ->TAG X "ROOT" ->TAG >> >> >> ``` Quote: PS: My consulting fees will be emailed to you. Should email fail, Mr T will show up in person with the invoice. I strongly suggest paying him cash AND without delay! From John: Quote: Thanks for the response, but I don't really follow how to do it. Neither do I, but I'll try it on my 48G or 49G. "I pity the fool" who must program in RPL! ;-) Good thing that the 49G+ has equation solving built in. -- KS

 Re: Newton's MethodMessage #8 Posted by Namir on 9 Mar 2006, 1:44 a.m.,in response to message #7 by Karl Schneider Karl, Using the Solver gets the HP calculator an A and John an F!!! :-) Namir

 Re: Newton's MethodMessage #9 Posted by Valentin Albillo on 9 Mar 2006, 6:35 a.m.,in response to message #7 by Karl Schneider Hi, Karl: Karl posted: ""I pity the fool" who must program in RPL! ;-)" Indeed ! Especially as the algorithm itself is so simple. This would be the no-frills HP-71B version, and I'm sure everyone would agree it's much more understandable (line numbers are arbitrary and comments aren't needed, of course): ``` 100 ! Basic Newton's Method: x1=x0-f(x)/f'(x) 110 ! 120 ! Ask for initial guess and tolerance 130 ! 140 INPUT "Initial guess=";X0 @ INPUT "Tolerance=";T @ D=.00001 150 ! 160 ! Perform up to 50 iterations and output result 170 ! 180 FOR I=1 TO 50 @ X1=X0-FNF(X0)/FND(X0) 190 IF ABS(X1-X0)RUN Initial guess = 2 Tolerance = 1E-10 Root = 2.60167913188 >RUN Initial guess = 0 Tolerance = 1E-10 Root = -.339876886624 >RUN Initial guess = -2 Tolerance = 1E-10 Root = -2.26180224527 ``` if no convergence occurs within 50 iterations (because the equations has no real roots, for instance), such as when f(x)=x2+1 = 0, this is what happens: ``` >230 DEF FNF(X)=X^2+1 >RUN Initial guess = 2 Tolerance = 1E-10 No convergence after 50 iterations ``` An important difference between my HP-71B version and Namir's RPL one is that I compute the derivative using a much more accurate 2nd-order method (error proportional to h2) instead of the 1st-order method (error proportional to h) he uses. Not that great accuracy is required for Newton's method, mind you, and you need three calls to f(x) instead of only two, but then you have the bonus of being able to compute a fairly accurate derivative right from the keyboard by simply calling FND with your argument. " Good thing that the 49G+ has equation solving built in." Oh, if built-in solvers are permitted then the HP-71B version is simply: ``` PRINT FNROOT(0,3,FVAR^3-6*FVAR-2) 2.60167913189 ``` where you can even omit the PRINT. I don't know why, but I strongly suspect that the 49G+ built-in equation solving process is still more complicated than that. As some clever contributor told me recently in a private communication: "I have to admit that RPL makes you focus more on the technicalities of the stack (DUP, DUP2, ROLL, ROLLD, OVER, PICK, etc...) rather than on your algorithms ..." I couldn't have said it better. Best regards from V. Edited: 9 Mar 2006, 6:57 a.m.

Go back to the main exhibit hall