a=-5 , b=5 , n=100
#================================================
* ex4-3.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 (1.0/exp(x*x/2))
end
result=0.0
n=100
a=-5.0
b=5.0
h=(b-a)/n
x=a
for i=1:n
x=x+h;
result=result+f(abs(x))
end
tn=(1.0/sqrt(2* pi ))*(h/2.0)*(f(abs(a))+f(abs(b))+2.0*result)
s=@sprintf("T%d=%10.6lf\n",n,tn)
println(s)
輸出畫面
T100= 1.000000
沒有留言:
張貼留言