MATLAB - Polynomial Multiplication



Polynomial multiplication is a fundamental operation in mathematics and engineering, especially in fields like signal processing, control theory, and numerical analysis. In MATLAB, polynomial multiplication can be performed using the conv function or the * operator.

Polynomial Multiplication Using conv Function

In MATLAB, polynomial multiplication can be efficiently performed using the conv function, which calculates the convolution of two sequences, representing the coefficients of the polynomials. Since the convolution of two polynomials is equal to their product, the conv function can be used for polynomial multiplication.

Let's consider we have two polynomials as shown below −

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

The coefficients for above polynomials equations are as follows −

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

Now let us use conv function to multiply the two polynomials.

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

The result R will be the coefficients of the polynomial product which is nothing but.

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

So if you want to execute the code and see the output we can use the following code −

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)

On execution the output is as follows −

Inner Join

The final polynomial we have is,

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

Let us try another example using conv() function

Example 1: Polynomial P(x) = 3x2 + 2x + 1 and Q(x) = 4x3 + 5x2 + 6x + 1 Multiplication

The polynomials we have is as follows −

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

The coefficients from above polynomials is as follows −

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

So the final code we can execute is as follows −

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

When the code is executed in matlab command window the output is −

Inner Join

The output we get for R is [12 23 32 38 20 7]; so the final polynomial we get is −

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

Example 2: Multiplying Two Quadratic Polynomials

Consider the following,

Polynomials:

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

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

The code in Matlab we have is,

% 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);

For the polynomials,

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

When the code is executed in matlab command window the output is −

>> % 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

>> 

Example 3: Multiplying a Cubic Polynomial by a Linear Polynomial

We have,

Polynomials:

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

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

The matlab code we have is -

% 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);

When the code is executed the output we have is −

>> % 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

>> 
Advertisements