C++ 程序计算二重积分
我们给定变量 x 的下限、变量 x 的上限、变量 y 的下限、变量 y 的上限、对应 x 的步长、对应 y 的步长,任务是生成二重积分并显示结果。
示例
Input-: steps for x = 1.2 steps for y = 0.54 lower limit of x = 1.3 upper limit of x = 2.1 lower limit of y = 1.0 upper limit for y = 2.1 Output-: double integration is : 2.1
**下面程序中使用的方法如下** −
- 输入 x 和 y 的上限和下限,然后输入 x 和 y 的步长
- 我们用于计算 x 和 y 的二重积分的方法是辛普森 1/3 方法
- 在继续执行之前,生成以下表格
- 针对第一积分将辛普森 1/3 规则应用于每行,然后重复两次以进行二重积分
- 打印结果
算法
Start Step 1-> declare function to calculate power for integration float fun(float x, float y) return pow(pow(x, 4) + pow(y, 5), 0.5) Step 2-> declare function to find the double integral value float doubleIntegral(float step_x, float step_y, float lower_x, float upper_x, float lower_y, float upper_y) Declare int n1, n2 Declare float arr[50][50], arr_2[50], result set n1 = (upper_x - lower_x) / step_x + 1 set n2 = (upper_y - lower_y) / step_y + 1 Loop For int i = 0 and i < n1 and ++i Loop For int j = 0 and j < n2 and ++j set arr[i][j] = fun(lower_x + i * step_x, lower_y + j * step_y) End End Loop For int i = 0 and i < n1 and ++i set arr_2[i] = 0 Loop For int j = 0 and j < n2 and ++j IF (j == 0 || j == n2 - 1) Set arr_2[i] += arr[i][j] End Else IF (j % 2 == 0) Set arr_2[i] += 2 * arr[i][j] End Else set arr_2[i] += 4 * arr[i][j] End set arr_2[i] *= (step_y / 3) End set result = 0 Loop For int i = 0 and i < n1 and ++i IF (i == 0 || i == n1 - 1) set result += arr_2[i] End Else IF (i % 2 == 0) set result += 2 * arr_2[i] End Else set result += 4 * arr_2[i] End set result *= (step_x / 3) End return result Step 2-> In main() declare step for x as float step_x = 1.2 Declare step for y as float step_y = 0.54 Declare lower limit of xfloat lower_x = 1.3 Declare upper limit of xfloat upper_x = 2.1 Declare lower limit of yfloat lower_y = 1.0 Declare upper limit of yfloat upper_y = 2.1 Call (step_x, step_y, lower_x, upper_x, lower_y, upper_y) Stop
示例
#include <bits/stdc++.h> using namespace std; float fun(float x, float y) { return pow(pow(x, 4) + pow(y, 5), 0.5); } // Function to find the double integral value float doubleIntegral(float step_x, float step_y, float lower_x, float upper_x, float lower_y, float upper_y) { int n1, n2; float arr[50][50], arr_2[50], result; n1 = (upper_x - lower_x) / step_x + 1; n2 = (upper_y - lower_y) / step_y + 1; for (int i = 0; i < n1; ++i) { for (int j = 0; j < n2; ++j) { arr[i][j] = fun(lower_x + i * step_x, lower_y + j * step_y); } } for (int i = 0; i < n1; ++i) { arr_2[i] = 0; for (int j = 0; j < n2; ++j) { if (j == 0 || j == n2 - 1) arr_2[i] += arr[i][j]; else if (j % 2 == 0) arr_2[i] += 2 * arr[i][j]; else arr_2[i] += 4 * arr[i][j]; } arr_2[i] *= (step_y / 3); } result = 0; for (int i = 0; i < n1; ++i) { if (i == 0 || i == n1 - 1) result += arr_2[i]; else if (i % 2 == 0) result += 2 * arr_2[i]; else result += 4 * arr_2[i]; } result *= (step_x / 3); return result; } int main() { float step_x = 1.2; //steps for x float step_y = 0.54; //steps for y float lower_x = 1.3; //lower limit of x float upper_x = 2.1; //upper limit of x float lower_y = 1.0; //lower limit of y float upper_y = 2.1; //upper limit of y cout<<"double integration is : "<<(step_x, step_y, lower_x, upper_x, lower_y, upper_y); return 0; }
输出
double integration is : 2.1
广告