MATLAB - Differential
MATLAB - Symbolic Differentiation and Calculus
Section titled “MATLAB - Symbolic Differentiation and Calculus”The Symbolic Math Toolbox™ in MATLAB allows you to perform calculus computations symbolically, rather than numerically. This means you can work with mathematical expressions and obtain exact analytical results. This chapter covers symbolic differentiation, finding maxima/minima, and solving differential equations.
Note: The functionality described requires the Symbolic Math Toolbox. Open-source alternatives like GNU Octave have a symbolic package that offers similar features, but the syntax and capabilities may differ.
Creating Symbolic Variables and Functions
Section titled “Creating Symbolic Variables and Functions”Before performing symbolic operations, you must declare your variables as symbolic using the syms command.
% Declare t and x as symbolic variablessyms t x
% Define a symbolic function f(t)f = 3*t^2 + 2/t^2;
% Now f is a symbolic expression, not a numerical valueComputing Derivatives
Section titled “Computing Derivatives”The diff command computes the derivative of a symbolic expression.
First-Order Derivatives
Section titled “First-Order Derivatives”syms xf = x^3 * sin(x);
% Compute the first derivative of f with respect to xdf_dx = diff(f, x)
df_dx =
3*x^2*sin(x) + x^3*cos(x)If the expression contains only one symbolic variable, you can omit it from the diff command: diff(f).
Higher-Order Derivatives
Section titled “Higher-Order Derivatives”To compute higher-order derivatives, provide the order as an additional argument. Let’s find the second derivative of f.
% Compute the second derivative of f (d^2f/dx^2)d2f_dx2 = diff(f, x, 2)
d2f_dx2 =
6*x*sin(x) + 6*x^2*cos(x) - x^3*sin(x)Application: Finding Maxima and Minima
Section titled “Application: Finding Maxima and Minima”A classic application of differentiation is finding the local maxima and minima of a function. This involves finding the stationary points (where the first derivative is zero) and then using the second derivative test to classify them.
Example: Analyze f(x) = x³ - 6x² + 5
Section titled “Example: Analyze f(x) = x³ - 6x² + 5”Step 1: Define the function and find its first derivative.
syms xf = x^3 - 6*x^2 + 5;df = diff(f, x)Step 2: Find the stationary points by solving df/dx = 0.
% The solve command finds the roots of the symbolic equationstationary_points = solve(df == 0, x)
stationary_points =
0 4Step 3: Use the second derivative test to classify these points.
d2f = diff(f, x, 2);
% Substitute the stationary points into the second derivativesecond_deriv_vals = subs(d2f, x, stationary_points)
second_deriv_vals =
-6 % At x=0, d2f < 0 -> Local Maximum 6 % At x=4, d2f > 0 -> Local MinimumStep 4: Visualize the results.
% Modern plotting with fplotfplot(f, [-2, 6]);hold on; % Keep the current plot and add to it
% Evaluate the function at the stationary pointsy_points = subs(f, x, stationary_points);
% Plot the maxima/minima as red circlesplot(stationary_points, y_points, 'ro', 'MarkerFaceColor', 'r');
title('Function with Local Maxima and Minima');xlabel('x');ylabel('f(x)');legend('f(x)', 'Stationary Points');grid on;hold off;This modern approach using fplot and plot provides a clear and informative visualization of the analytical results.
Solving Ordinary Differential Equations (ODEs)
Section titled “Solving Ordinary Differential Equations (ODEs)”The dsolve command finds symbolic solutions to ordinary differential equations. The derivative of a function y(t) is represented as diff(y,t).
Example 1: General Solution
Section titled “Example 1: General Solution”Let’s solve the second-order ODE: y” - y = 0.
syms y(t)
% Define the equation using ==ode = diff(y, t, 2) - y == 0;
% Solve for y(t)sol_general = dsolve(ode)
sol_general =
C1*exp(-t) + C2*exp(t)The solution includes arbitrary constants C1 and C2, representing the family of functions that satisfy the ODE.
Example 2: Initial Value Problem
Section titled “Example 2: Initial Value Problem”To find a specific solution, we can provide initial conditions. Let’s solve the same ODE with y(0) = 2 and y’(0) = -1.
syms y(t)
% Define the derivative for the initial conditionDy = diff(y, t);
% Define the initial conditionsconds = [y(0) == 2, Dy(0) == -1];
% Define the same ODEode = diff(y, t, 2) - y == 0;
% Solve with the conditionssol_specific = dsolve(ode, conds)
sol_specific =
(3*exp(-t))/2 + exp(t)/2This provides the exact, unique solution that satisfies both the differential equation and the given initial state.