最大差异:变量不能是数字;发现:1

时间:2017-03-14 11:37:29

标签: math maxima newtons-method

对不起我的英语,这不是我的母语。我正在尝试将Newton-Raphson算法建立到方程和非线性系统的数值分辨率上。

我有Windows 10和最大值14.12.1。这是我的算法:

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))

);

当我尝试在此功能上评估时:

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

我收到此错误:

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);

我不知道如何解决这个错误。请帮助我,谢谢所有人。

1 个答案:

答案 0 :(得分:1)

这里要解决两件事。 (1)注意g是函数的名称,而g(x)是表达式(即在x评估的函数的结果)。根据您对NR的定义,您可以写NR(g, ...)而不是NR(g(x), ...)。 (2)Maxima更喜欢精确数字(整数或理性)到不精确(浮动和大浮动)。如果您在循环中打印出中间结果,您会发现blog(4)log(8)等方面变得非常混乱。用float包裹它以使其被评估为单个浮点数。

这是修订版。

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)))$

以上是上述版本的示例。

(%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