Search code examples
mathmaximanewtons-method

Maxima diff: variable must not be a number; found: 1


sorry for my english,it isn´t my mother tongue.I´m trying to build Newton-Raphson algorithm to numerical resolution of equations and non-linear systems.

I have windows 10 and maxima 14.12.1.This is my algorithm:

NR(f,a,tol,n):=block(
define(k(x),diff(f(x),x)),
for i:1 thru n do(
    b : a - f(a)/k(a),
    if abs(b-a)<tol then 
         return (float(b))
    else
         a:b
),return (float(a))

);

When i try to evaluate this on this function:

g(x) :=x^3 -(3*x^2)*2^(-x)+3*x*4^(-x)-8^(-x);
NR(g(x),1,10^(-6),100);

I get this error:

diff: variable must not be a number; found: 1
#0: k(x=1)
#1: NR(f=-1/8^x+3*x/4^x-3*x^2/2^x+x^3,a=1,tol=1/1000000,n=100)
 -- an error. To debug this try: debugmode(true);

I dont know how can i solve this error.Please help me and thanks for all.


Solution

  • Two things to fix here. (1) Note that g is the name of a function while g(x) is an expression (namely the result of the function evaluated at x). Given your definition of NR you can write NR(g, ...) instead of NR(g(x), ...). (2) Maxima prefers exact numbers (integer or rational) to inexact (float and bigfloat). If you print out intermediate results within the loop, you'll see that b is turning into a big messy expression in terms of log(4) and log(8) and so on. Wrap it with float to cause that to be evaluated to a single float number.

    Here's a revised version.

    NR(f,a,tol,n):=block(define(k(x),diff(f(x),x)),
     for i thru n do
       (b:float(a-f(a)/k(a)),
        printf(true,"iteration ~d, a=~a, b=~a, (b - a)=~a~%",i,a,b,b-a),
        if abs(b-a) < tol then return(float(b))
        else a:b),return(float(a)))$
    

    Here's your example with the above version.

    (%i22) NR(g, 1, 10^(-6), 100);
    iteration 1, a=1, b=0.8762290691947893, (b - a)=-0.1237709308052107
    iteration 2, a=0.8762290691947893, b=0.7960328661177172, (b - a)=-0.08019620307707209
    iteration 3, a=0.7960328661177172, b=0.7435978033304336, (b - a)=-0.0524350627872836
    iteration 4, a=0.7435978033304336, b=0.7090971628901521, (b - a)=-0.03450064044028145
    iteration 5, a=0.7090971628901521, b=0.6862988635291022, (b - a)=-0.02279829936104993
    iteration 6, a=0.6862988635291022, b=0.6711896281138916, (b - a)=-0.01510923541521059
    iteration 7, a=0.6711896281138916, b=0.6611565733873623, (b - a)=-0.01003305472652927
    iteration 8, a=0.6611565733873623, b=0.6544855241390547, (b - a)=-0.006671049248307637
    iteration 9, a=0.6544855241390547, b=0.6500459976322316, (b - a)=-0.00443952650682311
    iteration 10, a=0.6500459976322316, b=0.6470897956059268, (b - a)=-0.002956202026304755
    iteration 11, a=0.6470897956059268, b=0.6451205413528185, (b - a)=-0.001969254253108343
    iteration 12, a=0.6451205413528185, b=0.6438083926046423, (b - a)=-0.001312148748176201
    iteration 13, a=0.6438083926046423, b=0.6429339322360853, (b - a)=-8.744603685569841E-4
    iteration 14, a=0.6429339322360853, b=0.6423510944072349, (b - a)=-5.828378288503799E-4
    iteration 15, a=0.6423510944072349, b=0.6419625961778163, (b - a)=-3.884982294186656E-4
    iteration 16, a=0.6419625961778163, b=0.6417036241811079, (b - a)=-2.589719967083237E-4
    iteration 17, a=0.6417036241811079, b=0.6415309880468466, (b - a)=-1.726361342613281E-4
    iteration 18, a=0.6415309880468466, b=0.6414159027268955, (b - a)=-1.150853199510804E-4
    iteration 19, a=0.6414159027268955, b=0.6413391816804196, (b - a)=-7.672104647593603E-5
    iteration 20, a=0.6413391816804196, b=0.6412880347133941, (b - a)=-5.114696702546162E-5
    iteration 21, a=0.6412880347133941, b=0.6412539385204955, (b - a)=-3.409619289862498E-5
    iteration 22, a=0.6412539385204955, b=0.6412312089557869, (b - a)=-2.272956470861232E-5
    iteration 23, a=0.6412312089557869, b=0.6412160535014019, (b - a)=-1.515545438501853E-5
    iteration 24, a=0.6412160535014019, b=0.6412059475250002, (b - a)=-1.010597640171973E-5
    iteration 25, a=0.6412059475250002, b=0.6411992383365198, (b - a)=-6.709188480336081E-6
    iteration 26, a=0.6411992383365198, b=0.6411946523270589, (b - a)=-4.586009460960661E-6
    iteration 27, a=0.6411946523270589, b=0.6411918666867252, (b - a)=-2.785640333624606E-6
    iteration 28, a=0.6411918666867252, b=0.6411902285428732, (b - a)=-1.638143852011886E-6
    iteration 29, a=0.6411902285428732, b=0.6411883963195618, (b - a)=-1.832223311404313E-6
    iteration 30, a=0.6411883963195618, b=0.6411901425747176, (b - a)=1.746255155810061E-6
    iteration 31, a=0.6411901425747176, b=0.6411885554504851, (b - a)=-1.587124232482751E-6
    iteration 32, a=0.6411885554504851, b=0.6411862242380371, (b - a)=-2.33121244808121E-6
    iteration 33, a=0.6411862242380371, b=0.6412129051345152, (b - a)=2.668089647817062E-5
    iteration 34, a=0.6412129051345152, b=0.6412038661598046, (b - a)=-9.038974710606773E-6
    iteration 35, a=0.6412038661598046, b=0.6411978270328432, (b - a)=-6.039126961399077E-6
    iteration 36, a=0.6411978270328432, b=0.641193747387054, (b - a)=-4.079645789190067E-6
    iteration 37, a=0.641193747387054, b=0.6411909672221692, (b - a)=-2.780164884863545E-6
    iteration 38, a=0.6411909672221692, b=0.641189166438378, (b - a)=-1.80078379119486E-6
    iteration 39, a=0.641189166438378, b=0.6411875933891164, (b - a)=-1.573049261627268E-6
    iteration 40, a=0.6411875933891164, b=0.6411875933891164, (b - a)=0.0
    (%o22)                        0.6411875933891164