C语言求定积分 C语言实现黎曼和求定积分

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言求定积分 C语言实现黎曼和求定积分

liu_if_else   2021-04-21 我要评论

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
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;
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们