MATLAB - 多項式乘法



多項式乘法是數學和工程學中的一項基本運算,尤其是在訊號處理、控制理論和數值分析等領域。在 MATLAB 中,可以使用 conv 函式或 * 運算子執行多項式乘法。

使用 conv 函式進行多項式乘法

在 MATLAB 中,可以使用 conv 函式有效地執行多項式乘法,該函式計算兩個序列的卷積,表示多項式的係數。由於兩個多項式的卷積等於它們的乘積,因此 conv 函式可用於多項式乘法。

讓我們考慮一下我們有兩個如下所示的多項式:

P(x) = 2x2 + 3x + 4
Q(x) = 5x + 6 

上述多項式方程的係數如下:

P = [2 3 4]; % Coefficients of P(x)
Q = [5 6];   % Coefficients of Q(x)

現在讓我們使用 conv 函式來乘以這兩個多項式。

R = conv(P, Q); % Result of polynomial multiplication

結果 R 將是多項式乘積的係數,它就是。

R(x) = P(x).Q(x)

因此,如果要執行程式碼並檢視輸出,我們可以使用以下程式碼:

P = [2 3 4]; % Coefficients of P(x)
Q = [5 6];   % Coefficients of Q(x)
R = conv(P, Q); % Result of polynomial multiplication
disp(R)

執行後,輸出如下:

Inner Join

我們得到的多項式為:

R(x) = 10x3 + 27x2 + 38x + 24

讓我們使用 conv() 函式嘗試另一個示例

示例 1:多項式 P(x) = 3x2 + 2x + 1 和 Q(x) = 4x3 + 5x2 + 6x + 1 乘法

我們擁有的多項式如下:

P(x) = 3x2 + 2x + 1
Q(x) = 4x3 + 5x2 + 6x + 1

上述多項式的係數如下:

P = [3 2 1]; % Coefficients of P(x)
Q = [4 5 6 7]; % Coefficients of Q(x)

因此,我們可以執行的最終程式碼如下:

P = [3 2 1]; % Coefficients of P(x)
Q = [4 5 6 7]; % Coefficients of Q(x)
R = conv(P,Q);
disp(R);

當在 matlab 命令視窗中執行程式碼時,輸出為:

Inner Join

我們得到的 R 的輸出為 [12 23 32 38 20 7];因此,我們得到的多項式為:

R(x) = 12x5 + 23x4 + 32x3 + 38x2 + 20x + 7

示例 2:兩個二次多項式的乘法

考慮以下:

Polynomials:

P(x)=x2+2x+3
Q(x)=4x2+5x+6

Coefficients:
P = [ 1 2 3 ]
Q = [ 4 5 6 ]

我們在 Matlab 中的程式碼是:

% Define the coefficients of the polynomials
P = [1 2 3];  % Coefficients of P(x) = x^2 + 2x + 3
Q = [4 5 6];  % Coefficients of Q(x) = 4x^2 + 5x + 6

% Perform the polynomial multiplication
result = conv(P, Q);

% Display the result
disp('Resulting polynomial coefficients after multiplication:');
disp(result);

對於多項式:

P(x) = x2  + 2x + 3 and 
Q(x) = 4x2 + 5x + 6 , the product is:

(x2 + 2x + 3) × (4x2 +5x +6) = 4x4 + 13x3 + 28x2 + 27x + 18

當在 matlab 命令視窗中執行程式碼時,輸出為:

>> % Define the coefficients of the polynomials
P = [1 2 3];  % Coefficients of P(x) = x^2 + 2x + 3
Q = [4 5 6];  % Coefficients of Q(x) = 4x^2 + 5x + 6

% Perform the polynomial multiplication
result = conv(P, Q);

% Display the result
disp('Resulting polynomial coefficients after multiplication:');
disp(result);

Resulting polynomial coefficients after multiplication:
     4    13    28    27    18

>> 

示例 3:將三次多項式乘以線性多項式

我們有:

Polynomials:

P(x) =3x3 +2x2 +x+4
Q(x) = x−2

Coefficients:
P = [ 3 2 1 4 ]
Q = [ 1 −2 ]

我們擁有的 matlab 程式碼為:

% Define the coefficients of the polynomials
P = [3 2 1 4];  % Coefficients of P(x) = 3x^3 + 2x^2 + x + 4
Q = [1 -2];     % Coefficients of Q(x) = x - 2

% Perform the polynomial multiplication
result = conv(P, Q);

% Display the result
disp('Resulting polynomial coefficients after multiplication:');
disp(result);

當代碼執行時,我們得到的輸出為:

>> % Define the coefficients of the polynomials
P = [3 2 1 4];  % Coefficients of P(x) = 3x^3 + 2x^2 + x + 4
Q = [1 -2];     % Coefficients of Q(x) = x - 2

% Perform the polynomial multiplication
result = conv(P, Q);

% Display the result
disp('Resulting polynomial coefficients after multiplication:');
disp(result);

Resulting polynomial coefficients after multiplication:
     3    -4    -3     2    -8

>> 
廣告
© . All rights reserved.