如图,矩形法就是把上下界分成若干份,用每份的长度乘每份的宽度,把所有的加起来。
//
// main.c
// demo
//
// Created by 布布 on 2020/4/17.
// Copyright © 2020 布布. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int main(int argc, const char * argv[]) {
float fun(float x);
float a,b;
printf("please input a and b:");
scanf("%f,%f",&a,&b);//输入上下界
int n=50;
float h=(b-a)/n;//把上下界分成五十份
float s=0;
float i=0;
for (i=a; i<b; i+=h) {
s=s+fun(i)*h;//每份的函数值乘以他的宽度然后累加起来
}
printf("\nresult is %f\n",s);
return 0;
}
float fun(float x)
{
return exp(x);
}