Post Reply 
Newton's method
12-27-2017, 03:41 AM (This post was last modified: 12-27-2017 04:20 AM by Namir.)
Post: #1
Newton's method
This is a version of Newton's method for the HP Prime where you specify a string representing f(x), along with the initial guess for the root, and the tolerance value. Thus you can solve for different functions without having to hard code these functions in the RPL code. Here is the source code:

Code:
EXPORT NEWTON(sfx, x, toler)
BEGIN
  LOCAL x0, h, f, diff, i, maxiter;
  
  sfx := LOWER(sfx);
  maxiter :=100;
  i := 0;
  diff := 2*toler;
  WHILE ABS(diff) > toler AND i < maxiter DO
    h := 0.01*(1 +ABS(x));
    f := EXPR(sfx);
    x0 := x;
    x := x + h;
    diff := h *f / (EXPR(sfx) - f);
    x := x0 - diff;
  END;
  RETURN x;
END;

Examples of using function NEWTON are:

NEWTON("exp(x)-3*x^2",4,0.0000001)
NEWTON("ln(100)-x",4,0.0000001)

The code convert the string of the expression into lowercase to make sure it uses variable x in lowercase, to match the lowercase parameter x. Also, the code controls the number of iterations not to exceed 99.

You can insert several function declarations with the above code and then pass a string containing the call to a particular function. For example, if we insert the following code before NEWTON:

Code:
EXPORT fx1(x)
BEGIN
  RETURN EXP(x)-3*x^2;
END;

EXPORT fx2(x)
BEGIN
  RETURN ln(100)-x;
END;

Then you can make the following calls:

NEWTON("fx1(x),4,0.0000001)
NEWTON("fx2(x)",4,0.0000001)

Which give the same results as before. This approach allows you to insert a catalogue of functions you frequently work and then invoke the solution for their roots by specifying the function's name instead of its expression. In the case the function requires more than a single statement to calculate the result, then this approach is definitely the one to choose!

If you comment out the statement that converts parameter sfx to lower case, then you can specify the Functions App's F0 through F9. Make sure that you use formats like "F1(x)" with the lowercase x.

Enjoy!

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


Messages In This Thread
Newton's method - Namir - 12-27-2017 03:41 AM
RE: Newton's method - Mark Power - 01-01-2018, 03:49 PM



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