MATLAB - 二重積分



數學中的積分是一個表示曲線下面積的概念。它是微積分中的一個基本概念,可以被認為是量的累積。積分用於求面積、體積、中心點以及許多有用的東西。

積分主要有兩種型別

定積分 - 計算曲線在兩個特定點之間的面積。

不定積分 - 表示一組函式,幷包含一個積分常數。

定積分示例

如果你有一個函式 f(x),從 a 到 b 的定積分寫成 -

$$\mathrm{\displaystyle\int_{b}^{a} f(x) \: dx}$$

這給出了 f(x) 從 x = a 到 x = b 的曲線下面積。

什麼是二重積分?

二重積分將單積分的概念擴充套件到兩個變數的函式。它用於計算三維空間中曲面下的體積。函式 f(x,y) 在 xy 平面上的區域 R 上的二重積分寫成。

$$\mathrm{\iint_{R} \: f(x,y) \: dA}$$

其中 dA 表示區域 R 中的一個微小面積元素。

二重積分示例

如果你有一個函式 f(x,y),並且你想找到曲面在矩形區域 R = [a,b] x [c,d] 上的體積,則二重積分是。

$$\mathrm{\iint_{R} \: f(x,y) \: dA \: = \: \int_{a}^{b}\int_{c}^{d}f(x,y)dy \: dx}$$

MATLAB 和二重積分

MATLAB 是一種強大的數值計算工具,包括評估積分和二重積分。

MATLAB 中的單積分

要在 MATLAB 中計算單積分,可以使用 integral 函式。例如,要找到 f(x) = x2 從 0 到 1 的積分 -

f = @(x) x.^2;
result = integral(f, 0, 1);
disp(result);

MATLAB 中的二重積分

要在 MATLAB 中計算二重積分,可以使用 integral2 函式。以下是如何計算 f(x,y) = x2 + y2 在該區域上的二重積分。

$$\mathrm{0 \: \leq \: x \: \leq \: 1 \: and \: 0 \: \leq \: y \: \leq \: 1}$$

f = @(x, y) x.^2 + y.^2;
result = integral2(f, 0, 1, 0, 1);
disp(result);

在這個例子中

f 是一個表示 f(x,y) = x2 + y2 的匿名函式。

integral2 計算在 x 和 y 的指定限值上的二重積分。

現在讓我們更詳細地瞭解 integral2() 方法。

使用 integral2() 函式的 Matlab 二重積分

integral2() 函式數值計算二重積分。

語法

q = integral2(fun,xmin,xmax,ymin,ymax)
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value)

語法解釋

q = integral2(fun,xmin,xmax,ymin,ymax):在此語法中

  • fun 是您要積分的函式。它用 x 和 y 表示。
  • xmin 和 xmax 是 x 變數的限值。該函式將從 x=xmin 積分到 x=xmax。
  • ymin 和 ymax 這些是 y 變數的限值。積分將從 y=ymin 執行到 y=ymax。這些是常數或 x 的函式。

q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value):在此語法中

  • fun - 要積分的函式。
  • xmin 和 xmax - x 變數的限值。
  • ymin 和 ymax - y 變數的限值。
  • “名稱,值” 對允許您自定義 MATLAB 執行積分的方式。

常見的“名稱,值”對

  • 'RelTol' - 設定精度相關的容差。
  • 'AbsTol' - 設定精度絕對容差。
  • 'Method' - 指定積分方法(例如,'auto'、'iterated'、'tiled')。
  • 'Waypoints' - 指定積分器應包含的中間點。

使用 integral2() 函式求解二重積分

示例 1

使用 integral2() 對邊界處具有奇異性的三角形區域進行積分。

我們想計算函式 f(x,y) 在 xy 平面上的三角形區域上的積分。邊界處的奇異性意味著該函式在區域的邊界處可能變得無限大或未定義。這可能使積分更難以計算,但 MATLAB 的 integral2 可以透過適當的限值和函式定義來處理此類情況。

定義三角形區域

考慮一個頂點位於點 (0,0)、(1,0) 和 (0,1) 的三角形區域。該區域可以用以下限值描述 -

0  ≤ x ≤1

0 ≤ y ≤ 1−x

讓我們考慮函式

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

此函式在原點 (0,0) 處具有奇異性,在該點處它變得無限大。

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Compute the double integral over the triangular region
q = integral2(fun, xmin, xmax, ymin, ymax);

% Display the result
disp(q);

程式碼說明

我們使用 MATLAB 中的匿名函式控制代碼定義如下所示的函式。

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

設定積分限值:對於 x,限值從 0 到 1。

對於 y,下限為 0,上限為 1−x。請注意,ymin 設定為 0(常數),而 ymax 是 x 的函式。

計算積分:我們使用 integral2 函式計算二重積分。透過確保 ymin 是常數並且 ymax 是 x 的函式,我們滿足了 integral2 函式的要求。

積分結果使用 disp(q) 顯示。

當您在 matlab 中執行程式碼時,我們得到的輸出為 -

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Compute the double integral over the triangular region
q = integral2(fun, xmin, xmax, ymin, ymax);

% Display the result
disp(q);
   1.2465

示例 2

使用 integral2() 計算具有特定方法和誤差容差的引數化函式的二重積分。

要計算具有其他選項(如積分方法和誤差容差)的引數化函式的二重積分,您可以使用帶有“名稱,值”對引數的 integral2 函式。

示例函式和區域

讓我們考慮在頂點位於點 (0,0)、(1,0) 和 (0,1) 的三角形區域上的函式。

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

以下是如何使用 MATLAB 設定和計算此積分,以及積分方法和誤差容差的其他選項

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Define additional options: Relative Tolerance, Absolute Tolerance, and Method
options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'};

% Compute the double integral over the triangular region with additional options
q = integral2(fun, xmin, xmax, ymin, ymax, options{:});

% Display the result
disp(q);

程式碼說明

該函式使用匿名函式控制代碼定義。

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

設定積分限值

For x, the limits are from 0 to 1.

對於 y,下限為 0(常數),上限為 1−x(x 的函式)。

我們定義的其他選項為 -

'RelTol', 1e-6 指定 10-6 的相對容差

'AbsTol', 1e-8 指定 10-8 的絕對容差

'Method', 'auto' 讓 MATLAB 自動選擇最合適的方法。如果需要,還可以指定其他方法,如 'iterated' 或 'tiled'。

integral2 函式被呼叫,其中包含函式、積分限值以及在 options 細胞陣列中指定的其他選項。options{:} 語法將細胞陣列解包成以逗號分隔的引數列表。

積分結果使用 disp(q) 顯示。

當您在 matlab 命令視窗中執行程式碼時,我們得到的輸出如下

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Define additional options: Relative Tolerance, Absolute Tolerance, and Method
options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'};

% Compute the double integral over the triangular region with additional options
q = integral2(fun, xmin, xmax, ymin, ymax, options{:});

% Display the result
disp(q);
   1.2465
廣告
© . All rights reserved.