MATLAB - Linear Algebra
MATLAB - Symbolic Algebra
Section titled “MATLAB - Symbolic 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.
Note for GNU Octave Users
Section titled “Note for GNU Octave Users”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 ysyms x y
% Create a symbolic expressionexpr = (x - y) * (x + y)Solving Algebraic Equations with solve
Section titled “Solving Algebraic Equations with solve”The solve function is the primary tool for solving symbolic equations. The modern approach is to define equations using the double equals sign ==.
Solving a Single Equation
Section titled “Solving a Single Equation”syms x
% Define a quadratic equationeq = x^2 - 7*x + 12 == 0;
% Solve the equation for xsol = solve(eq, x)
% Expected Output:sol = 3 4If 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/aSolving Systems of Equations
Section titled “Solving Systems of Equations”You can solve a system of equations by providing them as a vector.
syms u v
% Define the system of equationseqs = [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 solutionsdisp(['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/7Solution for v: -19/7Handling Numerical Solutions
Section titled “Handling Numerical Solutions”For equations without simple symbolic solutions, solve may return a complex expression. You can get a numeric approximation using the double function.
syms xeq = x^5 + 3*x - 1 == 0;sol = solve(eq, x);
% Convert the symbolic solution to a numeric (double-precision) valuenumeric_sol = double(sol)
% Expected Output (showing one real root):numeric_sol = 0.3328Manipulating Expressions
Section titled “Manipulating Expressions”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 xcollected_expr = collect(expr_to_collect, x)% Output: (y - z)*x^2 + (y - z)*xSolving 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 AA = [2, 4, -1; 3, -1, 2; 1, 2, 3];
% Right-hand side vector bb = [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.5185Best Practices
Section titled “Best Practices”- Use
syms: Always declare your variables withsymsfor modern symbolic workflows. - Use
==for Equations: Define equations forsolveusing the double equals sign. - Choose the Right Tool: Use the symbolic
solvefor symbolic problems and the numeric\operator for linear systems with numeric coefficients. The latter is much faster. - Simplify Results: Use
simplifyorexpandto make your symbolic results more readable.