這裏要解決兩件事。 (1)注意,g
是函數的名稱,而g(x)
是表達式(即在x
處評估的函數的結果)。鑑於您對NR
的定義,您可以編寫NR(g, ...)
而不是NR(g(x), ...)
。 (2)Maxima更喜歡精確的數字(整數或有理)到不精確(float和bigfloat)。如果你在循環中打印出中間結果,你會發現b
正在變成一個很大的混亂表達式,如log(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
謝謝大家,我明白你的修改。 :) – krithass