Skip to content

MATLAB - Linear Algebra

MATLAB, with its Symbolic Math Toolbox, provides powerful tools for solving, manipulating, and analyzing algebraic expressions symbolically. This allows you to work with mathematical expressions as you would on paper, without being restricted to numerical values.

This tutorial focuses on the modern, recommended workflow using symbolic variables.

While MATLAB has a built-in Symbolic Math Toolbox, Octave requires the external ‘symbolic’ package (pkg load symbolic). The syntax can also differ. This tutorial primarily covers the modern MATLAB syntax. For solving polynomial equations numerically, Octave’s roots function is equivalent to MATLAB’s.

Creating Symbolic Variables and Expressions

Section titled “Creating Symbolic Variables and Expressions”

The first step in any symbolic workflow is to declare the variables you will be using as symbolic. The syms command is the most convenient way to do this.

% Declare symbolic variables x and y
syms x y
% Create a symbolic expression
expr = (x - y) * (x + y)

The solve function is the primary tool for solving symbolic equations. The modern approach is to define equations using the double equals sign ==.

syms x
% Define a quadratic equation
eq = x^2 - 7*x + 12 == 0;
% Solve the equation for x
sol = solve(eq, x)
% Expected Output:
sol =
3
4

If you don’t specify the right side, solve assumes the expression is equal to zero.

syms a b t
% Solve 'a*t + b = 0' for the variable 't'
sol_t = solve(a*t + b, t)
% Expected Output:
sol_t =
-b/a

You can solve a system of equations by providing them as a vector.

syms u v
% Define the system of equations
eqs = [5*u + 2*v == 1, u - v == 4];
% Solve for u and v. The solution is returned as a structure.
sol = solve(eqs, [u, v])
% Access the solutions
disp(['Solution for u: ', char(sol.u)])
disp(['Solution for v: ', char(sol.v)])
% Expected Output:
sol =
struct with fields:
u: [1x1 sym]
v: [1x1 sym]
Solution for u: 9/7
Solution for v: -19/7

For equations without simple symbolic solutions, solve may return a complex expression. You can get a numeric approximation using the double function.

syms x
eq = x^5 + 3*x - 1 == 0;
sol = solve(eq, x);
% Convert the symbolic solution to a numeric (double-precision) value
numeric_sol = double(sol)
% Expected Output (showing one real root):
numeric_sol =
0.3328

The toolbox provides functions to expand, factor, and simplify expressions, which are essential for algebraic manipulation.

syms x y
% --- Expand an expression ---
expr_to_expand = (x + 3)^2 * (y - 1);
expanded_expr = expand(expr_to_expand)
% Output: x^2*y - x^2 + 6*x*y - 6*x + 9*y - 9
% --- Factor an expression ---
expr_to_factor = x^3 + y^3;
factored_expr = factor(expr_to_factor)
% Output: (x + y)*(x^2 - x*y + y^2)
% --- Simplify an expression ---
expr_to_simplify = (x^2 - 1) / (x - 1);
simplified_expr = simplify(expr_to_simplify)
% Output: x + 1
% --- Collect terms with respect to a variable ---
expr_to_collect = x*y + y*x^2 - x*z - z*x^2;
% Collect coefficients of x
collected_expr = collect(expr_to_collect, x)
% Output: (y - z)*x^2 + (y - z)*x

Solving Numeric Systems: The Backslash Operator (\)

Section titled “Solving Numeric Systems: The Backslash Operator (\)”

For systems of linear equations where you have numeric coefficients, using matrix division with the backslash operator (\) is far more efficient than the symbolic solve function.

To solve the system Ax = b:

% System of equations:
% 2x + 4y - z = 10
% 3x - y + 2z = 5
% x + 2y + 3z = 12
% Coefficient matrix A
A = [2, 4, -1;
3, -1, 2;
1, 2, 3];
% Right-hand side vector b
b = [10; 5; 12];
% Solve for x (a vector containing [x; y; z])
x_sol = A \ b
% Expected Output:
x_sol =
2.5926
1.6296
1.5185
  • Use syms: Always declare your variables with syms for modern symbolic workflows.
  • Use == for Equations: Define equations for solve using the double equals sign.
  • Choose the Right Tool: Use the symbolic solve for symbolic problems and the numeric \ operator for linear systems with numeric coefficients. The latter is much faster.
  • Simplify Results: Use simplify or expand to make your symbolic results more readable.