练习题2021111602

梯形法。用梯形法编程求函数f(x)=x2+2x+1的定积分,∫baf(x)dx的值。即将区间[a,b]划分成n个子区间,用梯形面积近似曲线下的面积。梯形面积:两底高度之和乘高除以2。

**输入格式要求:"%d,%lf,%lf" 提示信息:"Enter n,a,b:" **输出格式要求:"Sum=%lf\n" 程序运行示例如下: Enter n,a,b:10,0,2 Sum=8.680000

————————————————————————————————————————————————————————————————————————————————

#include <stdio.h>
int main (){
int n,i;
double x,a,b,h,ya,yb,sum,he;
printf("Enter n,a,b:");
scanf("%d,%lf,%lf",&n,&a,&b);
h=(b-a)/n;
ya=a*a+2*a+1;
yb=b*b+2*b+1;
for(i=1;i<n;i++)
{x=a+i*h;
he+=x*x+2*x+1;
}
sum=h*((ya+yb)/2+he);
printf("Sum=%lf\n",sum);
return 0;
}

上一篇:NoteBook-Git


下一篇:2. Understand the importance of the sampling rate