2019年2月25日 星期一

Julia語言 利用3點微分方式求f'(x)

Julia語言 利用3點微分方式求f'(x)
     3點向前
    fd1=((-f[i+2]+ 4*f[i+1]-3*f[i])/ (2*h))
     3點向後
    fd2=((3*f[m] -4*f[m-1]+ f[m-2]) / (2*h))

    x     f(x)              f'(x)
======================
-0.3   -0.20431       ???
-0.1   -0.08993       ???
0.1     0.11007       ???
0.3     0.39569       ??? 
======================

using Printf

#=====================================================
Trigonometric and hyperbolic functions
All the standard trigonometric and hyperbolic functions are also defined:

sin    cos    tan    cot    sec    csc
sinh   cosh   tanh   coth   sech   csch
asin   acos   atan   acot   asec   acsc
asinh  acosh  atanh  acoth  asech  acsch
sinc   cosc   atan2
=====================================================#

x=[-0.3 , -0.1, 0.1 , 0.3 ]
f=[-0.20431 , -0.08993 , 0.11007 , 0.39569]

println("3點向前差")
for i=1: (length(x)-2)
    h= abs(x[i+1]-x[i])
    fd1=((-f[i+2]+ 4*f[i+1]-3*f[i])/ (2*h))
    s=@sprintf("3點向前差 Three-point fordward Diff : fd(%0.5lf) =%0.5lf \n",x[i], fd1)
    println(s)
end   

println("3點向後差")
for m=length(x):-1:3
    h= abs(x[m]-x[m-1])
    fd2=((3*f[m] -4*f[m-1]+ f[m-2]) / (2*h))
    s=@sprintf("3點向後差 Three-point bachfoward Diff : fd(%0.5lf) =%0.5lf \n",x[m], fd2)
    println(s)
end   


輸出畫面
3點向前差
3點向前差 Three-point fordward Diff : fd(-0.30000) =0.35785 

3點向前差 Three-point fordward Diff : fd(-0.10000) =0.78595 

3點向後差
3點向後差 Three-point bachfoward Diff : fd(0.30000) =1.64215 

3點向後差 Three-point bachfoward Diff : fd(0.10000) =1.21405 

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

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