2019年2月1日 星期五

例題1-7 牛頓的多項式內插法 已知3點座標 求 x=0.5 f(x)= ??

例題1-7 牛頓的多項式內插法

已知3點座標
  x                 f(x)
================
 0                  0
 1                  -3
 2                  0
求 x=0.5   f(x)= ??




#/****** Newton Divided Difference  interpolation in Python  ************/
import math

n=3

x= [0.0 for i in range(n+1)]     #x [n] 矩陣
y= [0.0 for i in range(n+1)]     #y [n] 矩陣
p =[0.0 for i in range(n+1)]     #pf [n] 矩陣
x=[0.0 , 0.0 , 1.0 , 2.0 ]
y=[0.0 , 0.0 , -3.0 , 0.0 ]

k=0.5
print("\nEnter the number of observations:",n )
print("\nEnter the different values of x:\n");
for i  in range (1,n+1) :     # 
    print( round( x[i],4),"\t",end='')
print()

print("\nThe corresponding values of y are:\n");
for i  in range (1,n+1) :     # 
    print( round(y[i],4),"\t",end='')
print()

print("\nEnter the value of 'k' in f(k) you want to evaluate:" , k)

j=1
f1=1
f2=0;
f=y[1]

while(n !=1):
    for i in range (1,n):
        p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]))
        y[i]=p[i]

    f1=1;
    for i  in range (1 , j+1):
        f1*=(k-x[i])

    f2+=(y[1]*f1)
    n=n-1
    j=j+1


f+=f2

print("\nf {%.2f} = (%.6f) "  %(k , f))


輸出畫面
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX1-7.py =============

Enter the number of observations: 3

Enter the different values of x:

0.0 1.0 2.0

The corresponding values of y are:

0.0 -3.0 0.0

Enter the value of 'k' in f(k) you want to evaluate: 0.5

f {0.50} = (-2.250000) 
>>> 



沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...