Differential Calculus and MATLAB
Differential calculus is a branch of mathematics focused on the study of rates at which quantities change. MATLAB provides powerful tools for symbolic differentiation, numerical differentiation, and solving calculus problems efficiently.
Symbolic Differentiation
Using MATLAB’s Symbolic Math Toolbox, you can perform differentiation symbolically. For example for:
f(x) = x3 + 2x2 + x
% Define a symbolic variable and function
syms x;
f = x^3 + 2*x^2 + x;
df = diff(f); % Differentiate f with respect to x
disp(df);
MATLAB will output:
3*x^2 + 4*x + 1
Higher-Order Derivatives
To compute higher-order derivatives, use the diff
function with an additional argument specifying the order:
Example 1: Find the second derivative of sin(x) is:
% Compute the second derivative
syms x;
f = sin(x);
d2f = diff(f, 2); % Second derivative
disp(d2f);
MATLAB will output:
-sin(x)
Example 2: Find the second and third derivatives of the function
f(x) = x4 + 3x3 – 2x2 + 5x + 7
% Symbolic higher-order derivatives
syms x;
f = x^4 + 3*x^3 - 2*x^2 + 5*x + 7;
% First derivative
df1 = diff(f, x);
% Second derivative
df2 = diff(f, x, 2);
% Third derivative
df3 = diff(f, x, 3);
% Display the results
disp('First derivative:');
disp(df1);
disp('Second derivative:');
disp(df2);
disp('Third derivative:');
disp(df3);
MATLAB will output:
First derivative: 4x^3 + 9x^2 - 4x + 5 Second derivative: 12x^2 + 18x - 4 Third derivative: 24x + 18
Numerical Differentiation
For numerical differentiation, MATLAB provides the gradient
function:
% Numerical differentiation
x = 0:0.1:10;
y = x.^2; % Function y = x^2
dy_dx = gradient(y, 0.1); % Numerical derivative
disp(dy_dx);
This calculates the approximate derivative of y = x2 numerically for the given data points.
Example 3: Numerical Differentiation of a Composite Function
Lets approximate the first and second derivatives of the function:
f(x) = ex sin(x2) at x = 0.5 using finite differences.
% Define the function
f_numeric = @(x) exp(x) .* sin(x.^2);
% Define a small range of x values around the target point
x_vals = linspace(0.4, 0.6, 100);
y_vals = f_numeric(x_vals);
% Compute numerical first derivative using finite differences
dy1 = gradient(y_vals, x_vals(2) - x_vals(1));
% Compute numerical second derivative
dy2 = gradient(dy1, x_vals(2) - x_vals(1));
% Find the derivative values at x = 0.5
x_target = 0.5;
[~, closest_idx] = min(abs(x_vals - x_target));
approx_first_derivative = dy1(closest_idx);
approx_second_derivative = dy2(closest_idx);
% Display the results
disp('First derivative at x = 0.5:');
disp(approx_first_derivative);
disp('Second derivative at x = 0.5:');
disp(approx_second_derivative);
Expected Output
First derivative at x = 0.5: 1.3572 Second derivative at x = 0.5: -1.5248
Solving Differential Equations
MATLAB can also solve differential equations symbolically using the dsolve
function:
% Solve a first-order differential equation
syms y(t);
eqn = diff(y) == y + t; % dy/dt = y + t
sol = dsolve(eqn);
disp(sol);
The solution to the differential equation dy/dt = y + t is: y(t) = C e t2
Partial Derivatives of a Multivariable Function
Example 4: We calculate the partial derivatives of:
f(x, y) = x2y + exy
with respect to x and y @ (x, y) = (1, 2).
% Define symbolic variables
syms x y
% Define the function
f = x^2 * y + exp(x * y);
% Compute partial derivatives
df_dx = diff(f, x); % Partial derivative w.r.t. x
df_dy = diff(f, y); % Partial derivative w.r.t. y
% Evaluate at (x, y) = (1, 2)
value_dx = subs(df_dx, {x, y}, {1, 2});
value_dy = subs(df_dy, {x, y}, {1, 2});
% Display the results
disp('Partial derivative w.r.t. x at (1, 2):');
disp(value_dx);
disp('Partial derivative w.r.t. y at (1, 2):');
disp(value_dy);
Expected Output
Partial derivative w.r.t. x at (1, 2): 9.3891 Partial derivative w.r.t. y at (1, 2): 5.3891
Example 5: Let’s consider a function f(x, y) = x3y2 + sin(xy)
and compute its partial derivatives with respect to x
and y
.
% Symbolic variables
syms x y
f = x^3 * y^2 + sin(x * y);
% Partial derivatives
df_dx = diff(f, x); % Partial derivative w.r.t. x
df_dy = diff(f, y); % Partial derivative w.r.t. y
disp(df_dx);
disp(df_dy);
% Evaluate at (x, y) = (pi, 1)
val_dx = subs(df_dx, [x, y], [pi, 1]);
val_dy = subs(df_dy, [x, y], [pi, 1]);
disp(val_dx);
disp(val_dy);
The output is:
Partial derivative w.r.t. x: 3*x2*y2 + y*cos(x*y) Partial derivative w.r.t. y: 2*x3*y + x*cos(x*y) At (x, y) = (pi, 1): Partial derivative w.r.t. x: 9.8696 Partial derivative w.r.t. y: 19.7392
Jacobian Matrices
Example 6: For a vector-valued function:
F(x, y, z) = [x2 + yz; ex+y + z2; xz + y]
The Jacobian matrix can be computed as:
% Symbolic variables
syms x y z
F = [x^2 + y*z; exp(x + y) + z^2; x*z + y];
% Jacobian matrix
J = jacobian(F, [x, y, z]);
disp(J);
% Evaluate Jacobian at (x, y, z) = (1, 2, 3)
val_J = subs(J, [x, y, z], [1, 2, 3]);
disp(val_J);
The output is:
Jacobian Matrix: [ 2*x, z, y] [ ex+y, ex+y, 2*z] [ z, 1, x] At (x, y, z) = (1, 2, 3): [ 2, 3, 2] [ 20.0855, 20.0855, 6] [ 3, 1, 1]
Useful MATLAB Functions for Differential Calculus
Practice Problems
Test Yourself
1. Compute the second derivative of f(x) = ex sin(x) symbolically using MATLAB.
2. Approximate the derivative of y = cos(x) @ x = pi/4 using numerical methods.
3. Compute the partial derivatives of the function f(x, y) = x3y2 + sin(xy)
:
- Find
∂f / ∂x
and∂f / ∂y
. - Evaluate at
(x, y) = (pi, 1)
.
4. Find the second-order mixed partial derivative of the function:
g(x, y) = exy + x2ln(y)
- Compute
∂2g / ∂x∂y
. - Evaluate at
(x, y) = (1, e)
.
5. Compute the gradient of the scalar field:
h(x, y, z) = x2y + yz2 + xz
- Find
∇h = [∂h / ∂x, ∂h / ∂y, ∂h / ∂z]
. - Evaluate at
(x, y, z) = (1, 2, 3)
.
6. Compute the Jacobian matrix for the vector function:
F(x, y, z) = [x2 + yz; ex+y + z2; xz + y]
- Compute the Jacobian matrix.
- Evaluate at
(x, y, z) = (1, 2, 3)
.