2019年3月1日 星期五

Julia語言 例題EX4-2 請用梯形法求統計學常態分布的面積

Julia語言 例題EX4-2 請用梯形法求統計學常態分布的面積



a=-3.5  b=3.0 n=10 的積分面積


程式
#================================================
 * ex4-2.jl based on Trapezoidol Rule is
 * used for computing definite integral with
 * domain [a,b] with n even-grids.
 *
   梯形法 T'= (h/2) [ f(a) + f(b) + 2 sigma i=1 to n-1 f(xi)]

=================================================#

using Printf

function f(x::Float64) #// 欲微分函數
    return  return (1.0/exp(x*x/2))
end

result=0.0
n=10
a=-3.5
b=-3.0

h=(b-a)/n
x=a
for i=1:n-1
    x=x+h
    s=@sprintf("i=%2d , x=%0.2f ---- f(x)=%0.4f",i,x,f(abs(x)))
    println(s)
    result=result+f(abs(x))
end

tn=(1.0/sqrt(2* pi ))*(h/2.0)*(f(abs(a))+f(abs(b))+2.0*result)

println("\nf(x)=(1.0/exp(x*x/2)) a=0.0 ,b=1.0 n=10 ")
s=@sprintf("T%d=%0.7lf\n",n,tn)
println(s)


ET1= (b^2 -1)* exp(- (b^2)/2)
#println(ET1)
ET= (ET1 * (b-a)^3) / (12*n^2)
s=@sprintf("梯形法的誤差範圍= %0.7f",ET)
println(s)


輸出畫面
i= 1 , x=-3.45 ---- f(x)=0.0026
i= 2 , x=-3.40 ---- f(x)=0.0031
i= 3 , x=-3.35 ---- f(x)=0.0037
i= 4 , x=-3.30 ---- f(x)=0.0043
i= 5 , x=-3.25 ---- f(x)=0.0051
i= 6 , x=-3.20 ---- f(x)=0.0060
i= 7 , x=-3.15 ---- f(x)=0.0070
i= 8 , x=-3.10 ---- f(x)=0.0082
i= 9 , x=-3.05 ---- f(x)=0.0095

f(x)=(1.0/exp(x*x/2)) a=0.0 ,b=1.0 n=10 
T10=0.0011194

梯形法的誤差範圍= 0.0000093

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...