Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ คำนวณการปริพันธ์สองชั้น (Double Integration)

เราได้รับขอบเขตล่างของตัวแปร 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 คือวิธี Simpson 1/3
  • สร้างตารางต่อไปนี้ก่อนดำเนินการต่อ

โปรแกรม C++ คำนวณการปริพันธ์สองชั้น (Double Integration)

  • นำกฎ Simpson 1/3 ไปใช้กับแต่ละแถวสำหรับการปริพันธ์ครั้งแรก และทำซ้ำ 2 ครั้งสำหรับการปริพันธ์สองชั้น
  • พิมพ์ผลลัพธ์

อัลกอริทึม

Start
Step 1 → ประกาศฟังก์ชันคำนวณกำลังสำหรับการปริพันธ์
  float fun(float x, float y)
  return pow(pow(x, 4) + pow(y, 5), 0.5)

Step 2 → ประกาศฟังก์ชันหาค่าการปริพันธ์สองชั้น
  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)
    End
  End
  ลูป 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]
      End
      Else IF (j % 2 == 0)
        ตั้งค่า arr_2[i] += 2 * arr[i][j]
      End
      Else
        ตั้งค่า arr_2[i] += 4 * arr[i][j]
      End
    ตั้งค่า arr_2[i] *= (step_y / 3)
  End
  ตั้งค่า result = 0
  ลูป For int i = 0 และ i < n1 และ ++i
    IF (i == 0 || i == n1 - 1)
      ตั้งค่า result += arr_2[i]
    End
    Else IF (i % 2 == 0)
      ตั้งค่า result += 2 * arr_2[i]
    End
    Else
      ตั้งค่า result += 4 * arr_2[i]
    End
    ตั้งค่า result *= (step_x / 3)
  End
  return result

Step 3 → ใน main()
  ประกาศ step สำหรับ x เป็น float step_x = 1.2
  ประกาศ step สำหรับ y เป็น float step_y = 0.54
  ประกาศขอบเขตล่างของ x float lower_x = 1.3
  ประกาศขอบเขตบนของ x float upper_x = 2.1
  ประกาศขอบเขตล่างของ y float lower_y = 1.0
  ประกาศขอบเขตบนของ y float upper_y = 2.1
  เรียก (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); 
} 

// ฟังก์ชันหาค่าการปริพันธ์สองชั้น
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; // ขั้นตอนสำหรับ x 
    float step_y = 0.54; // ขั้นตอนสำหรับ y 
    float lower_x = 1.3; // ขอบเขตล่างของ x 
    float upper_x = 2.1; // ขอบเขตบนของ x 
    float lower_y = 1.0; // ขอบเขตล่างของ y 
    float upper_y = 2.1; // ขอบเขตบนของ y 
    cout << "double integration is : " << doubleIntegral(step_x, step_y, lower_x, upper_x, lower_y, upper_y); 
    return 0; 
}

ผลลัพธ์

double integration is : 2.1