MATLAB - 多項式除法



多項式除法是將一個多項式除以另一個多項式的過程。在 MATLAB 中,可以使用 `deconv` 函式執行多項式除法,該函式計算兩個多項式的解卷積。由於多項式除法本質上是多項式乘法的逆運算,因此 `deconv` 函式可用於多項式除法。

在 Matlab 中使用 deconv() 函式

MATLAB 中的 `deconv()` 函式可以幫助你將一個多項式除以另一個多項式。

語法

[x,r] = deconv(y,h)

假設你有兩個多項式 P(x) 和 Q(x)。當你將 P(x) 除以 Q(x) 時,你會得到商 X(x) 和餘數 R(x)。Matlab 中的 `deconv()` 函式會為你完成這個除法。

例如,如果你有 P(x) = 6x³ + 5x² + 4x + 3 和 Q(x) = 2x + 1,使用 `deconv()` 將得到商 X(x) = 3x² + 2x + 1 和餘數 R(x) = 0。

在 Matlab 中,你使用 `deconv(y, h)`,其中 y 是 P(x) 的係數,h 是 Q(x) 的係數。該函式返回 X(x) 的係數作為商和 R(x) 作為餘數。

示例 1:多項式除法

讓我們考慮以下兩個多項式:

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

上述多項式的係數如下:

P = [ 6  5  4  3 ];
Q = [2 1]

這是一個 Matlab 程式碼:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

**在上面的示例中** - 將 P(x) 除以 Q(x) 的結果,即除法產生的多項式。無法被 Q(x) 進一步除的多項式。

商 X(x) 將是一個 2 次多項式,因為 P(x) 是 3 次多項式,而 Q(x) 是 1 次多項式。

餘數 R(x) 將是一個常數項,因為它表示除法後的剩餘部分。

程式碼執行後,我們將得到以下輸出:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

Quotient coefficients:
    3.0000    1.0000    1.5000

Remainder coefficients:
         0         0         0    1.5000

>>

示例 2:高次多項式

讓我們除以兩個高次多項式。

Polynomials:

P(x) = 4x5 + 3x4 + 2x3 + x2 + 5x + 6

Q(x) = 2x2 + x + 1

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

為了解決上述多項式除法,我們使用的程式碼是:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

在上面的程式碼中,我們有:

  • P(x) 是 5 次多項式。
  • Q(x) 是 2 次多項式。
  • 商將是一個 3 次多項式,餘數將是一個 1 次多項式。

在 Matlab 命令視窗中執行程式碼後,我們將得到以下輸出:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

Quotient coefficients:
    2.0000    0.5000   -0.2500    0.3750

Remainder coefficients:
         0         0         0         0    4.8750    5.6250

>> 

示例 3:餘數為零的除法

考慮兩個多項式,其中除法結果的餘數為零。

Polynomials:
P(x) = x4 + 4x3 + 6x2 + 4x + 1  (which is the expansion of (x+1)4)
Q(x) = x + 1

Coefficients:
P = [ 1 4 6 4 1 ]

Q = [ 1 1 ]
  

我們的 Matlab 程式碼是:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

在上面的程式碼中:

  • 由於 P(x) 被 Q(x) 整除,因此餘數將為零。
  • 商將是 P(x) 除以 Q(x) 的結果,即 (x+1)³

在 Matlab 命令視窗中執行程式碼後,輸出為:

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

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);


Quotient coefficients:
     1     3     3     1

Remainder coefficients:
     0     0     0     0     0

>> 

示例 4:多項式除以常數

如果將多項式除以常數(0 次多項式),則商將只是原始多項式除以該常數。

Polynomials:
P(x) = 3x3 + 6x2 + 9x + 12
Q(x) = 3 (constant)

Coefficients:
P = [ 3 6 9 12 ]
Q = [ 3 ]

我們的 Matlab 程式碼是:

% Define the coefficients of the polynomials
P = [3 6 9 12];  % Coefficients of P(x) = 3x^3 + 6x^2 + 9x + 12
Q = [3];         % Coefficients of Q(x) = 3 (constant)

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

在 Matlab 命令視窗中執行程式碼後,輸出為:

>> % Define the coefficients of the polynomials
P = [3 6 9 12];  % Coefficients of P(x) = 3x^3 + 6x^2 + 9x + 12
Q = [3];         % Coefficients of Q(x) = 3 (constant)

% Perform the polynomial division
[quotient, remainder] = deconv(P, Q);

% Display the results
disp('Quotient coefficients:');
disp(quotient);

disp('Remainder coefficients:');
disp(remainder);

Quotient coefficients:
     1     2     3     4

Remainder coefficients:
     0     0     0     0

>> 
廣告
© . All rights reserved.