MATLAB - Polynomial Multiplication
MATLAB - Modern Polynomial Operations
Section titled “MATLAB - Modern Polynomial Operations”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 - 7P = [2 0 5 -7]; % Note the explicit zero for the missing x^2 term!Polynomial Multiplication with conv
Section titled “Polynomial Multiplication with conv”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 vectorsP = [3 2]; % Represents 3x + 2Q = [1 -4 5]; % Represents x^2 - 4x + 5
% Perform the multiplication using convolutionR_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 10This 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 polynomialsA = [1 2 3];B = [4 5 6];
% Perform the polynomial multiplicationC = 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 18Verification and Testing Your Results
Section titled “Verification and Testing Your Results”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 1P = [3 2];Q = [1 -4 5];R_coeffs = conv(P, Q); % [3 -10 7 10]
% Choose a test point, e.g., x = 2x_test = 2;
% Evaluate P(2) and Q(2)val_P = polyval(P, x_test); % 3*2 + 2 = 8val_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 matchif abs(val_R - (val_P * val_Q)) < 1e-9 % Use a tolerance for floating point disp('Verification successful!');else disp('Verification failed!');endThis programmatic check provides confidence in your calculations and is a great debugging technique.
Beyond Multiplication: A Suite of Tools
Section titled “Beyond Multiplication: A Suite of Tools”MATLAB offers a complete set of tools for polynomial analysis:
deconv(R, P): Polynomial division. Finds the result ofR(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 degreento 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.
Alternative: Symbolic Math Toolbox
Section titled “Alternative: Symbolic Math Toolbox”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 symbolicallyP_sym = 3*x + 2;Q_sym = x^2 - 4*x + 5;
% Multiplication is now intuitiveR_sym = P_sym * Q_sym;
% Expand to see the full polynomialexpand(R_sym)MATLAB will return:
ans =
3*x^3 - 10*x^2 + 7*x + 10