Post Reply 
Newton's method
01-01-2018, 03:49 PM
Post: #2
RE: Newton's method
Hi Namir, I like the compactness of the solution! I hope you don't mind a few tweaks that perhaps make it better:
  • Added incrementing i so that the program doesn't loop indefinitely
  • Removed conversion of sfx to lowercase to make it compatible with the Function App
  • Changed x to X to make it compatible with functions stored in F0-F9 in the Function App
  • Added a test that if maxiter is reached, it returns "No root found" rather than the last (possibly inaccurate) estimate

Code:

EXPORT NEWTON(sfx, X, toler)
BEGIN
  LOCAL x0, h, f, diff, i, maxiter;
  
  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;
    i := i + 1;
  END;
  IF i==maxiter THEN
    RETURN("Root not found");
  ELSE
    RETURN(X);
  END;
END;

With these changes, if you have functions in the Function App that plot nicely like for example F1(X)=17*SIN(1.26*X-90)-5, then you can run NEWTON("F1(X)",1,0.00000001) and find the root 1.85233166719.
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)