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);
     }
    }

  • 相关阅读:
    入侵特斯拉——智能汽车安全性分析
    D-Link系列路由器漏洞挖掘入门
    工控安全入门之 Ethernet/IP
    浅谈JS数据类型存储问题
    【备忘】12306购票必杀技
    制作炫酷的专题页面
    杂记(下)
    杂记(上)
    跨域请求解决方案
    好用的表单验证插件
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1388647.html
Copyright © 2011-2022 走看看