Symbolic Math in MATLAB
Symbolic math in MATLAB allows you to perform mathematical operations symbolically, which means that variables and expressions are manipulated in their symbolic form rather than as numeric values. This is especially useful for algebra, calculus, solving equations, and more.
Basics of Symbolic Math in Matlab
To use symbolic math in MATLAB, you need the Symbolic Math Toolbox
. You can create symbolic variables using the syms
function:
% Creating symbolic variables
syms x y z
disp(x);
disp(y);
disp(z);
x y z
Basic Symbolic Operations
Once you have symbolic variables, you can perform algebraic operations:
% Basic symbolic operations
syms a b
f = a^2 + 2*a*b + b^2; % Expand a polynomial
disp(f);
a^2 + 2*a*b + b^2
Solving Equations
You can solve equations symbolically using the solve
function. Here is an example:
% Solving an equation
syms x
eqn = x^2 - 4 == 0;
sol = solve(eqn, x);
disp(sol);
2 -2
Calculus with Symbolic Math
Symbolic math is extremely powerful for calculus operations like differentiation and integration:
Differentiation
% Differentiation
syms x
f = x^3 + 2*x^2 - x + 5;
df = diff(f, x); % First derivative
disp(df);
3*x^2 + 4*x - 1
Integration
% Integration
syms x
f = x^3 + 2*x^2 - x + 5;
int_f = int(f, x); % Indefinite integral
disp(int_f);
(1/4)*x^4 + (2/3)*x^3 - (1/2)*x^2 + 5*x
Working with Matrices Symbolically
You can also perform matrix operations with symbolic variables:
% Symbolic matrix operations
syms a b c d
A = [a b; c d];
B = inv(A); % Finding the inverse of a symbolic matrix
disp(B);
[ d/(a*d - b*c), -b/(a*d - b*c)] [-c/(a*d - b*c), a/(a*d - b*c)]
Plotting Symbolic Expressions
You can plot symbolic expressions for better visualization using fplot
:
% Plotting symbolic expressions
syms x
f = sin(x) / x;
fplot(f, [-10, 10]);
Advanced Example Problems in Symbolic Math
Below are more complex problems solved using MATLAB’s symbolic math capabilities. These examples demonstrate how symbolic math can be applied to advanced mathematical operations.
Example 1: Solving a System of Equations
Solve the system of linear equations:
2x + y - z = 3
x - 2y + 4z = -6
-3x + y + z = 2
% Solving a system of equations
syms x y z
eq1 = 2*x + y - z == 3;
eq2 = x - 2*y + 4*z == -6;
eq3 = -3*x + y + z == 2;
solutions = solve([eq1, eq2, eq3], [x, y, z]);
disp(solutions.x);
disp(solutions.y);
disp(solutions.z);
x = 1 y = -1 z = -2
Example 2: Solving a Nonlinear Equation
Solve the equation ex - 3x = 0
.
\[ e^x – 3x = 0 \]
% Solving a nonlinear equation
syms x
eqn = exp(x) - 3*x == 0;
sol = solve(eqn, x);
disp(sol);
x = LambertW(3)
The solution involves the Lambert W function, which is defined as the inverse function of f(w) = w * ew
.
Example 3: Eigenvalues and Eigenvectors
Find the eigenvalues and eigenvectors of the symbolic matrix:
A = [2 -1 0; -1 2 -1; 0 -1 2]
% Finding eigenvalues and eigenvectors
syms a b c
A = [2 -1 0; -1 2 -1; 0 -1 2];
[eigVec, eigVal] = eig(A);
disp(eigVec); % Eigenvectors
disp(eigVal); % Eigenvalues
Eigenvalues: 0.5858 2 3.4142 Eigenvectors: [ 0.5, -0.7071, 0.5 ] [ 0.7071, 0, -0.7071 ] [ 0.5, 0.7071, 0.5 ]
Example 4: Taylor Series Expansion
Find the Taylor series expansion of f(x) = sin(x)
around x = 0
up to the 5th order.
% Taylor series expansion
syms x
f = sin(x);
taylor_series = taylor(f, x, 'Order', 6);
disp(taylor_series);
x - (1/6)*x^3 + (1/120)*x^5
Example 5: Definite Integral
Evaluate the integral ∫0π x sin(x) dx
.
\[ \int_0^\pi x \sin(x) \, dx \]
% Definite integral
syms x
f = x*sin(x);
result = int(f, x, 0, pi);
disp(result);
2
Example 6: Solving Differential Equations
\[ \frac{dy}{dx} + y = e^x \]
Solve the differential equation dy/dx + y = ex
with the initial condition y(0) = 1
.
% Solving a differential equation
syms y(x)
Dy = diff(y, x);
eqn = Dy + y == exp(x);
sol = dsolve(eqn, y(0) == 1);
disp(sol);
y(x) = e^x + C*e^(-x) where C is constant
Example 7: Laplace Transform
Find the Laplace Transform of f(t) = t e-2t
.
\[ f(t) = t e^{-2t} \]
% Laplace Transform
syms t s
f = t*exp(-2*t);
laplace_f = laplace(f, t, s);
disp(laplace_f);
1/(s + 2)^2
Example 8: Fourier Transform
Compute the Fourier Transform of f(t) = e-t u(t)
, where u(t)
is the unit step function.
\[ f(t) = e^{-t} u(t) \]
% Fourier Transform
syms t w
f = exp(-t)*heaviside(t);
fourier_f = fourier(f, t, w);
disp(fourier_f);
1/(1 + j*w)
Useful MATLAB Functions for Symbolic Math
Practice Questions
Test Yourself
1. Solve the quadratic equation x2 + 3x - 4 = 0
symbolically.
2. Find the first and second derivatives of f(x) = 4x3 - 2x2 + 7
.
3. Compute the indefinite and definite integral of g(x) = e-x
over [0, ∞]
.
4. Find the inverse of a 2×2 symbolic matrix.
5. Plot the symbolic function h(x) = sin(x2)
over [0, 2π]
.
6. Solve the system of nonlinear equations:
x2 + y2 = 1
x - y = 0.5
7. Find the Taylor series expansion of cos(x)
around x = π/4
up to the 4th order.
8. Solve the differential equation d2y/dx2 - 3dy/dx + 2y = 0
with initial conditions y(0) = 2
and dy/dx(0) = -1
.
9. Compute the Laplace Transform of t2e-3t
.
10. Evaluate the integral ∫-∞∞ e-x2 dx
.