Program NEWT2 - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Program NEWT2 (/thread-10348.html) |
Program NEWT2 - KarelPostulart - 03-19-2018 02:58 PM Hi, The program below is to my opinion an improved working variation on the program provided by Han. The original version led to an error message. Original version EXPORT NEWT2() BEGIN LOCAL n, xold, xnew, err, N, f; N := 100; err := .00001; xnew := 1; IF INPUT( {f, xnew, err, N}, "Newton's Method", {"f(X) = ", "Guess = ", "Error = ", "Max Iter. = "}, { "Enter the function surrounded by single quotes", "Enter the initial guess", "Enter the tolerance", "Enter the maximum number of iterations" }, {f, xnew, err, N} ) THEN F1 := f; CAS("F0 := id-F1/F1'"); L1 := {}; L1(1) := xnew; FOR n FROM 2 TO N+1 DO xold := xnew; xnew := F0(xold); L1(n) := xnew; IF ABS(xnew-xold) < err THEN break; END; END; EDITLIST(L1); END; Improved version EXPORT NEWT2 BEGIN LOCAL n, xold, xnew, err, N, f; N := 100; err := .00001; xnew := 1; f:='X'; // By defining f as'X' as a default value // the program works IF INPUT( {f, xnew, err, N}, "Newton's Method", {"f(X) = ", "Guess = ", "Error = ", "Max Iter. = "}, { "Enter the function surrounded by single quotes", "Enter the initial guess", "Enter the tolerance", "Enter the maximum number of iterations" }, {f, xnew, err, N} ) THEN F1 := f; CAS("F0 := id-F1/F1'"); L1 := {}; L1(1) := xnew; FOR n FROM 2 TO N+1 DO xold := xnew; xnew := F0(xold); L1(n) := xnew; IF ABS(xnew-xold) < err THEN break; END; END; EDITLIST(L1); END; END; |