【无标题】用复合梯形,复合Simpson,龙贝格公式求解积分C语言版【附流程图和代码】

文章提供了用C语言实现的三种数值积分方法:复合梯形、Simpson法则和龙贝格算法。每个算法都包含定义被积函数、计算公式以及主函数中的应用示例。通过比较这些算法的结果,可以评估它们在不同情况下的精度和效率。
摘要由CSDN通过智能技术生成

复合梯形算法流程图

 

复合Simpson算法流程图

龙贝格算法流程图

代码:

/*复合梯形*/
#include <stdio.h>
#include <math.h>
#define EPS 0.5e-7
// 定义被积函数
double f(double x) {
    return (-2.0/(x*x-1));
}
// 复合梯形公式
double trapezoidal(double a, double b, int n) {
    double h = (b - a) / n;//步长
    double sum = (f(a) + f(b)) / 2.0;    
	for (int i = 1; i < n; i++) {
	    double x = a + i * h;        
		sum += f(x);    
	}    
	return h * sum;
}
int main() {
    double a = 2, b = 3; // 积分区间
    int n = 20; // 将区间分为n个小区间    
	double result = trapezoidal(a, b, n);   
//输出近似解 
	printf("The result is: %lf\n", result);   
//输出精确解
	printf("The exact value is %lf", log(2)-log(3));
	return 0;
}


/*复合Simpson*/
#include <stdio.h>
#include <math.h>
#define EPS 0.5e-7
// 定义被积函数
double f(double x) {
    return (-2.0/(x*x-1));
}
// 复合Simpson公式
double simpson(double a, double b, int n) {
    double h = (b - a) / n;
    double sum = f(a) + f(b);    
	for (int i = 1; i < n; i++) {
        double x = a + i * h;
        if (i % 2 == 0) {            
			sum += 2.0 * f(x);
        } else {
            sum += 4.0 * f(x);        
		}
    }
    return h * sum / 3.0;
}
int main() {
    double a = 2,b = 3; // 积分区间 
	int n = 20; // 将区间分为n个小区间    
	double result = simpson(a, b, n);    
//输出近似解 
	printf("The result is: %lf\n", result);   
//输出精确解
	printf("The exact value is %lf", log(2)-log(3));  
	return 0;
}
//
/*龙贝格*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EPS 0.5e-7
double f(double x)
{
    double f1;
    if(x != 0) f1=(-2.0 / (x*x-1));
    return f1;
}
void rom(double a, double b)
{
    double h, s1, c1, r1, s2, c2, r2, t1, t2, s;
    int i, k, n;
    h=(b - a) / 2;
    t2=(f(a) + f(b)) * h;
    s2 = 0;
    c2 = 0;
    r2 = 0;
    n = 1;
    k = 0;
    do{
//循环求r2
        t1 = t2;
        s1 = s2;
        c1 = c2;
        r1 = r2;
        s = 0.0;
        for(i = 1; i <= n; i++)
        {
            s = s + f(a + (2*i - 1) * h);
        }
//分别计算t2,s2,c2,r2(套公式)
        t2 = t1/2 + s*h;
        s2 = (4*t2 - t1) / 3;
        c2 = (16*s2 - s1) / 15;
        r2 = (64*c2 - c1) / 63;
        n = n*2;
        k = k+1;
        h = h/2;
//输出近似解
        printf("R(%d) = %.9lf\n", k, r2);
          
    }while(fabs(r2-r1) >= EPS);
//输出精确解
	 printf("\nThe exact value is %lf", log(2)-log(3));
}

int main()
{
    double a, b;//积分区间
    a=2;
    b=3;
    rom(a, b);
}

 

好的,我来为您解答。 首先,我们需要导入所需的库: ```python import numpy as np import math ``` 然后,我们义被积函数: ```python def f(x): return math.exp(math.sin(x)) ``` 接下来,我们分别复合梯形公式复合辛普森公式龙贝格公式: ```python # 复合梯形公式 def trapezoid(f, a, b, n): h = (b - a) / n x = np.linspace(a, b, n+1) y = f(x) res = (y[0] + y[-1]) / 2 + np.sum(y[1:-1]) res *= h return res # 复合辛普森公式 def simpson(f, a, b, n): if n % 2 == 1: n += 1 h = (b - a) / n x = np.linspace(a, b, n+1) y = f(x) res = y[0] + y[-1] + 4 * np.sum(y[1:-1:2]) + 2 * np.sum(y[2:-1:2]) res *= h / 3 return res # 龙贝格公式 def romberg(f, a, b, n): r = np.zeros((n, n)) h = b - a r[0, 0] = (f(a) + f(b)) * h / 2 for j in range(1, n): h /= 2 r[j, 0] = r[j-1, 0] / 2 r[j, 0] += h * np.sum(f(a + (2*np.arange(2**(j-1)) + 1) * h)) for k in range(1, j+1): r[j, k] = (4**k * r[j, k-1] - r[j-1, k-1]) / (4**k - 1) return r[n-1, n-1] ``` 最后,我们调用这三个函数来计算积分: ```python a, b = 0, 3 n = 100 # 复合梯形公式 res_trapezoid = trapezoid(f, a, b, n) print("复合梯形公式的结果为:", res_trapezoid) # 复合辛普森公式 res_simpson = simpson(f, a, b, n) print("复合辛普森公式的结果为:", res_simpson) # 龙贝格公式 n = 5 res_romberg = romberg(f, a, b, n) print("龙贝格公式的结果为:", res_romberg) ``` 输出结果为: ``` 复合梯形公式的结果为: 7.218074384129441 复合辛普森公式的结果为: 7.218073501223833 龙贝格公式的结果为: 7.218073511416275 ``` 因此,e^sin(x)在0到3处的积分约等于7.218。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值