具有无限极限的二维数值积分(C ++)

时间:2017-08-10 18:37:43

标签: c++ numerical-integration

为了整合形式的二维函数

cmdkey

我一直在尝试使用以下代码(用C ++编写),这些代码主要来自于Numerical Recipes一书,该书调用了高斯求积法来进行积分:

cmdkey /list |
    Select-String -Pattern $ComputerName -Context 2 |
    Select-Object -Property @(
         @{N='ComputerName';E={$ComputerName}}
         @{N='User';E={$PSItem.Context.PostContext[-1] -replace '\s*User:\s*'}}
    )

此代码适用于具有有限限制的二维积分,但由于外部限制被视为无穷大而失败。为了解释这一点,我试图使用变量的变化:

$$\int_{1}^\infty \int_{-\sqrt{x^2-1}}^{\sqrt{x^2-1}} e^{-x} \rm{d}y \rm{d}x,$$

但这仍然没有给出正确的答案。它应该是

 static float xsav;
 static float (*nrfunc)(float,float);

 float quad2d(float (*func)(float, float), float x1, float x2)
 {
     float qgaus(float (*func)(float), float a, float b); 
     float f1(float x);

     nrfunc=func;
     return qgaus(f1,x1,x2); 
 }

 float f1(float x) 
 {
     float qgaus(float (*func)(float), float a, float b); 
     float f2(float y);
     float yy1(float),yy2(float);

     xsav=x;
     return qgaus(f2,yy1(x),yy2(x)); 
 }

 float f2(float y)  
 {
     return (*nrfunc)(xsav,y);
 }

但是给出了

#define FUNC(x) ((*funk)(-log(x))/(x))

float qgaus(float (*funk)(float), float aa, float bb)
{
    int j;
    float xr,xm,dx,s,a,b;

    b=exp(-aa); 
    a=0.0;

    static float x[]={0.0,0.1488743389,0.4333953941,
        0.6794095682,0.8650633666,0.9739065285};
    static float w[]={0.0,0.2955242247,0.2692667193, 
        0.2190863625,0.1494513491,0.0666713443};

    xm=0.5*(b+a); 
    xr=0.5*(b-a);
    s=0;
    for (j=1;j<=5;j++)
    {
        dx=xr*x[j];
        s += w[j]*(FUNC(xm+dx)+FUNC(xm-dx)); 
    }
    return s *= xr; 
}


float f(float x, float y)
{
    float a = exp(-x);
    return a;
}

float yy1(float x)
{
    float y = -sqrt(x*x-1);
    return y;   
}

float yy2(float x)
{
    float y = sqrt(x*x-1);
    return y;
}



static float xsav;
static float (*nrfunc)(float, float);

float quad2d(float (*func)(float, float), float x1, float x2)
{
    float qgaus(float (*func)(float), float aa, float bb);
    float f1(float x);

    nrfunc=func;
    float t = qgaus(f1,x1,x2);
    return t;
}

float f1(float x)
{
    float qgaus(float (*func)(float), float aa, float bb);
    float f2(float y);
    float yy1(float);
    float yy2(float);

    xsav=x;
    float r = qgaus(f2,yy1(x),yy2(x));
    return r;
}

float f2(float y) 
{
    float k = (*nrfunc)(xsav,y);
    return k;
}

int main ()
{
    float z;
    z = quad2d(f, 1.0, 20.0);
    cout << z << endl;
}

有关如何修改此代码以解释无限限制的任何建议将非常感谢!

0 个答案:

没有答案