先使用牛頓向前的內插多項式
x= f(x)= f ' (x)= f ''(x)=
=============================================
0.15 0.1761 2.8775 -17 前向
0.17 0.2304 2.5675 -14.75 中央 公式相同
0.19 0.2788 2.295 -12.5 中央 公式相同
0.21 0.3222 2.06 -10.25 後向
h=0.02
程式
using Printf
x=[ 0.15 , 0.17 , 0.19 , 0.21 ]
f= [ [0.1761 ,0.0 , 0.0 ],
[0.2304 ,0.0 , 0.0 ],
[0.2788 ,0.0 , 0.0 ],
[0.3222 ,0.0 , 0.0 ] ]
h=abs(x[1]-x[2])
i=1
s=@sprintf("f(%0.3f)的微分.......",x[i])
println(s)
fd1=( 2*f[i+3][1] - 9*f[i+2][1] + 18*f[i+1][1] - 11*f[i][1] ) / (6*h)
fd2=( - f[i+3][1] + 4*f[i+2][1] + -5*f[i+1][1] + 2*f[i][1] ) / (h*h)
s=@sprintf("一次微分值= %0.4f , 二次微分值= %0.4f ",fd1,fd2)
f[i][2]=fd1
f[i][3]=fd2
println(s)
println("\n")
i=2
s=@sprintf("f(%0.3f)的微分.......",x[i])
println(s)
fd1=(f[i+1][1]- f[i+-1][1]) / (2*h)
fd2=(f[i+1][1] - 2*f[i][1] + f[i-1][1] ) / (h*h)
s=@sprintf("一次微分值= %0.4f , 二次微分值= %0.4f ",fd1,fd2)
f[i][2]=fd1
f[i][3]=fd2
println(s)
println("\n")
i=3
s=@sprintf("f(%0.3f)的微分.......",x[i])
println(s)
fd1=(f[i+1][1]- f[i+-1][1]) / (2*h)
fd2=(f[i+1][1] - 2*f[i][1] + f[i-1][1] ) / (h*h)
s=@sprintf("一次微分值= %0.4f , 二次微分值= %0.4f ",fd1,fd2)
f[i][2]=fd1
f[i][3]=fd2
println(s)
println("\n")
i=4
s=@sprintf("f(%0.3f)的微分.......",x[i])
println(s)
fd1=( 11*f[i][1] - 18*f[i-1][1] + 9*f[i-2][1] - 2*f[i-3][1] ) / (6*h)
fd2=( 2*f[i][1] - 5*f[i-1][1] + 4*f[i-2][1] - f[i-3][1] ) / (h*h)
s=@sprintf("一次微分值= %0.4f , 二次微分值= %0.4f ",fd1,fd2)
f[i][2]=fd1
f[i][3]=fd2
println(s)
println("\n")
println("x\tf(x)\t\tf'(x)\t\tf''(x)")
for i=1:4
print(x[i],"\t")
s=@sprintf("%0.5f\t\t%0.5f\t\t%0.5f",f[i][1],f[i][2],f[i][3])
println(s)
end
輸出結果
f(0.150)的微分.......
一次微分值= 2.8775 , 二次微分值= -17.0000
f(0.170)的微分.......
一次微分值= 2.5675 , 二次微分值= -14.7500
f(0.190)的微分.......
一次微分值= 2.2950 , 二次微分值= -12.5000
f(0.210)的微分.......
一次微分值= 2.0600 , 二次微分值= -10.2500
x f(x) f'(x) f''(x)
0.15 0.17610 2.87750 -17.00000
0.17 0.23040 2.56750 -14.75000
0.19 0.27880 2.29500 -12.50000
0.21 0.32220 2.06000 -10.25000
沒有留言:
張貼留言