首页 > 编程知识 正文

c语言求定积分通用函数,简单的c语言求定积分

时间:2023-05-04 11:04:14 阅读:215816 作者:3986

本文实例为大家分享了C语言程序实现黎曼和求定积分,供大家参考,具体内容如下

通过黎曼和解定积分既是把在xy平面中函数曲线与x轴区间区域划分成多个矩形并求它们的面积之和,矩形数量越多,得出的面积越精确。

#include

#include

#include

#include

int main(){

float function1(float); //函数f(x)1

float function2(float); //函数f(x)2

float function3(float); //函数f(x)3

void integration(float f(float),float,float); //求定积分方法,参数为,函数fx,区间[a,b]的两个点

int result_a=integration(function1,1,0);

int result_b=integration(function2,1,-1);

int result_c=integration(function3,2,0);

}

void integration(float f(float),float endPos,float startPos) //求定积分方法,参数为,函数fx,区间[a,b]的两个点

{

float x;

float totalArea=0; //totalArea,所有矩形的总面积

float n=1000; //将函数曲线下方划为n个矩形,n值越大,精确值越高

float width; //单个矩形宽度

float area=0; //单个矩形面积

width=(endPos-startPos)/n; //求单个矩形宽度,既是函数总长度除以矩形数量

for(float i=1;i<=n;i++) //计算每个矩形的面积

{

x=startPos+width*i; //转入到xy平面, 通过i的递增,得出每个矩形底部x的值,以求矩形高度

area=f(x)*width; //用x做实参调用函数进一步求出y值,既矩形的高度,再用底乘高得出面积

totalArea=totalArea+area; //各个矩形面积相加

}

printf("the value of function is %f",t2);

}

float function1(float x){ //函数f(x)1

float y;

y=sin(x);

return y;

}

float function2(float x){ //函数f(x)2

float y;

y=cos(x);

return y;

}

float function3(float x){ //函数f(x)3

float y;

y=exp(x);

return y;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2020-02-26

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。