n=5
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
依照牛頓差除法值計算P(0.2)之值
=============程式===============
print ("Divided Differences (Newton) 插除法")
n = 5+1
vector=[0.0 , 0.1 , 0.3 , 0.6 , 1.0 , 1.1]
matrix=[[-6.0 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ,
[-5.89483 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ,
[-5.65014 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ,
[-5.17788 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ,
[-4.28172 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ,
[-3.99583 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
point_offset = 0.2
for i in range(1,n):
for j in range(i,n):
print ("i=%5d , j=%5d" %(i , j) )
print ("(%0.6f - %0.6f ) / (%0.6f - %0.6f) ) " % ((matrix[j][i-1]) , ( matrix[j-1][i-1]) , (vector[j]) ,( vector[j-i]) ) )
matrix[j][i] = ( (matrix[j][i-1]-matrix[j-1][i-1]) / (vector[j]-vector[j-i]))
print ("matrix[ %5d] [ %5d] =%0.6f " %(j, i, (matrix[j][i-1]-matrix[j-1][i-1])/(vector[j]-vector[j-i])) )
for i in range(n):
print ((matrix[i]) )
aprx = 0
mul = 1.0
for i in range(n):
print ("matrix[%5d][%5d] = %0.6f" %(i,j,matrix[i][i]) )
mul = matrix[i][i];
for j in range(1,i+1):
mul = mul * (point_offset - vector[j-1])
aprx = aprx + mul
print ("The approximate value of f(",point_offset,") is: %0.6f" %( aprx))
輸出結果
Divided Differences (Newton) 插除法
i= 1 , j= 1
(-5.894830 - -6.000000 ) / (0.100000 - 0.000000) )
matrix[ 1] [ 1] =1.051700
i= 1 , j= 2
(-5.650140 - -5.894830 ) / (0.300000 - 0.100000) )
matrix[ 2] [ 1] =1.223450
i= 1 , j= 3
(-5.177880 - -5.650140 ) / (0.600000 - 0.300000) )
matrix[ 3] [ 1] =1.574200
i= 1 , j= 4
(-4.281720 - -5.177880 ) / (1.000000 - 0.600000) )
matrix[ 4] [ 1] =2.240400
i= 1 , j= 5
(-3.995830 - -4.281720 ) / (1.100000 - 1.000000) )
matrix[ 5] [ 1] =2.858900
i= 2 , j= 2
(1.223450 - 1.051700 ) / (0.300000 - 0.000000) )
matrix[ 2] [ 2] =0.572500
i= 2 , j= 3
(1.574200 - 1.223450 ) / (0.600000 - 0.100000) )
matrix[ 3] [ 2] =0.701500
i= 2 , j= 4
(2.240400 - 1.574200 ) / (1.000000 - 0.300000) )
matrix[ 4] [ 2] =0.951714
i= 2 , j= 5
(2.858900 - 2.240400 ) / (1.100000 - 0.600000) )
matrix[ 5] [ 2] =1.237000
i= 3 , j= 3
(0.701500 - 0.572500 ) / (0.600000 - 0.000000) )
matrix[ 3] [ 3] =0.215000
i= 3 , j= 4
(0.951714 - 0.701500 ) / (1.000000 - 0.100000) )
matrix[ 4] [ 3] =0.278016
i= 3 , j= 5
(1.237000 - 0.951714 ) / (1.100000 - 0.300000) )
matrix[ 5] [ 3] =0.356607
i= 4 , j= 4
(0.278016 - 0.215000 ) / (1.000000 - 0.000000) )
matrix[ 4] [ 4] =0.063016
i= 4 , j= 5
(0.356607 - 0.278016 ) / (1.100000 - 0.100000) )
matrix[ 5] [ 4] =0.078591
i= 5 , j= 5
(0.078591 - 0.063016 ) / (1.100000 - 0.000000) )
matrix[ 5] [ 5] =0.014159
[-6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[-5.89483, 1.051700000000002, 0.0, 0.0, 0.0, 0.0, 0.0]
[-5.65014, 1.223449999999997, 0.5724999999999832, 0.0, 0.0, 0.0, 0.0]
[-5.17788, 1.5742000000000012, 0.7015000000000082, 0.21500000000004166, 0.0, 0.0, 0.0]
[-4.28172, 2.2404, 0.9517142857142844, 0.2780158730158624, 0.06301587301582073, 0.0, 0.0]
[-3.99583, 2.858899999999995, 1.2369999999999892, 0.356607142857131, 0.07859126984126863, 0.014159451659498086, 0.0]
matrix[ 0][ 5] = -6.000000
matrix[ 1][ 5] = 1.051700
matrix[ 2][ 1] = 0.572500
matrix[ 3][ 2] = 0.215000
matrix[ 4][ 3] = 0.063016
matrix[ 5][ 4] = 0.014159
The approximate value of f( 0.2 ) is: -5.778599
>>>
沒有留言:
張貼留言