2019年2月1日 星期五

例題1-10 牛頓向後差除法

例題1-10 牛頓向後差除法

已知4點座標
n=4
x       f(x)
==========
1.0    0.0
2.0    0.693
3.0    1.099
4.0    1.386
求牛頓向後差除表

'''
EX1-10-1.py
Code for NEWTON'S BACKWARD DIFFERENCE METHOD in Python Programming
n=4
x       f(x)
==========
1.0    0.0
2.0    0.693
3.0    1.099
4.0    1.386
'''
import math

def fact(a):
    fac = 1.0
    if (a == 0):
       return (1.0)
    else:
       fac = a * fact(a-1)

    return(fac)

#++++++++++++++++++++++++++++++
n=4
x= [0.0 for i in range(0,n)]     #x [n] 矩陣
y=  [ [0.0 for i in range(n)]  for j in range(n) ]       #[n] * [n] 矩陣
x=[ 1.0 , 2.0 , 3.0 ,4.0]
y=[[ 0.0 , 0.693 , 1.099 , 1.386],
      [0.0 , 0.0 , 0.0, 0.0 ],
      [0.0 , 0.0 , 0.0, 0.0],
      [0.0 , 0.0 , 0.0, 0.0],
      [0.0  , 0.0, 0.0, 0.0] ]
 
print("\nhow many record you will be enter: ",n)
print("\n\nenter the value of x%d: ")
for i  in range (len(x)) :   
    print(  '\t',round(x[i],4))
print()
k=0
print("\n\nenter the value of f(x): ")
for i  in range (len(x)) :   
    print(  '\t',i,"\t\t",round( y[k][i],4))
print()

p=1.5
print("\n\nEnter X for finding f(x): ",p)
for i in range(1,n):
    #print(i,'\t',end='')
    for j in range (i , n):
        #print(j,'\t',end='')
        y[i][j]= (y[i-1][j]-y[i-1][j-1])  / (x[j]-x[j-i]);
        print(round( y[i][j],4),end=',')

print("\n_____________________________________________________\n")
print("\n  x(i)\t   y(i)    y1(i)    y2(i)    y3(i)    y4(i)")
print("\n_____________________________________________________\n")
for i in range (0 , n):
    print("\n {%.4f}" %(x[i]),end='')
    for j in range(0 , i+1):
        print("   ",end='')
        print(" {%.4f}" %(y[j][i]),end='')
    print("\n");





======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/Ex1-10-1.py ==========

how many record you will be enter:  4


enter the value of x%d: 
1.0
2.0
3.0
4.0



enter the value of f(x): 
0 0.0
1 0.693
2 1.099
3 1.386



Enter X for finding f(x):  1.5
0.693,0.406,0.287,-0.1435,-0.0595,0.028,
_____________________________________________________


  x(i)    y(i)    y1(i)    y2(i)    y3(i)    y4(i)

_____________________________________________________


 {1.0000}    {0.0000}


 {2.0000}    {0.6930}    {0.6930}


 {3.0000}    {1.0990}    {0.4060}    {-0.1435}


 {4.0000}    {1.3860}    {0.2870}    {-0.0595}    {0.0280}

>>> 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...