C语言实现龙贝格求积
2016-01-20 11:26
465 查看
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
#include<string.h>
double f(double x)
{
return (4*x*x/(1+x*x));
}
double Romberg(double top,double bottom,double precision)
{
int k=1;
double S,x,T1,T2,S1,S2,C1,C2,R1,R2,h=bottom-top;
/*
转载请注明出处:去转盘网www.quzhuanpan.com
*/
//S梯形公式,T梯型变步长,S梯形加速,C幸普森加速,R龙贝格求积
T1=h*(f(top)+f(bottom))/2;//梯形公式
while(1)
{
S=0;
x=top+h/2;
do
{
S+=f(x);
x+=h;
}while(x<bottom);
T2=(T1+h*S)/2.0;
if(fabs(T2-T1)<precision)
{
return T2;
}
S2=T2+(T2-T1)/3;//梯形加速
if(k==1)
{
T1=T2;
S1=S2;//几下原来的S2
h/=2;
k+=1;
continue;
}
C2=S2+(S2-S1)/15;//新的S2减原来的S2,即使S1,幸普森加速
if(k==2)
{
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
continue;
}
R2=C2+(C2-C1)/63.0;
if(k==3)
{
R1=R2;
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
continue;
}
if(fabs(S2-S1)<precision)
{
return S2;
}
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
if(fabs(R2-R1)<precision);
{
return R2;
}
}
}
int main()
{
double top,bottom,precision,S;
printf("Pletopse input the begin: ");
scanf("%lf",&top);
printf("Pletopse input the end: ");
scanf("%lf",&bottom);
printf("Pletopse input the precision:");
scanf("%lf",&precision);
S=Romberg(top,bottom,precision);
printf("The result is:%lf",S);
getch();
return 0;
}
#include <stdlib.h>
#include<conio.h>
#include<math.h>
#include<string.h>
double f(double x)
{
return (4*x*x/(1+x*x));
}
double Romberg(double top,double bottom,double precision)
{
int k=1;
double S,x,T1,T2,S1,S2,C1,C2,R1,R2,h=bottom-top;
/*
转载请注明出处:去转盘网www.quzhuanpan.com
*/
//S梯形公式,T梯型变步长,S梯形加速,C幸普森加速,R龙贝格求积
T1=h*(f(top)+f(bottom))/2;//梯形公式
while(1)
{
S=0;
x=top+h/2;
do
{
S+=f(x);
x+=h;
}while(x<bottom);
T2=(T1+h*S)/2.0;
if(fabs(T2-T1)<precision)
{
return T2;
}
S2=T2+(T2-T1)/3;//梯形加速
if(k==1)
{
T1=T2;
S1=S2;//几下原来的S2
h/=2;
k+=1;
continue;
}
C2=S2+(S2-S1)/15;//新的S2减原来的S2,即使S1,幸普森加速
if(k==2)
{
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
continue;
}
R2=C2+(C2-C1)/63.0;
if(k==3)
{
R1=R2;
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
continue;
}
if(fabs(S2-S1)<precision)
{
return S2;
}
C1=C2;
T1=T2;
S1=S2;
h/=2;
k+=1;
if(fabs(R2-R1)<precision);
{
return R2;
}
}
}
int main()
{
double top,bottom,precision,S;
printf("Pletopse input the begin: ");
scanf("%lf",&top);
printf("Pletopse input the end: ");
scanf("%lf",&bottom);
printf("Pletopse input the precision:");
scanf("%lf",&precision);
S=Romberg(top,bottom,precision);
printf("The result is:%lf",S);
getch();
return 0;
}
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法