在数值分析中,辛普森的1/3规则是定积分数值近似的一种方法。具体来说,它是以下近似值:
在辛普森(Simpson)的1/3规则中,我们使用抛物线来近似曲线的每个部分。
该区域分成宽度相等的Δx的n个段。
辛普森法则可以通过近似被积数f(x)(蓝色)来推导
由二次插值P(x)(红色)表示。
为了对间隔(a,b)中的任何函数f(x)进行积分,请遵循以下步骤:
1.为n选择一个值,该值是间隔分为的部分数。
2.计算宽度,h =(ba)/ n
3.计算x0到xn的值,因为x0 = a,x1 = x0 + h,….. xn-1 = xn-2 + h,xn = b。
考虑y = f(x)。现在为对应的x(x0到xn)值找到y(y0到yn)的值。
4.将上述所有发现的值替换为Simpson的“规则公式”以计算积分值。
积分的近似值可以通过辛普森定律给出:
注意:在此规则中,n必须为偶数。
应用 :
当很难用数学方法求解给定的积分时使用它。
该规则很容易给出近似值,而无需实际知道积分规则。
例子 :
Evaluate logx dx within limit 4 to 5.2.
First we will divide interval into six equal
parts as number of interval should be even.
x : 4 4.2 4.4 4.6 4.8 5.0 5.2
logx : 1.38 1.43 1.48 1.52 1.56 1.60 1.64
Now we can calculate approximate value of integral
using above formula:
= h/3[( 1.38 + 1.64) + 4 * (1.43 + 1.52 +
1.60 ) +2 *(1.48 + 1.56)]
= 1.84
Hence the approximation of above integral is
1.827 using Simpson's 1/3 rule.
C++
// CPP program for simpson's 1/3 rule
#include
#include
using namespace std;
// Function to calculate f(x)
float func(float x)
{
return log(x);
}
// Function for approximate integral
float simpsons_(float ll, float ul, int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x and f(x)
float x[10], fx[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver program
int main()
{
float lower_limit = 4; // Lower limit
float upper_limit = 5.2; // Upper limit
int n = 6; // Number of interval
cout << simpsons_(lower_limit, upper_limit, n);
return 0;
}
Java
// Java program for simpson's 1/3 rule
public class GfG{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void main(String s[])
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
System.out.println(simpsons_(lower_limit,
upper_limit, n));
}
}
// This code is contributed by Gitanjali
Python3
# Python code for simpson's 1 / 3 rule
import math
# Function to calculate f(x)
def func( x ):
return math.log(x)
# 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 = 4 # Lower limit
upper_limit = 5.2 # Upper limit
n = 6 # Number of interval
print("%.6f"% simpsons_(lower_limit, upper_limit, n))
C#
// C# program for simpson's 1/3 rule
using System;
public class GfG
{
// Function to calculate f(x)
static float func(float x)
{
return (float)Math.Log(x);
}
// Function for approximate integral
static float simpsons_(float ll, float ul,
int n)
{
// Calculating the value of h
float h = (ul - ll) / n;
// Array for storing value of x
// and f(x)
float[] x = new float[10];
float[] fx= new float[10];
// Calculating values of x and f(x)
for (int i = 0; i <= n; i++) {
x[i] = ll + i * h;
fx[i] = func(x[i]);
}
// Calculating result
float res = 0;
for (int i = 0; i <= n; i++) {
if (i == 0 || i == n)
res += fx[i];
else if (i % 2 != 0)
res += 4 * fx[i];
else
res += 2 * fx[i];
}
res = res * (h / 3);
return res;
}
// Driver Code
public static void Main()
{
// Lower limit
float lower_limit = 4;
// Upper limit
float upper_limit = (float)5.2;
// Number of interval
int n = 6;
Console.WriteLine(simpsons_(lower_limit,
upper_limit, n));
}
}
// This code is contributed by vt_m
PHP
Javascript
输出:
1.827847