2019年2月25日 星期一

Julia語言例題3-1 利用向前差 向後差 中央差 求f(x)=sin(x)的值

Julia語言例題3-1 利用向前差 向後差 中央差 求f(x)=sin(x)的值
h=0.001 , 0.005 , 0.01 , 0.05 , 0.1 ,0.5 


def func( x):    # // 欲微分函數
     return math.sin(x)

def FordDiff( x, h, fx):
    # // 前差微分
    return ( ( fx(x+h) - fx(x) ) / h)

def BackDiff(x,  h,  fx):
    # // 後差微分
    return (( fx(x) - fx(x-h) ) / h)

def MidDiff(x,  h,  fx):
    # // 中差微分
    return (0.5 * ( fx(x+h) - fx(x-h) ) / h)

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
=====================================================#

function func(x::Float64) #// 欲微分函數
    return sin(x)
end

function FordDiff(x::Float64 , h::Float64, fx::typeof(func)) #//前差微分
    a=fx(x+h)
    b=fx(x)
    return ((a-b) / h)
end

function BackDiff(x::Float64 , h::Float64, fx::typeof(func)) #//後差微分
    return ((fx(x) - fx(x-h) ) / h )
end

function MidDiff(x::Float64 , h::Float64, fx::typeof(func)) #//中差微分
    return (0.5 * ( fx(x+h) - fx(x-h) ) / h)
end

#=====================================================#
hx=[0.001 , 0.005 , 0.01 , 0.05 , 0.1 , 0.5]

for m=1:length(hx)
    x=1.0
    h=hx[m]
    println("h=",h,"\n")
    answer = cos(x)  # // 答案
    cal = FordDiff(x, h, func)
    delta = (cal - answer)/answer
    s=@sprintf("FordDiff : %0.5lf, delta = %0.5lf %%\n",cal,delta)
    println(s)
               
    cal = BackDiff(x, h, func)
    delta = (cal - answer)/answer
    s=@sprintf("BackDiff : %0.5lf, delta = %0.5lf %%\n",cal,delta)
    println(s)
               
    cal = MidDiff(x, h, func)
    delta = (cal - answer)/answer
    s=@sprintf("MidDiff : %0.5lf, delta = %0.5lf %%\n",cal,delta)
    println(s)
    println("================================================")
end


輸出畫面
h=0.001

FordDiff : 0.53988, delta = -0.00078 %

BackDiff : 0.54072, delta = 0.00078 %

MidDiff : 0.54030, delta = -0.00000 %

================================================
h=0.005

FordDiff : 0.53820, delta = -0.00390 %

BackDiff : 0.54240, delta = 0.00389 %

MidDiff : 0.54030, delta = -0.00000 %

================================================
h=0.01

FordDiff : 0.53609, delta = -0.00780 %

BackDiff : 0.54450, delta = 0.00777 %

MidDiff : 0.54029, delta = -0.00002 %

================================================
h=0.05

FordDiff : 0.51904, delta = -0.03934 %

BackDiff : 0.56111, delta = 0.03851 %

MidDiff : 0.54008, delta = -0.00042 %

================================================
h=0.1

FordDiff : 0.49736, delta = -0.07947 %

BackDiff : 0.58144, delta = 0.07614 %

MidDiff : 0.53940, delta = -0.00167 %

================================================
h=0.5

FordDiff : 0.31205, delta = -0.42246 %

BackDiff : 0.72409, delta = 0.34016 %

MidDiff : 0.51807, delta = -0.04115 %

================================================


沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...