kill(all)$

maxima:programming , fixed point iteration (find :sqrt(2)),version 1.0
peter.vlasschaert@gmail.com,30/08/2022

rem : for loops "see:below "  here we use "while condition".

result : make analog code in python.

find : f(x),root for f(x) = 0 ⇒ x^2 =2 =0 ⇒ x =sqrt(2)
result : x=g(x)

→ parameter

tol:0.0000000000001;
niter:100;

→ x = g(x)

g(x):=1/2*(2/x+x);

rem g(x) =1/2*(a/x+x), Heron Method : see below.

⇒ see below :g(x)

→ init values

x0:0.9$

i:1$

→ code : fixed iteration

while(i≤niter) do(
    x : g(x0),
   ( if i=1 then print(1,"th iteration =",x0) ),      
   if abs(x-x0) < tol then   
        return("")
    else
        (i:i+1,
        print(i,"th iteration =",x),
        x0:x)
     )$

print("fixed iteration f(x)=0 ⇒ x =",sqrt(2))$

→ numerical

s1:print(sqrt(2),"=",print(x))$

→ exact :

s2:print(sqrt(2),"=",float(sqrt(2)))$

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

find : g(v) → g(x)
---------------------

q1a: v^2-2 =0;
q1b: q1a+2;
q1c: q1b/v;
q1d:q1c+v;
q1e:q1d/2;
q1f:lhs(q1e) = expand(rhs(q1e));

subst : v → x

r1a:f1(y):=y^2-2;
r1b:diff(f1(y),y);

newton raphson :

r1c:y=expand(ratsimp(y-f1(y)/r1b));

r1d:lhs(r1c) = 1/2*c;

r1e:rhs(r1d) = rhs(r1c);

r1f:solve(r1e,c);

result :subst(r1f,r1d);

result1:rhs(result);

same : if v=y ⇒ q1f =r1c ⇒ true

fixed point : sqrt(a) , a=2
-----------------------------

Heron's METHOD: see "above : g(x),a=2"

 x → y , g → h :

h(y):=''result1;

h(sqrt(2));

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

appendix : for loops.
------------

→ for ⇒ in

for k in [1,2,3] do block(
     if k=2 then y:(7))$

print(y)$

→ for ⇒ thru

 for i thru 5
      do print ("found j =", catch (for j thru 5
                      do if i = j then throw (j)))$

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