随笔-48  评论-259  文章-1  trackbacks-0

#include"iostream.h"
#include"math.h"

double f(double x)
{
   return pow(x,3.0/2.0);
}
double T(double n,double a,double b)
{
  double sum=0;
  for(int i=0;2*i+1<=n;i++)
  {
    sum+=f(a+double(2*i+1)/n);
  }
  if(n==1) return 0.5*(f(a)+f(b));
  return 0.5*T(n/2,a,b)+sum*(1.0/n);
}
void Romberg(double a,double b,double e)
{
  double Old[10]={0.0},New[10]={0.0};
  double h=b-a;
  New[0]=h*(f(a)+f(b))/2.0;
  int j;
  for(int i=1;;i++)
  {
    for(j=0;j<i;j++) Old[j]=New[j];
 New[0]=T(pow(2,i),a,b);
 for(j=1;j<=i;j++)
  New[j]=(pow(4,j)*New[j-1]/(pow(4,j)-1)-Old[j-1]/(pow(4,j)-1));
 if(fabs(New[i]-Old[i-1])<e) break;
  }
  cout<<New[i];
}

void main()
{
  double a,b; 
  double e;
  cout<<"请输入积分的下、上限:";
  cin>>a>>b;
  cout<<"输入你要的精度:";
  cin>>e;
  Romberg(a,b,e);
}

posted on 2007-06-08 23:28 星梦情缘 阅读(3372) 评论(6)  编辑 收藏 引用 所属分类: 数据结构的所有实现程序

评论:
# re: 数值分析--龙贝格算法 2007-06-11 15:30 | 农夫
怎么用的啊?  回复  更多评论
  
# re: 数值分析--龙贝格算法 2007-06-11 20:53 | 星梦情缘
double f(double x)
{
return pow(x,3.0/2.0);
}

你把这里的函数改成你要的函数就可以了用了的  回复  更多评论
  
# re: 数值分析--龙贝格算法 2007-06-20 23:39 |
有关于反幂法计算矩阵特征值的程序吗  回复  更多评论
  
# re: 数值分析--龙贝格算法 2010-05-12 21:23 | S
对 函数的 调用不明确····  回复  更多评论
  
# re: 数值分析--龙贝格算法 2010-05-12 21:24 | S
k:\jisuan\longbeigea\longbeigea\longbeigea.cpp(34) : error C2668: “pow”: 对重载函数的调用不明确
1> d:\软件程序\vc++8.0\vc\include\math.h(575): 可能是“long double pow(long double,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(527): 或 “float pow(float,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(489): 或 “double pow(double,int)”
1> 试图匹配参数列表“(int, int)”时
1>k:\jisuan\longbeigea\longbeigea\longbeigea.cpp(36) : error C2668: “pow”: 对重载函数的调用不明确
1> d:\软件程序\vc++8.0\vc\include\math.h(575): 可能是“long double pow(long double,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(527): 或 “float pow(float,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(489): 或 “double pow(double,int)”
1> 试图匹配参数列表“(int, int)”时
1>k:\jisuan\longbeigea\longbeigea\longbeigea.cpp(36) : error C2668: “pow”: 对重载函数的调用不明确
1> d:\软件程序\vc++8.0\vc\include\math.h(575): 可能是“long double pow(long double,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(527): 或 “float pow(float,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(489): 或 “double pow(double,int)”
1> 试图匹配参数列表“(int, int)”时
1>k:\jisuan\longbeigea\longbeigea\longbeigea.cpp(36) : error C2668: “pow”: 对重载函数的调用不明确
1> d:\软件程序\vc++8.0\vc\include\math.h(575): 可能是“long double pow(long double,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(527): 或 “float pow(float,int)”
1> d:\软件程序\vc++8.0\vc\include\math.h(489): 或 “double pow(double,int)”
1> 试图匹配参数列表“(int, int)”时
1>生成日志保存在“file://k:\jisuan\longbeigea\longbeigea\Debug\BuildLog.htm”
1>longbeigea - 4 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
  回复  更多评论
  
# re: 数值分析--龙贝格算法[未登录] 2011-04-20 19:58 | C++爱好者
太谢谢你的这段龙贝格积分的程序,很赞!谢谢!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理