C语言求积分的近似值

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43959027/article/details/89713068
/**
用梯形法求积分的近似值
**/
#include"stdio.h"
#include"math.h"
float seekIntegral(float a, float b, int n)
{
	float f1 = 0, f2 = 0;
	float s = 0;
	float deta, x;
	x = a;
	deta = (b - a) / n;
	f1 = sin(x) + x;
	for (int i = 0; i < n; i++)
	{
		x += deta;
		f2 =  sin(x) + x;
		s += (f2 + f1) * deta / 2;
		f1 = f2;
	}
	return s;
}
void main()
{
	float s1,s2;
	s1 = seekIntegral(0, 10, 60);
	s2 = seekIntegral(0, 10, 60000);
	printf("%.6f\n", s1);
	printf("%.6f\n", s2);
}

猜你喜欢

转载自blog.csdn.net/qq_43959027/article/details/89713068
今日推荐