二重积分函数.cpp

来自「4个关于二维积分函数的C程序」· C++ 代码 · 共 42 行

CPP
42
字号
#include <iostream.h>
#include <math.h>

float f1( float x,float y )                               //定义被积函数 1+x^2
  { float z;
  
    z=1+pow(x,2)+y;
    return z;
    }

   //////////////////////////////////////////////////////
   // *fun被积函数;b,a第一维的上下限;d,c第二维的上下限 //
   // m第一维的距离等分数;n第二维的距离等分数;         //
   //////////////////////////////////////////////////////
float integral(float (*fun)(float ,float ),float a, float b ,float c, float d,int m,int n)
  { float s,h1,h2,y;

    s=((*fun)( a,b )+(*fun)( c,d ))/2.0;
    h1 =(b-a)/m;
    h2 =(d-c)/n;
    for ( int i=1; i<m; i++ )
      for ( int j=1; j<n; j++ )
         s =(2*s+(*fun)( a+i*h1 ,b+j*h2)+(*fun)( a+(i+1)*h1 ,b+(j+1)*h2))/2;
    y=s*h1*h2;
    return y;  }


void main (  )
  {  int m,n;
     float c;
     cout << "\nplease input the value of m: ";
     cin >> m;
     cout << "\nplease input the value of n: ";
     cin >> n;
     cout << "\nthe value of integral:   ";
     c =integral( f1,1.0,3.0,0.5,1.0,m,n );

     cout << c;
     cin >> c;
         }

⌨️ 快捷键说明

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