Post Reply 
(12C) Newton's Method
03-29-2018, 05:04 PM (This post was last modified: 03-29-2018 05:25 PM by Dieter.)
Post: #8
RE: (12C) Newton's Method
(03-29-2018 05:11 AM)Carsen Wrote:  I like it too. Gamo said it all. Key in your guess and press [R/S]. Well done

Thank you. But there is always a way to squeeze out a few bytes. Here is an optimized version with some changes. These include:
  • Instead of R0, R1, R3 and R4 the program now uses four consecutive registers, R0...R3.
  • The f(x)=0 test was removed. It is not required since in this case the correction term is zero as well so that the iteration will exit anyway. This way one more call of f(x+h) may be required, but hey: who cares if you can save two steps? ;-)
  • The exit condition now is set to a relative error < 1E–8. This is slightly higher than before but it makes sure that even in the worst case the program finishes if the last change in x affected only the last one ore two digits. You can always add one more iteration with a simple [R/S]. Also the test of the last correction term (is it below the threshold or not?) has been changed to a slightly more effective method.
  • Here and there the code was optimized to save the one or other step.
All this lead to the version below which now is down to 40 steps (plus the f(x) code).
Here is a commented listing:

Code:
01 STO 1   // store initial guess
02 1
03 STO 0   // flag = 1
04 RCL 1   // call f(x)
05 GTO 41
06 RCL 0   // function calls return here
07 x=0?    // is this a return from f(x+h)?
08 GTO 22  // then continue with Newton correction
09 STO-0   // flag = 0
10 Rv
11 STO 2   // save f(x)
12 RCL 1   // calculate h
13 x=0?
14 e^x     // set h = x/10000, or 1/10000 if x=0
15 EEX
16 4
17 /
18 STO 3   // store h
19 RCL 1
20 +
21 GTO 41  // call f(x+h)
22 Rv
23 RCL 2   // calculate correction term
24 -       // according to Newton's method
25 RCL 3
26 /       // f'(x)
27 STO/2
28 RCL 2   // correction term = f(x)/f'(x)
29 STO-1   // adjust x
30 RCL 3
31 /       // = relative correction * 10000
32 EEX
33 4
34 x       // = relative correction * 1E+8
35 INTG
36 x=0?    // was |relative correction| < 1E-8 ?
37 GTO 39  // then exit
38 GTO 02  // else do another loop
39 RCL 1   // return x
40 GTO 00  // and quit
41 .       // start f(x) here
   .
   .
nn GTO 06  // end f(x) with this line

As usual, comments and suggestions are welcome.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Newton's Method - Gamo - 03-24-2018, 01:10 PM
RE: (12C) Newton's Method - Dieter - 03-24-2018, 01:31 PM
RE: (12C) Newton's Method - Dieter - 03-27-2018, 06:08 PM
RE: (12C) Newton's Method - Gamo - 03-25-2018, 03:05 AM
RE: (12C) Newton's Method - Dieter - 03-25-2018, 06:26 PM
RE: (12C) Newton's Method - Gamo - 03-28-2018, 04:23 AM
RE: (12C) Newton's Method - Carsen - 03-29-2018, 05:11 AM
RE: (12C) Newton's Method - Dieter - 03-29-2018 05:04 PM
RE: (12C) Newton's Method - Gene - 03-29-2018, 05:23 PM
RE: (12C) Newton's Method - Gamo - 03-30-2018, 01:15 PM
RE: (12C) Newton's Method - Dieter - 03-30-2018, 02:00 PM
RE: (12C) Newton's Method - Gamo - 03-31-2018, 01:15 PM
RE: (12C) Newton's Method - Dieter - 03-31-2018, 05:27 PM



User(s) browsing this thread: 3 Guest(s)