编程数值积分的矩形法和梯形法
答案:1 悬赏:10
解决时间 2021-02-17 04:35
- 提问者网友:空白
- 2021-02-17 01:40
编程数值积分的矩形法和梯形法
最佳答案
- 二级知识专家网友:厭世為王
- 2021-02-17 03:06
我们用的教材后面就附有书上讲的一些算法的程序设计的步骤,你们的也应该有的吧。你可以参考呀。如果你们没有,或者你希望得到程序的完整代码,你可以再给我发百度消息,我给你(目前我还没有现成的呢)。
矩形算法:
#include <stdio.h>
#include <math.h>
//一下四行都可以根据实际情况修改
float f(float x); //前向声明函数
const float a=0;
const float b=1; //分别定义函数的上下界
const float h=0.1; //定义步长
void main()
{
int N=(b-a)/h; //计算梯度
float re=0;
for(int i=0;i<N;i++)
re+=h*f(a+i*h);
printf("结果为:%f",re);
printf("\n");
}
float f(float x)
{
return cos(x); //以 y=sinx 为例求0到1的积分
}
梯形算法:
#include <stdio.h>
#include <math.h>
//一下四行都可以根据实际情况修改
float f(float x); //前向声明函数
const float a=0;
const float b=1; //分别定义函数的上下界
const float h=0.1; //定义步长
void main()
{
int N=(b-a)/h;
float re=0;
for(int i=0;i<N;i++)
re+=h*(f(a+i*h)+f(a+(i+1)*h))/2;
printf("结果为:%f",re);
printf("\n");
}
float f(float x)
{
return cos(x); //以 y=sinx 为例求0到1的积分
}
矩形算法:
#include <stdio.h>
#include <math.h>
//一下四行都可以根据实际情况修改
float f(float x); //前向声明函数
const float a=0;
const float b=1; //分别定义函数的上下界
const float h=0.1; //定义步长
void main()
{
int N=(b-a)/h; //计算梯度
float re=0;
for(int i=0;i<N;i++)
re+=h*f(a+i*h);
printf("结果为:%f",re);
printf("\n");
}
float f(float x)
{
return cos(x); //以 y=sinx 为例求0到1的积分
}
梯形算法:
#include <stdio.h>
#include <math.h>
//一下四行都可以根据实际情况修改
float f(float x); //前向声明函数
const float a=0;
const float b=1; //分别定义函数的上下界
const float h=0.1; //定义步长
void main()
{
int N=(b-a)/h;
float re=0;
for(int i=0;i<N;i++)
re+=h*(f(a+i*h)+f(a+(i+1)*h))/2;
printf("结果为:%f",re);
printf("\n");
}
float f(float x)
{
return cos(x); //以 y=sinx 为例求0到1的积分
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯