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

Olinda ·
更新时间:2024-09-21
· 663 次阅读

本文实例为大家分享了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; }

您可能感兴趣的文章:C语言求解定积分的方法C语言:利用指针编写程序,用梯形法计算给定的定积分实例C语言实现求定积分的方法C语言使用矩形法求定积分的通用函数



定积分 C语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号