kill(all)$

maxima :theoretical, simple code for find values f(x)=0,version 1.0
peter.vlasschaert@gmail.com,25/05/2023.

info :https://www.wikiwand.com/nl/Methode_van_Newton-Raphson

theoretical base : to find g(xx) =0
--------------------------------------------

=============================================================================

definition : limit function for derivative, f'(x)
----------------------------------------------------------
==========================================
f' '(x) = limit       (f(x+Δx) -f(x))/Δx ≅  (f(x+Δx) -f(x))/Δx
            Δx→0
==========================================

q1x:[[v=xx0],[xx=xx0]];

rem : How to use "at" for differential for g'(xx)
------------------------------------------------------------

q1a:g(xx+Δx) = g(xx)+at(diff(g(xx),xx),xx=v)*Δx;

q1b:subst([xx=xx0],lhs(q1a))=subst([v=xx0],part(rhs(q1a),1))+subst([xx=xx0],part(rhs(q1a),2));

use : list from q1x,use sum for rhs "q1a"
---------------------------------------------------

→ length : rhs(expression)

q1bx1:length(rhs(q1b));

→ use :sum.

q1bx2:subst([xx=xx0],lhs(q1a)) = sum(subst(q1x[j],part(rhs(q1a),j)),j,1,q1bx1);

check : rhs(q1b)  ? rhs(q1bx2)
======================

is(rhs(q1b)=rhs(q1bx2));

set :Δx = xx-xx0

q1b1:Δx = xx-xx0;

q1c:subst([q1b1],q1b);

find root:  g(xx) =0, lhs
------------------------------

first step :
=======

q1b:subst([g(xx)=0],lhs(q1c)) = rhs(q1c);

q1b1:expand(solve(q1b,xx))[1];

second step :lhs ,xx=xx1 , next value approximation of root g(xx)
==================================================

q1b2:subst([xx=xx1],lhs(q1b1)) =rhs(q1b1);

thrid step : iteration to find roots g(xx), given : j=1, xx[0]
===========================================

q1b3:subst([xx1=xx[j],xx0=xx[j-1]],q1b2);

=============================================================================

example : g(xx) = xx^2-2
----------------------------------

definition : f(x)
---------------------

f(x):=x^2-2;

definition : f'(x)
---------------------

df(y):=subst([x=y],diff(f(x),x));

df(1.5);

x1=1.5-f(1.5)/df(1.5);

x[0]:1.5;

i:1;

x[i]=x[i-1]-f(x[i-1])/df(x[i-1]);

see "above" : theoretical base for the problem
------------------------------------------------------------

for i:1 thru 5 do
(x[i]:=x[i-1]-f(x[i-1])/df(x[i-1]),print("x[",i,"]=",x[i]));

ev(x[1],nouns);

first : without block programming
---------------------------------------------

for i:1 thru 5 do
(x[i]:=x[i-1]-f(x[i-1])/df(x[i-1]));
for i:0 thru 5 do
(print("x[",i,"]=",x[i]))
$

x[0];

second : with block programming without use of the return statement
-------------------------------------------------------------------------------------------

myratroot(xx0):=block([],x[0]:xx0,
for i:1 thru 5 do
(x[i]:x[i-1]-f(x[i-1])/df(x[i-1])),
for i:0 thru 5 do
(print("x[",i,"]=",x[i]))  
)$

myratroot(1)$

p1:makelist(x[j],j,0,5);

p1l:length(p1);

p1x:makelist(float(i),i,1,p1l);

p1y:float(p1);

p1ly:length(p1y);

pxy:makelist([p1x[i],p1y[i]],i,1,p1ly);

find exact root : positive root, negative root of f(x)
-------------------------------------------------------------------

rem : see definition f(x), "see above".

 p2:f(x) =0;

p2a:solve(p2,x);

p2b:float(p2a);

plot: pos roots of f(x) =0
--------------------------------

 wxplot2d(
        [[discrete, pxy],[discrete, pxy]],
        [style, points, lines], [color, red, blue],
        [point_type, asterisk],
        [title,"pos roots of f(x)"],
        [legend, "values", "values"],
        [xlabel, "number of iterations"],
        [ylabel, "values : roots of f(x)"]
              )$

find negative root
-----------------------

myratroot(-1.0)$

p3y:makelist(x[j],j,0,5);

pnxy:makelist([p1x[i],p3y[i]],i,1,p1ly);

plot : neg roots of f(x) =0
--------------------------------

wx before plot : result of inline plot.

 wxplot2d(
        [[discrete, pnxy],[discrete, pnxy]],
        [style, points, lines], [color, red, blue],
        [point_type, asterisk],
        [title,"neg roots of f(x)"],
        [legend, "values", "values"],
        [xlabel, "number of iterations"],
        [ylabel, "values : roots of f(x)"]
              )$

------------------------------------------------------------------------------------------------------------