⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 连分式法求积.cpp

📁 数值分析中最常用的14个程序: 01_N皇后问题 01_循环赛程表 02_分段线性插值 02_牛顿插值法 03_构造正交多项式 03_最佳一致逼近多项式 04_简单迭代法求方程根[1
💻 CPP
字号:
// 连分式法求积.cpp : Defines the entry point for the console application.
//

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

double cf(double a,double b,double e,double (*f)(double))
{
    int m,n,j,k,l;
    double h[10],bb[10],hh,t1,s1,ep,s,x,t2,g;
    m=1;
	n=1;
    hh=b-a;
	h[0]=hh;
    t1=hh*((*f)(a)+(*f)(b))/2.0;	//梯形公式计算初值
    s1=t1;
	bb[0]=s1;
	ep=1.0+e;
    while ((ep>=e)&&(m<=9))
      { s=0.0;
        for (k=0;k<=n-1;k++)
          {
			x=a+(k+0.5)*hh;
            s=s+(*f)(x);
          }
        t2=(t1+hh*s)/2.0;	//变步长梯形法求积
        m=m+1;
        h[m-1]=h[m-2]/2.0;	
        g=t2;
        l=0;
		j=2;
        while ((l==0)&&(j<=m))	//计算连分式参数
          { 
			s=g-bb[j-2];
            if (fabs(s)+1.0==1.0) l=1;
            else g=(h[m-1]-h[j-2])/s;
            j=j+1;
          }
        bb[m-1]=g;
        if (l!=0) bb[m-1]=1.0e+35;
        g=bb[m-1];
        for (j=m;j>=2;j--)	//计算连分式值
           g=bb[j-2]-h[j-2]/g;
        ep=fabs(g-s1);
        s1=g;
		t1=t2;
		hh=hh/2.0;
		n=n+n;
      }
    return(g);
}

 double f(double x)
{    
    double y;
    y=x*x+sin(x);	//被积函数
    return(y);
}
  
 
void main()
{
	cout << "  *******************************************" << endl;
	cout << "  **                                       **" << endl;
	cout << "  **              连分式法求积             **" << endl;
	cout << "  **                                       **" << endl;
	cout << "  *******************************************" << endl << endl;
	double a=2.5;	//积分下限
	double b=8.4;	//积分上限
	double e=0.0001;	//精度
    double s=cf(a,b,e,f);
	cout.setf(ios::scientific);
	cout << "所求积分S=" << s << endl;
}

  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -