已知4點座標
n=4
x f(x)
==========
1.0 0.0
2.0 0.693
3.0 1.099
4.0 1.386
求牛頓向後差除表
程式
using Printf
x=[ 1.0 , 2.0 , 3.0 ,4.0]
f= [ [0.00 ,0.0 , 0.0 ,0.0 ,0.0 ,0.0 ],
[0.693 ,0.0 , 0.0 ,0.0 ,0.0 ,0.0 ],
[1.099 ,0.0 , 0.0 ,0.0 ,0.0 ,0.0 ],
[1.386 ,0.0 , 0.0 ,0.0 ,0.0 ,0.0 ]]
y=f
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
d=[0.0 for i=1:n]
for i=1:n
d[i]=f[1][i]
end
s=@sprintf("牛頓前向差除表")
println(s,d)
#================================================
for(i=1;i<=n;i++)
{
printf("%4.2lf %8.5lf",x[i],f[i][0]);
for(j=1;j<=i;j++)
{
f[i][j]=(f[i][j-1]-f[i-1][j-1])/(x[i]-x[i-j]);
printf(" %8.5lf",f[i][j]);
}
printf("\n");
}
===============================================#
println()
for i=2:n
for j=2:i
y[i][j]=(y[i][j-1]-y[i-1][j-1]) / (x[i]-x[i-j+1])
# println(y[i][j])
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:i
s=@sprintf("\t%8.5f",y[i][j])
print(s)
end
println()
end
d=[0.0 for i=1:n]
for i=1:n
d[i]=y[n][i]
end
s=@sprintf("牛頓後向差除表")
println(s,d)
輸出畫面
沒有留言:
張貼留言