2019年2月28日 星期四

Julia語言 習題3-6請選擇適當的微分近似法完成下表:

Julia語言 習題3-6請選擇適當的微分近似法完成下表:
x  =    0  ,2   ,3      ,5
f(x)=  1  ,7   , 25   ,121 
f'(x) = ?   ?     ?     ? 


程式
using Printf

x=[ 0 , 2 , 3 , 5]
f= [  [1.0    ,0.0 , 0.0   ,0.0  ,0.0 ,0.0 ],
      [7.0    ,0.0 , 0.0   ,0.0  ,0.0 ,0.0 ],
      [25.0   ,0.0 , 0.0   ,0.0  ,0.0 ,0.0 ],
      [121.0  ,0.0 , 0.0   ,0.0  ,0.0 ,0.0 ] ]

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-1]
for i=1:n-1
    d[i]=f[1][i+1]
end 

s=@sprintf("\n牛頓一階微分前向差除表")
println(s,d)

xa=[ 0.0 , 2.0 , 3.0 , 5.0]
pn=[0.0 , 0.0 , 0.0 ,0.0]
s=@sprintf("\nTHE RESULTS OF INTERPOLATION:\n")
print(s)
for i=1:length(xa)
    pn[i]=d[1]
    println("========================================================")
    s=@sprintf("xa[%1d]= %0.3f , P'n( %0.3f ) = %0.5f",i,xa[i],xa[i],pn[i] )
    println(s)
end


s=@sprintf("\nTHE RESULTS OF INTERPOLATION:\n")
print(s)
for i=1:length(xa)
    xb=xa[i]
    dx=0.0
    for j=1:2
        #println(xb,"  ", x[j])
        dx=dx+(xb-x[j])   
    end
    #println(dx)
    dx=d[2]*dx
    pn[i]=pn[i]+dx
    println("========================================================")
    s=@sprintf("xa[%1d]= %0.3f , P'n( %0.3f ) = %0.5f",i,xa[i],xa[i],pn[i] )
    println(s)
end


s=@sprintf("\nTHE RESULTS OF INTERPOLATION:\n")
print(s)
for i=1:length(xa)
    xb=xa[i]
    dx=0.0
    dx=(xb-x[1])*(xb-x[2])+ (xb-x[2])*(xb-x[3])+ (xb-x[3])*(xb-x[1])
    dx=d[3]*dx
    pn[i]=pn[i]+dx
    println("========================================================")
    s=@sprintf("xa[%1d]= %0.3f , P'n( %0.3f ) = %0.5f",i,xa[i],xa[i],pn[i] )
    println(s)
end



輸出畫面
Divided Difference Table: 
=============================
i x(i)  f(i)  f(i,i+1) f(i,i+1.i+2),  ......................
1  0.00000  1.00000  3.00000  5.00000  1.00000
2  2.00000  7.00000 18.00000 10.00000
3  3.00000 25.00000 48.00000
4  5.00000 121.00000

牛頓一階微分前向差除表[3.0, 5.0, 1.0]

THE RESULTS OF INTERPOLATION:
========================================================
xa[1]= 0.000 , P'n( 0.000 ) = 3.00000
========================================================
xa[2]= 2.000 , P'n( 2.000 ) = 3.00000
========================================================
xa[3]= 3.000 , P'n( 3.000 ) = 3.00000
========================================================
xa[4]= 5.000 , P'n( 5.000 ) = 3.00000

THE RESULTS OF INTERPOLATION:
========================================================
xa[1]= 0.000 , P'n( 0.000 ) = -7.00000
========================================================
xa[2]= 2.000 , P'n( 2.000 ) = 13.00000
========================================================
xa[3]= 3.000 , P'n( 3.000 ) = 23.00000
========================================================
xa[4]= 5.000 , P'n( 5.000 ) = 43.00000

THE RESULTS OF INTERPOLATION:
========================================================
xa[1]= 0.000 , P'n( 0.000 ) = -1.00000
========================================================
xa[2]= 2.000 , P'n( 2.000 ) = 11.00000
========================================================
xa[3]= 3.000 , P'n( 3.000 ) = 26.00000
========================================================
xa[4]= 5.000 , P'n( 5.000 ) = 74.00000


沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...