MATLAB - Derivatives of Polynomials
MATLAB - Computing Derivatives
Section titled “MATLAB - Computing Derivatives”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.
Modern Approach: Symbolic Derivatives
Section titled “Modern Approach: Symbolic Derivatives”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.
Key Functions: syms and diff
Section titled “Key Functions: syms and diff”syms: This command declares symbolic variables. For example,syms xtells MATLAB to treatxas a mathematical symbol rather than a numerical variable.diff(f, var): This function computes the derivative of a symbolic expressionfwith respect to a symbolic variablevar.
Example: Derivative of a Polynomial
Section titled “Example: Derivative of a Polynomial”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 variablesyms x
% 2. Define the symbolic polynomial expressionP_x = 4*x^3 + 3*x^2 + 2*x + 1;
% 3. Compute the first derivative with respect to xdP_dx = diff(P_x, x);
% 4. Display the resultdisp('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 + 2You 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.
Syntax
Section titled “Syntax”% Derivative of a single polynomialk = 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)Example 1: Single Polynomial with polyder
Section titled “Example 1: Single Polynomial with polyder”Using the same polynomial: $$P(x) = 4x^3 + 3x^2 + 2x + 1$$
% The polynomial is represented by its coefficient vectorp_coeffs = [4 3 2 1];
% Compute the derivative's coefficientsk_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$$.
Example 2: Derivative of a Product
Section titled “Example 2: Derivative of a Product”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 polynomialsa = [3 2 1];b = [1 0 -4]; % Note the 0 for the missing x term
% Compute the coefficients of the derivative of the productk = 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,
polyderis a fast and efficient choice. - For numerical data (e.g., from an experiment), use the numerical
difffunction, which calculates differences between adjacent elements. This approximates the derivative but is fundamentally different from symbolic differentiation.