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

通过黎曼和解定积分既是把在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;
}

 

===========================================================

维护日志:

2018-7-24:Review

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值