Skip to content

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.

Before performing symbolic operations, you must declare your variables as symbolic using the syms command.

% Declare t and x as symbolic variables
syms t x
% Define a symbolic function f(t)
f = 3*t^2 + 2/t^2;
% Now f is a symbolic expression, not a numerical value

The diff command computes the derivative of a symbolic expression.

syms x
f = x^3 * sin(x);
% Compute the first derivative of f with respect to x
df_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).

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)

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.

Step 1: Define the function and find its first derivative.

syms x
f = 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 equation
stationary_points = solve(df == 0, x)
stationary_points =
0
4

Step 3: Use the second derivative test to classify these points.

d2f = diff(f, x, 2);
% Substitute the stationary points into the second derivative
second_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 Minimum

Step 4: Visualize the results.

% Modern plotting with fplot
fplot(f, [-2, 6]);
hold on; % Keep the current plot and add to it
% Evaluate the function at the stationary points
y_points = subs(f, x, stationary_points);
% Plot the maxima/minima as red circles
plot(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).

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.

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 condition
Dy = diff(y, t);
% Define the initial conditions
conds = [y(0) == 2, Dy(0) == -1];
% Define the same ODE
ode = diff(y, t, 2) - y == 0;
% Solve with the conditions
sol_specific = dsolve(ode, conds)
sol_specific =
(3*exp(-t))/2 + exp(t)/2

This provides the exact, unique solution that satisfies both the differential equation and the given initial state.