Skip to content

MATLAB - Derivatives of Polynomials

In calculus, the derivative represents the instantaneous rate of change of a function. It’s a cornerstone of science and engineering, used to model everything from velocity and acceleration to optimization and sensitivity analysis.

MATLAB offers powerful and distinct methods for computing derivatives, depending on whether you are working with symbolic expressions or numerical data.

The most flexible and intuitive way to find an analytical derivative is by using the Symbolic Math Toolbox™. This approach allows you to work with mathematical expressions directly, just as you would on paper.

  • syms: This command declares symbolic variables. For example, syms x tells MATLAB to treat x as a mathematical symbol rather than a numerical variable.
  • diff(f, var): This function computes the derivative of a symbolic expression f with respect to a symbolic variable var.

Let’s find the derivative of the polynomial: $$P(x) = 4x^3 + 3x^2 + 2x + 1$$

Here is the modern workflow in MATLAB:

% 1. Declare 'x' as a symbolic variable
syms x
% 2. Define the symbolic polynomial expression
P_x = 4*x^3 + 3*x^2 + 2*x + 1;
% 3. Compute the first derivative with respect to x
dP_dx = diff(P_x, x);
% 4. Display the result
disp('The polynomial is:')
disp(P_x)
disp('The derivative is:')
disp(dP_dx)

MATLAB will output the analytical result:

The polynomial is:
4*x^3 + 3*x^2 + 2*x + 1
The derivative is:
12*x^2 + 6*x + 2

You can also compute higher-order derivatives. For the second derivative, you would use diff(P_x, x, 2).

Specialized Tool: polyder for Coefficient Vectors

Section titled “Specialized Tool: polyder for Coefficient Vectors”

For applications focused purely on polynomial manipulation, MATLAB provides the polyder function. It operates on vectors of coefficients, where the vector [c1, c2, ..., cn] represents the polynomial $$c_1x^{n-1} + c_2x^{n-2} + … + c_n$$. This can be efficient but is less intuitive than the symbolic approach.

% Derivative of a single polynomial
k = polyder(p)
% Derivative of the product of two polynomials a(x) * b(x)
k = polyder(a, b)
% Derivative of the quotient of two polynomials a(x) / b(x)
[q, d] = polyder(a, b)

Using the same polynomial: $$P(x) = 4x^3 + 3x^2 + 2x + 1$$

% The polynomial is represented by its coefficient vector
p_coeffs = [4 3 2 1];
% Compute the derivative's coefficients
k_coeffs = polyder(p_coeffs);
disp('Original coefficients:');
disp(p_coeffs);
disp('Derivative coefficients:');
disp(k_coeffs);

The output vector k_coeffs will be [12 6 2], which represents the derivative polynomial $$12x^2 + 6x + 2$$.

Let’s find the derivative of the product of $$a(x) = 3x^2 + 2x + 1$$ and $$b(x) = x^2 - 4$$.

% Define coefficient vectors for the two polynomials
a = [3 2 1];
b = [1 0 -4]; % Note the 0 for the missing x term
% Compute the coefficients of the derivative of the product
k = polyder(a, b);
disp('Derivative of product coefficients:');
disp(k);
% Result: [12 6 -22 -8]

This result [12 6 -22 -8] corresponds to the polynomial $$12x^3 + 6x^2 - 22x - 8$$.

Best Practices and Choosing the Right Method

Section titled “Best Practices and Choosing the Right Method”
  • For analytical work and clarity, always prefer the Symbolic Math Toolbox (syms, diff). It’s more readable and handles complex, non-polynomial functions.
  • For performance-critical code that only deals with polynomials represented by coefficients, polyder is a fast and efficient choice.
  • For numerical data (e.g., from an experiment), use the numerical diff function, which calculates differences between adjacent elements. This approximates the derivative but is fundamentally different from symbolic differentiation.