Skip to content

MATLAB - Polynomial Multiplication

Polynomials are central to many areas of engineering and science, including signal processing (filters), control theory (transfer functions), and data analysis (curve fitting). MATLAB provides a simple and efficient way to work with them by representing a polynomial as a vector of its coefficients.

The convention is to store the coefficients in a row vector in descending order of power. For example, the polynomial P(x) = 2x³ + 5x - 7 is represented as:

% P(x) = 2*x^3 + 0*x^2 + 5*x - 7
P = [2 0 5 -7]; % Note the explicit zero for the missing x^2 term!

The multiplication of two polynomials is mathematically equivalent to the convolution of their coefficient vectors. Therefore, MATLAB uses the conv function to perform polynomial multiplication.

Example 1: Multiplying Two Simple Polynomials

Section titled “Example 1: Multiplying Two Simple Polynomials”

Let’s multiply P(x) = 3x + 2 by Q(x) = x² - 4x + 5.

% Define the coefficient vectors
P = [3 2]; % Represents 3x + 2
Q = [1 -4 5]; % Represents x^2 - 4x + 5
% Perform the multiplication using convolution
R_coeffs = conv(P, Q);
disp('Coefficients of the resulting polynomial R(x):');
disp(R_coeffs);

MATLAB will execute this and display the resulting coefficients:

Coefficients of the resulting polynomial R(x):
3 -10 7 10

This result [3 -10 7 10] corresponds to the polynomial R(x) = 3x³ - 10x² + 7x + 10.

Example 2: Multiplying Two Quadratic Polynomials

Section titled “Example 2: Multiplying Two Quadratic Polynomials”

Let’s multiply A(x) = x² + 2x + 3 and B(x) = 4x² + 5x + 6.

% Define the coefficients of the polynomials
A = [1 2 3];
B = [4 5 6];
% Perform the polynomial multiplication
C = conv(A, B);
disp('Resulting polynomial coefficients:');
disp(C);

The expected result is (x²+2x+3)(4x²+5x+6) = 4x⁴ + 13x³ + 28x² + 27x + 18. The output from MATLAB confirms this:

Resulting polynomial coefficients:
4 13 28 27 18

How can you be sure the multiplication is correct? A simple and effective method is to evaluate the original and resulting polynomials at a test point. If R(x) = P(x) * Q(x), then R(z) must equal P(z) * Q(z) for any number z. We can use the polyval function for this check.

% Using the polynomials from Example 1
P = [3 2];
Q = [1 -4 5];
R_coeffs = conv(P, Q); % [3 -10 7 10]
% Choose a test point, e.g., x = 2
x_test = 2;
% Evaluate P(2) and Q(2)
val_P = polyval(P, x_test); % 3*2 + 2 = 8
val_Q = polyval(Q, x_test); % 2^2 - 4*2 + 5 = 1
% Evaluate R(2)
val_R = polyval(R_coeffs, x_test); % 3*8 - 10*4 + 7*2 + 10 = 8
% Check if the results match
if abs(val_R - (val_P * val_Q)) < 1e-9 % Use a tolerance for floating point
disp('Verification successful!');
else
disp('Verification failed!');
end

This programmatic check provides confidence in your calculations and is a great debugging technique.

MATLAB offers a complete set of tools for polynomial analysis:

  • deconv(R, P): Polynomial division. Finds the result of R(x) / P(x).
  • roots(P): Finds the roots (zeros) of the polynomial.
  • poly(r): Creates a polynomial from its roots.
  • polyfit(x, y, n): Fits a polynomial of degree n to data points (x, y). This is fundamental for regression analysis.
  • polyder(P): Computes the derivative of the polynomial.
  • polyint(P): Computes the integral of the polynomial.

For more intuitive, textbook-style manipulation, you can use the Symbolic Math Toolbox. This approach can be more readable but may be slower than the numerical conv method for large polynomials.

syms x;
% Define polynomials symbolically
P_sym = 3*x + 2;
Q_sym = x^2 - 4*x + 5;
% Multiplication is now intuitive
R_sym = P_sym * Q_sym;
% Expand to see the full polynomial
expand(R_sym)

MATLAB will return:

ans =
3*x^3 - 10*x^2 + 7*x + 10