Solve Algebraic Systems

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Linear Systems of Equations

When solving a linear system of symbolic equations, the general solver returns a set of solutions:

S := solve([x + y = 1, 3*x - 2*y = 5], [x, y])

Assign individual solutions to variables using the | or evalAt operator:

xSol := x | S[1];
ySol := y | S[1];

The function linsolve returns a list of solutions:

linsolve([x + y = 1, 3*x - 2*y = 5], [x, y])

If there are more unknowns than independent equations in a system, linsolve solves the system for the first unknowns:

linsolve([x + y = a, 3*x - 2*y = b], [x, y, a, b])

Providing the unknowns in different order affects the solution:

linsolve([x + y = a, 3*x - 2*y = b], [a, b, x, y])

Linear Systems in a Matrix Form

To state the problem of solving the system of linear equations in a matrix form, use the following steps:

  1. Create a matrix A containing the coefficients of the terms of linear equations. Each equation contributes to a row in A.

  2. Create a column vector containing the right sides of the equations.

  3. The matrix form of the linear system is . When solving a system in a matrix form, you provide a matrix A and a vector . The solver returns the solutions of the system as a vector .

The dimensions m×n of the coefficient matrix define the following types of linear systems.

m = nSquare systemIf the determinant of A is not a zero, a unique solution of the system exists. Otherwise, the system has either infinitely many solutions or no solutions.
m > nOverdetermined systemThe system includes more equations than variables. The system can have one solution, infinitely many solutions, or no solutions.
m < nUnderdetermined systemThe system includes more variables than equations. The system has either infinitely many solutions or no solutions.

To solve a linear system in a matrix form, use the linalg::matlinsolve command. For example, solve the following system of linear equations:

eqn1 := 2*x + 3*y = 4:
eqn2 := 3*x - 2*y = 1:

To convert the system to a matrix form, use the matrix command to create a matrix of coefficients and a vector containing right sides of equations:

A := matrix([[2, 3],[3, -2]]);
b := matrix([4, 1])

As a shortcut for converting a system of linear equations to a matrix form, use linalg::expr2Matrix:

Ab := linalg::expr2Matrix([eqn1, eqn2], [x,y])

Now, use linalg::matlinsolve to solve the system:

linalg::matlinsolve(Ab)

Alternatively, split the matrix Ab into a matrix of coefficients A and a vector b containing the right sides of equations. Use linalg::matlinsolve to solve the system:

A := Ab[1..2, 1..2]:
b := Ab[1..2, 3..3]:
linalg::matlinsolve(A, b)

If your linear system is originally defined by a matrix equation, using the matrix form to solve the system is more intuitive. Also, the matrix form is convenient for solving equations with many variables because it avoids creating symbols for these variables. For example, the following matrices define a linear system:

A := linalg::hilbert(10);
b := matrix([i^(-2) $ i = 1..10])

To solve this system, use linalg::matlinsolve:

linalg::matlinsolve(A, b)

Specialized Matrices

If your system of linear equations can be presented as a specialized matrix, you can solve the system by calling a special solver. Direct calls to the special solvers often improve the performance of your code. MuPAD® offers special solvers for linear systems that can be represented by matrices of the following types:

  • A matrix given by , where L is a lower triangular matrix, and U is an upper triangular matrix (LU-decomposition of a matrix)

  • Toeplitz matrix. For example, the following matrix is a Toeplitz matrix:

    .

    See linalg::toeplitz for the definition and details.

  • Vandermonde matrix. For example, the following matrix is a Vandermonde matrix::

    .

    See linalg::vandermonde for the definition and details.

Suppose you want to solve the following system given by a Toeplitz matrix. Use linalg::toeplitz to define the system:

T := linalg::toeplitz(3, [0, 2, 5, 3, 0])

The vector y defines the right sides of equations:

y := matrix([1, 2, 3])

To solve a system given by a Toeplitz matrix, use the linalg::toeplitzSolve special solver. This special solver is more efficient than linalg::matlinsolve. This solver accepts a vector or a list of diagonal elements t of a Toeplitz matrix instead of a Toeplitz matrix itself:

t := [0, 2, 5, 3, 0]:
x := linalg::toeplitzSolve(t, y)

For the list of special solvers available in MuPAD, see Choosing Solver. For information on linear algebra functions, see Linear Algebra.

Nonlinear Systems

To solve a system of nonlinear equations symbolically, use the general solver. For example, solve the following system of trigonometric equations:

solve({4*cos(x) + 2*cos(y) = 3, 2*sin(x) + sin(y) = 1}, [x, y])

When you use the VectorFormat option, the solver returns the solutions as a set of vectors. If you want the solver to return one solution from the set, use the PrincipalValue option. When you use PrincipalValue, the solver still returns a set, although that set contains only one solution:

solve({4*cos(x) + 2*cos(y) = 3, 2*sin(x) + sin(y) = 1},
                           [x, y], VectorFormat, PrincipalValue)

You can also approximate the exact symbolic solution numerically:

float(%)

If solve cannot compute explicit solutions for a system, use numeric methods to approximate the solutions. For nonlinear systems, MuPAD offers the following special solvers:

System of Polynomial Equations

Suppose you want to solve the following system of polynomial equations:

eqn1 := x^3 + x^2*y - 1:
eqn2 := x^2 - y^2 = 1/2:

The general symbolic solver produces the result in terms of the RootOf object:

solve({eqn1, eqn2}, [x, y])

To approximate the solutions of the system of polynomial equations numerically, use the special solver numeric::polysysroots:

numeric::polysysroots({eqn1, eqn2}, [x, y])

System of Arbitrary Nonlinear Equations

Suppose you want to solve the following system of equations:

eqn1 := sin(a) - cos(b) + a*b:
eqn2 := (sin(a) + cos(b))^3:

The general solver cannot find a symbolic solution:

solve({eqn1, eqn2}, [a, b])

For numeric approximations of the solutions of the nonlinear system of equations, use the numeric::fsolve. The numeric solver returns only one solution:

numeric::fsolve({eqn1, eqn2}, [a, b])

Note

When numeric::fsolve finds one solution, it stops looking for other solutions.

When you solve an arbitrary nonlinear system numerically, there is no general way to find all solutions. For more information, see Solving Equations Numerically.