#計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積
#========================================================
/* ex4-5.jl based on Simpson's Rule to compute
* definite integral with domain [a,b] and
* n even-grid. n must be even.
*/
========================================================#
using Printf
function F(x::Float64) #// 欲微分函數
return (( (1+x^3)^ (1/3) )^2)
end
a=0.0
b=2.0
n=10
m=n/2
h=(b-a)/n
sum1=0.0
sum2=0.0
for i=1:2*m-1
x=a+i*h;
if(i%2==0)
sum2=sum2+F(x)
s=@sprintf("i=%2d ,x=%0.3f --- F(x)=%0.6f",i,x,F(x))
println(s)
else
sum1=sum1+F(x)
s=@sprintf("i=%2d ,x=%0.3f --- F(x)=%0.6f",i,x,F(x))
println(s)
end
end
sn= pi *(h/3.0)*(F(a)+F(b)+2.0*sum2+4.0*sum1)
print("\n\n辛普森積分法 ")
s=@sprintf("S%d=%lf\n",n,sn)
println(s)
輸出畫面
i= 1 ,x=0.200 --- F(x)=1.005326
i= 2 ,x=0.400 --- F(x)=1.042224
i= 3 ,x=0.600 --- F(x)=1.139259
i= 4 ,x=0.800 --- F(x)=1.317350
i= 5 ,x=1.000 --- F(x)=1.587401
i= 6 ,x=1.200 --- F(x)=1.952374
i= 7 ,x=1.400 --- F(x)=2.411148
i= 8 ,x=1.600 --- F(x)=2.961326
i= 9 ,x=1.800 --- F(x)=3.600520
辛普森積分法 S10=12.325078
沒有留言:
張貼留言