範例EX4-8 梯行積分法 雙重積分計算 C++ 與 python
Trapezoidal Rule for Approximate Value of Definite Integral
In the field of numerical analysis, Trapezoidal rule is used to find the approximation of a definite integral. The basic idea in Trapezoidal rule is to assume the region under the graph of the given function to be a trapezoid and calculate its area.
It follows that:
It follows that:
For more accurate results the domain of the graph is divided into n segments of equal size as shown below:
Grid spacing or segment size h = (b-a) / n.
Therefore, approximate value of the integral can be given by:
Therefore, approximate value of the integral can be given by:
// C++ program to implement Trapezoidal rule
#include<stdio.h>
#include<math.h>
// A sample function whose definite integral's
// approximate value is computed using Trapezoidal
// rule
float y(float x)
{
// Declaring the function f(x) = 2x-4x^3-2x^5
return ( (2*x)-(4*pow(x,3))+(2*pow(x,5)) );
}
// Function to evalute the value of integral
float trapezoidal(float a, float b, float n)
{
// Grid spacing
float h = (b-a)/n;
// Computing sum of first and last terms
// in above formula
float s = y(a)+y(b);
// Adding middle terms in above formula
for (int i = 1; i < n; i++)
s += 2*y(a+i*h);
// h/2 indicates (b-a)/2n. Multiplying h/2
// with s.
return (h/2)*s;
}
// Driver program to test above function
int main()
{
// Range of definite integral
float x0 = 0;
float xn = 1;
// Number of grids. Higher value means
// more accuracy
int n = 10;
printf("Value of integral is %6.4f\n",
trapezoidal(x0, xn, n));
return 0;
}
輸出結果
Value of integral is 0.3316
==========
python 語言
==========
# Python3 code to implement Trapezoidal rule
# A sample function whose definite
# integral's approximate value is
# computed using Trapezoidal rule
def y( x ):
# Declaring the function
# f(x) = return ( (2*x)-(4*pow(x,3))+(2*pow(x,5)) )
return ( (2*x)-(4*pow(x,3))+(2*pow(x,5)) )
# Function to evalute the value of integral
def trapezoidal (a, b, n):
# Grid spacing
h = (b - a) / n
# Computing sum of first and last terms
# in above formula
s = (y(a) + y(b))
# Adding middle terms in above formula
i = 1
while i < n:
s += 2 * y(a + i * h)
i += 1
# h/2 indicates (b-a)/2n.
# Multiplying h/2 with s.
return ((h / 2) * s)
# Driver code to test above function
# Range of definite integral
x0 = 0
xn = 1
# Number of grids. Higher value means
# more accuracy
n = 10
print ("Value of integral is ",
"%.4f"%trapezoidal(x0, xn, n))
# This code is contributed by "Sharad_Bhardwaj".
==========
python 語言
==========
# A sample function whose definite
# integral's approximate value is
# computed using Trapezoidal rule
def y( x ):
# Declaring the function
# f(x) = return ( (2*x)-(4*pow(x,3))+(2*pow(x,5)) )
return ( (2*x)-(4*pow(x,3))+(2*pow(x,5)) )
# Function to evalute the value of integral
def trapezoidal (a, b, n):
# Grid spacing
h = (b - a) / n
# Computing sum of first and last terms
# in above formula
s = (y(a) + y(b))
# Adding middle terms in above formula
i = 1
while i < n:
s += 2 * y(a + i * h)
i += 1
# h/2 indicates (b-a)/2n.
# Multiplying h/2 with s.
return ((h / 2) * s)
# Driver code to test above function
# Range of definite integral
x0 = 0
xn = 1
# Number of grids. Higher value means
# more accuracy
n = 10
print ("Value of integral is ",
"%.4f"%trapezoidal(x0, xn, n))
# This code is contributed by "Sharad_Bhardwaj".
輸出結果
Value of integral is 0.3316
沒有留言:
張貼留言