x f(x)
===========
0 0
1 -3
2 0
3 15
4 48
===========
程式
using Printf
x= [0.0 , 1.0 ,2.0 , 3.0 , 4.0 ]
f= [[ 0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[-3.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[15.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[48.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ]]
n=length(x)
println(" Divided Difference Table: ")
println("=============================")
for j=2:n
for i=1:n-j+1
f[i][j]=(f[i+1][j-1]-f[i][j-1])/(x[i+j-1]-x[i])
end
end
print("i\tx(i)\t\tf(i)\t\tf(i,i+1)\tf(i,i+1.i+2), ......................\n")
for i=1:n
s=@sprintf("%d\t%8.5f",i,x[i])
print(s)
for j=1:n-i+1
s=@sprintf("\t%8.5f",f[i][j])
print(s)
end
println()
end
輸出畫面
Divided Difference Table:
=============================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......................
1 0.00000 0.00000 -3.00000 3.00000 1.00000 0.00000
2 1.00000 -3.00000 3.00000 6.00000 1.00000
3 2.00000 0.00000 15.00000 9.00000
4 3.00000 15.00000 33.00000
5 4.00000 48.00000
i=1 到 5
Pn(x)= 0.0 + -3.0 (x-0.0) + 3.0 (x -0.0)(x -1.0) + 1.0 (x -0.0)(x -1.0) ()x -2.0) + 0.0 (x -0.0)(x -1.0) ()x -2.0)(x -3.0)
i=3 到 5
Pn(x)= 0.0 + 15 (x-2.0) + 9.0 (x -2.0)(x -3.0)
沒有留言:
張貼留言