通过黎曼和解定积分既是把y曲线下方面积划分成无数矩形,矩形数量越多,得出的面积越精确
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float function1(float);
float function2(float);
float function3(float);
void integration(float f(float),float,float); //求定积分公式
integration(function1,1,0);
integration(function2,1,-1);
integration(function3,2,0);
}
void integration(float f(float),float a,float b) //f(float),f(x)。float a,float b,区间两点
{
float x,t2=0; //t2,矩形总面积
float n=1000; //n值越大,精确值越高
float h; //矩形宽度
float t1=0; //单个矩形面积
h=(a-b)/n; //矩形宽度为面积总长度除以矩形个数
for(float i=1;i<=1000;i++) //计算每个矩形的面积
{
x=b+h*i; //通过i的变化,得出每个矩形底部x的值
t1=f(x)*h; //用x做实参调用函数进一步求出y值,既矩形的高度,底乘高得出面积
t2=t2+t1; //各个矩形面积相加
}
printf("the value of function is %f",t2);
}
float function1(float x)
{
float y;
y=sin(x);
return y;
}
float function2(float x)
{
float y;
y=cos(x);
return y;
}
float function3(float x)
{
float y;
y=exp(x);
return y;
}