zoukankan      html  css  js  c++  java
  • 自适应simpson

    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    #include<stdio.h>
    #define EPS1 0.0001
    double result=0;
    int count=0;//preserve the iterative times
    double func(double x,char choose);
    double Simpson(double a,double b,int n,char m);
    void Adapt_Simpson(double a,double b,double EPS,int n,char m);
    void main()
    {
     char fun;//choose the function of intergal
     double a,b;
     cout<<"Choose the function of intergal(1 -exp(x) ## other-exp(x)*sin(x))"<<endl;
        cin>>fun;
     cout<<"Input the lower of intergal"<<endl;
     cin>>a;
     cout<<"Input the upper of intergal"<<endl;
     cin>>b;
     cout<<"***************************************************************************"<<endl;
     Adapt_Simpson(a,b,EPS1,1,fun);
     printf("%s%0.4lf %s %5.4lf %s\t%-10.4lf\n","The result of intergal between ",a,"and",b,"is",result);
     cout<<"The number of section is "<<count<<endl;
     cout<<"==========================================================================="<<endl;
        cout<<setw(60)<<"Copyright@Cuijun 2007 Information Engineer USTB."<<endl;

    }
    //choose the function of intergal
    double func(double x,char choose)
    {
     switch(choose)
     {
      case '1': return exp(x);
       break;
      default:
       return exp(x)*sin(x);
       break;
     }
    }
    //Simpson
    double Simpson(double a,double b,int n,char fun)
    {
     double x;
     double h=(b-a)/(2*n);
     double F0;
     double F1=0;
     double F2=0;
     F0=func(a,fun)+func(b,fun);
     for(int i=1;i<=2*n-1;i++)
     {
      x=a+i*h;
      if(i%2==0)
       F2=F2+func(x,fun);
      else F1=F1+func(x,fun);  
     }
     double S=(F0+2*F2+4*F1)*h/3;
     return S;//return result
    }

    //calculate autoSimpson
    void Adapt_Simpson(double a,double b,double EPS,int n,char fun)
    {
     double S1=Simpson(a,b,n,fun);
     double S2=Simpson(a,b,2*n,fun);
     double x=S1-S2;
     double S=S2;
     if(fabs(x)<15*EPS)
     { 
      printf("%c%0.4lf,%5.4lf %c\t%-10.4lf\n",'[',a,b,']',S2);
      result+=S2;
      count++;
     }
     else
     {
      Adapt_Simpson(a,(a+b)/2,EPS/2,n,fun);
      Adapt_Simpson((a+b)/2,b,EPS/2,n,fun);
     }
    }

  • 相关阅读:
    【leetcode】153. 寻找旋转排序数组中的最小值
    vue下载网络图片
    前端开发项目细节
    如何在手机上预览本地h5页面
    react拖拽添加新组件
    js拖入并复制和拖动改变位置和改变大小
    dva model
    postMessage跨源通信
    react-router
    event.stopPropagation()和event.preventDefault(),return false的区别
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1388647.html
Copyright © 2011-2022 走看看