(42s) Reducing the Number of Slope Evaluations for Newton's Method
|
11-11-2024, 05:08 PM
(This post was last modified: 11-11-2024 06:35 PM by Namir.)
Post: #1
|
|||
|
|||
(42s) Reducing the Number of Slope Evaluations for Newton's Method
I introduce an HP-42s program that solves the roots of nonlinear functions using Newton’s method. What is different about this program is that it reduces the number of function calls used in calculating the slope. The HP-42s program calculates the slope using the forward divided difference method:
Code: f’(x) = (f(x+h) – f(x))/h (1) Where h = 0.001*(1 + |x|). Thus, calculating the slope requires two calls to the nonlinear function f(x)=0. If you re-implement the code to use a separate function to calculate the slope, that function should also count as a function call. So, either way a regular Newton's method iteration requires two function calls. While the HP-42s has a built-in SOLVE command, I am using this calculator to work with named registers and illustrate the algorithm. They are easier to read than numerical register used in earlier HP calculators. The program Root1 reduces the number of function calls, by using a counter k and maximum counter max, and the following steps: • The first two iterations refine the guess for the root x by using equation (1) which requires two function calls per iteration. The values for k and max are initially set to zero and then to 1 after the first iteration. • During the third iteration k has the value of 1 and max has the value of 2. Since max-k is 1, the root-seeking iteration skips the step to explicitly calculate a new slope and uses the previous value of the slope. This scheme saves one call to function f(x). • During the fourth iteration k has the value of 2 and max has the value of 2. Since max-k is 0, the root-seeking iteration calculates an updated slope value by calling f(x) twice. The variables max is set to 3 and the variable k is set to 1. • The next two iterations will skip updating the slope and use the current slope value. • The seventh iteration will have max-k = 0 and evaluates a new slope value. • The remaining iterations continue with incrementing max by 1 when k equals max, and resetting k to 1. Otherwise, the iterations simply increment k by 1 until k equals max. • The iterations stop when the absolute value for the refined guess for the root is les than the root tolerance value. Here is the HP-42s code with the test function f(x)=exp(x) – 3*x^2: Code: 01 LBL "ROOT1" The program Root1 prompts you for the values x and toler. The parameter x is the initial guess for the root. The parameter toler is the root tolerance value. The program returns count, the number of function calls, and x, the refined root value. The program Root1 works better as the initial guess for the root is close to the actual root value. Here are two sample calls to program Root1: For x = 3 and toler 1E-8 we get 20 and 3.733079029 in the Y and X registers, respectively. For x = 4 and toler 1E-8 we get 9 and 3.733079029 in the Y and X registers, respectively. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)