http://www.math.ncu.edu.tw/~yu/ecocal98/board.php
2018年12月31日 星期一
利用辛普森 理則 (Simpson's Rule) #計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積 --Python語言
# 利用辛普森 理則 (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" % (n,sn))
===============================
輸出結果======== RESTART: H:/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}
>>>
====================
辛普森理則的python另一寫法
====================
# Python code for simpson's 1 / 3 rule
import math
# Function to calculate f(x)
def func( x ):
return (math.pi* math.pow((1+math.pow(x,3)),(2/3)))
# Function for approximate integral
def simpsons_( ll, ul, n ):
# Calculating the value of h
h = ( ul - ll )/n
# List for storing value of x and f(x)
x = list()
fx = list()
# Calcuting values of x and f(x)
i = 0
while i<= n:
x.append(ll + i * h)
fx.append(func(x[i]))
i += 1
# Calculating result
res = 0
i = 0
while i<= n:
if i == 0 or i == n:
res+= fx[i]
elif i % 2 != 0:
res+= 4 * fx[i]
else:
res+= 2 * fx[i]
i+= 1
res = res * (h / 3)
return res
# Driver code
lower_limit = 0.0 # Lower limit
upper_limit = 2.0 # Upper limit
n = 10 # Number of interval
print("%.6f"% simpsons_(lower_limit, upper_limit, n))
12.325078
>>>
利用辛普森 理則 (Simpson's Rule) 計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積
利用辛普森 理則 (Simpson's Rule) 計算
x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積
/* ex4-5.c based on Simpson's Rule to compute
* definite integral with domain [a,b] and
* n even-grid. n must be even.
*/
#include <stdio.h>
#include <math.h>
#define F(x) pow(pow((1+pow(x,3)),1.0/3),2)
#define PI 3.14159
void main()
{
int i,m,n;
double a,b,h,x,sum1=0.0,sum2=0.0,sn;
scanf("a=%lf b=%lf n=%d",&a,&b,&n);
m=n/2;
h=(b-a)/n;
for(i=1;i<=2*m-1;i++)
{
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);
printf("S%d=%lf\n",n,sn);
return;
}
輸入資料
a=0 b=2 n=10
輸出資料
S10=12.325068
x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積
/* ex4-5.c based on Simpson's Rule to compute
* definite integral with domain [a,b] and
* n even-grid. n must be even.
*/
#include <stdio.h>
#include <math.h>
#define F(x) pow(pow((1+pow(x,3)),1.0/3),2)
#define PI 3.14159
void main()
{
int i,m,n;
double a,b,h,x,sum1=0.0,sum2=0.0,sn;
scanf("a=%lf b=%lf n=%d",&a,&b,&n);
m=n/2;
h=(b-a)/n;
for(i=1;i<=2*m-1;i++)
{
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);
printf("S%d=%lf\n",n,sn);
return;
}
輸入資料
a=0 b=2 n=10
輸出資料
S10=12.325068
範例EX4-1 利用梯行積分法求方程式近似解---Python語言
範例EX4-1 利用梯行積分法求方程式近似解---Python語言
# 利用梯行積分法求方程式近似解
from math import *
def F(x):
temp=(exp(x))
return temp
n=10
a=0.0
b=1.0
sum1=0.0
h=(b-a)/n
x=a
for i in range (1,n):
x=x+h
sum1=sum1+F(abs(x))
tn=(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum1)
print("T{%d}={%10.6f}\n" %(n,tn))
輸出畫面
======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX4-1.py ==============
T{10}={ 1.719713}
>>>
# 利用梯行積分法求方程式近似解
from math import *
def F(x):
temp=(exp(x))
return temp
n=10
a=0.0
b=1.0
sum1=0.0
h=(b-a)/n
x=a
for i in range (1,n):
x=x+h
sum1=sum1+F(abs(x))
tn=(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum1)
print("T{%d}={%10.6f}\n" %(n,tn))
======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX4-1.py ==============
T{10}={ 1.719713}
>>>
範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值---python語言
範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值---python語言
# 利用梯行積分法求方程式近似解
from math import *
def F(x):
temp=(1.0 /exp(x*x/2))
return temp
n=100
a=-5.0
b=5.0
sum1=0.0
h=(b-a)/n
x=a
for i in range (1,n):
x=x+h
sum1=sum1+F(abs(x))
tn=(1.0/sqrt(2*pi))*(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum1)
print("T{%d}={%10.6f}\n" %(n,tn))
======== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX4-3.py ===========
T{100}={ 0.999999}
>>>
# 利用梯行積分法求方程式近似解
from math import *
def F(x):
temp=(1.0 /exp(x*x/2))
return temp
n=100
a=-5.0
b=5.0
sum1=0.0
h=(b-a)/n
x=a
for i in range (1,n):
x=x+h
sum1=sum1+F(abs(x))
tn=(1.0/sqrt(2*pi))*(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum1)
print("T{%d}={%10.6f}\n" %(n,tn))
輸出畫面
======== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX4-3.py ===========
T{100}={ 0.999999}
>>>
利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.0 to 5.0 的積分值
範例 EX4-3 梯行法求 (exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5 to 5
/* ex4-3.c based on Trapezoidol Rule is
* used for computing definite integral with
* domain [a,b] with n even-grids.
*/
#include <stdio.h>
#include <math.h>
#define PI 3.141596
#define F(x) (1.0/exp(x*x/2))
void main()
{
int n,i;
double a,b,x,tn,h ,sum=0.0;
scanf("n=%d a=%lf b=%lf",&n,&a,&b);
h=(b-a)/n;
x=a;
for(i=1;i<=n-1;i++)
{
x=x+h;
sum=sum+F(fabs(x));
}
tn=(1.0/sqrt(2*PI))*(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum);
printf("T%d=%10.6lf\n",n,tn);
return;
}
輸入資料
n=100 a=-5.0 b=5.0
輸出資料
T100= 0.999999
範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值
範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值
#include <stdio.h>
#include <math.h>
/* 示範梯形數值積分法 (trapeziodal.c) */
#define STEP 10
double f(double);
main()
{
int n, k, i;
double h, T, a, b, x;
T = 0;
a = -3.5;
b = -3.0;
for (n=1, i=0; i < STEP; ++i) {
n = 2*n;
h = (b-a)/n;
x = a;
T = f(x)/2;
for (k=1; k<n ; ++k) {
x = x + h;
T = T + f(x);
}
T = T + f(b)/2;
T = h*T;
printf("T_%-5d = %17.14f.\n", n, T);
}
}
double f(double x) {
return ( (exp(-pow(x,2)/2)) / sqrt(2*3.14159));
}
T_8 = 0.00112060204348.
T_16 = 0.00111810278396.
T_32 = 0.00111747777712.
T_64 = 0.00111732151342.
T_128 = 0.00111728244675.
T_256 = 0.00111727268003.
T_512 = 0.00111727023835.
T_1024 = 0.00111726962793.
...Program finished with exit code 0
Press ENTER to exit console.
訂閱:
文章 (Atom)
清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...
-
Line 發報機 Python TKinter (CONFIG_LINE Message API_2.py) import serial import serial.tools.list_ports import tkinter as tk from tkinter import...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...















