学习笔记——利用C&C++语言计算二重积分

二重积分定义如下(来源百度百科)
学习笔记——利用C&C++语言计算二重积分_第1张图片
以z=xy在区域0<=y<=1,0<=x<=y內的二重积分为例,经计算理论值为:0.125,下面用C语言实现二重积分的离散计算

#include       
using namespace std;
double fun_dbjf(double step,double low_y, double high_y, double(*p_y_1)(double), double(*p_y_2)(double), double(*p_f_x_y)(double, double));//计算f(x,y)的二次积分
double fun_jf_fxy(double step, double low_x, double high_x, double y, double(*p_x)(double, double));//计算函数f(x,y0)的积分
double fun_fxy(double x, double y);//函数f(x,y)
double fun_y_l(double x);//下限积分
double fun_y_h(double x);//上限积分
int main(int argc, char*argv[])
{
    cout << "二次积分值为:" << fun_dbjf(0.00001, 0, 1, fun_y_l, fun_y_h, fun_fxy) << endl;
    cout << "理论值为:" << 1.0 / 8.0;


}
double fun_dbjf(double step,double low_y, double high_y, double(*p_y_l)(double), double(*p_y_h)(double), double(*p_f_x_y)(double, double))
{
    double sum = 0;
    for (double t = low_y; t < high_y; t += step)
    {
        sum += fun_jf_fxy(step, (*p_y_l)(t), (*p_y_h)(t), t, p_f_x_y)*step;
    }
    return sum;
}
double fun_jf_fxy(double step, double low_x, double high_x, double y,double(*p_x)(double,double))
{
    double sum = 0;
    for (double t = low_x; t < high_x; t += step)
    {
        sum += (*p_x)(t, y)*step;
    }
    return sum;
}
double fun_fxy(double x, double y)
{
    return x*y;
}
double fun_y_l(double x)
{
    return 0;//f(x)=0
}
double fun_y_h(double x)
{
    return x;//f(x)=x
}

运行结果:
学习笔记——利用C&C++语言计算二重积分_第2张图片
运行时间较长,但结果与理论值十分接近

你可能感兴趣的:(算法,学习笔记)