2019年1月21日 星期一

例題4-5 # 利用辛普森 理則 (Simpson's Rule) #計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積

例題4-5
# 利用辛普森 理則 (Simpson's Rule)
#計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積

from math import *

def F(x):
    temp=pow(pow((1+pow(x,3)),1.0/3),2)
    return temp

a=0.0
b=2.0
n=10
sum1=0.0
sum2=0.0
sn=0.0

m=int(n/2)

h=(b-a)/n
for i in range (1,2*m):
    x=a+i*h
    if(i%2==0):
        sum2=sum2+F(x)
    else:
        sum1=sum1+F(x)
    sn= pi *(h/3.0)*(F(a)+F(b)+2.0*sum2+4.0*sum1)
    print("S{%d}={%f}" % (n,sn))


輸出畫面
======== RESTART: F:\2018-09勤益科大數值分析\數值分析\PYTHON\EX4-5.py ===========
S{10}={1.957852}
S{10}={2.394418}
S{10}={3.348841}
S{10}={3.900651}
S{10}={5.230509}
S{10}={6.048317}
S{10}={8.068276}
S{10}={9.308714}
S{10}={12.325078}
>>> 

沒有留言:

張貼留言

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

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