Simultaneous Equations Solver in MATLAB
For most numerical linear systems A*x = b, use MATLAB's backslash operator: x = A\b. It solves the system without explicitly forming A⁻¹. Check the residual norm before relying on the result.
Written and checked by TheToolNet Editorial Team · Last reviewed: August 21, 2026 · Editorial policy
The backslash operator calls mldivide. MathWorks recommends it for most numerical linear systems.
Solve a numerical linear system
For 2x + 3y = 13 and x − y = −1, put coefficients in A and constants in b:
A = [2 3; 1 -1];
b = [13; -1];
x = A\bMATLAB returns a column vector with x = 2 and y = 3. Semicolons create new rows, spaces separate columns, and the row order in b must match the equation order in A.
A 3×3 system uses exactly the same pattern:
A = [1 1 1; 2 -1 1; 1 2 -1];
b = [6; 3; 2];
x = A\bThis returns [1; 2; 3].
Verify the MATLAB solution
Calculate the residual vector and its norm:
residual = A*x - b;
residualNorm = norm(residual)For an ordinary well-conditioned system, the residual norm should be close to zero, allowing for floating-point rounding. A small residual shows that A*x is close to b; it does not by itself guarantee that a badly conditioned system has an accurate x. Check rcond(A) for a square matrix when conditioning matters.
Backslash, linsolve or symbolic solve?
| MATLAB method | Use it for | Example |
|---|---|---|
A\b | Most numerical linear systems; MATLAB chooses an appropriate factorization. | x = A\b |
linsolve(A,b) | Numerical systems when you want its optional reciprocal-condition or rank output, or can safely specify matrix properties. | [x,r] = linsolve(A,b) |
solve | Symbolic equations, exact parameters, or expressions that are not already written as A*x = b. Requires Symbolic Math Toolbox. | [x,y] = solve(eqns,[x y]) |
inv(A)*b for routine numerical solving. MathWorks explains that backslash solves A*x = b without explicitly computing the inverse, which is generally less work and avoids extra finite-precision error.Singular and rectangular systems
If A is square but singular or nearly singular, MATLAB may issue a warning and return a basic or approximate result depending on the matrix. Do not treat that output as a unique solution without checking rank and residual:
rankA = rank(A);
rankAugmented = rank([A b]);If rank(A) is smaller than rank([A b]), the system is inconsistent. If the ranks match but are smaller than the number of unknowns, there are free variables and infinitely many exact solutions.
Backslash also accepts rectangular matrices. For an overdetermined system it ordinarily returns a least-squares solution; that is a best fit, not necessarily a vector that makes every residual zero. Always interpret the dimensions and residual in context.
Common MATLAB problems
| Problem | Check |
|---|---|
| Incorrect matrix dimensions | A and b must have the same number of rows for A\b. |
| Unexpected one-row result | Use semicolons in b to create a column vector: [13; -1], not [13 -1]. |
| Singular-matrix warning | Compare ranks and inspect the residual; the system may lack a unique solution. |
| Symbolic variables are undefined | Create them with syms and confirm Symbolic Math Toolbox is available. |
| Tiny non-zero residuals | Floating-point rounding is normal; judge the scale instead of testing exact equality. |
Frequently asked questions
How do I solve simultaneous equations in MATLAB?
For a numerical linear system, put coefficients in A, constants in b, and run x = A\b. Verify with norm(A*x − b).
Why is backslash better than inv(A)*b?
Backslash solves the system directly without explicitly computing A inverse. This generally requires less work and avoids extra numerical error.
Can MATLAB solve nonlinear simultaneous equations?
Yes, but A\b is only for linear systems. Depending on the problem and installed toolboxes, symbolic solve, numerical vpasolve, or an optimization/root-finding function may be appropriate.
Official MATLAB references
For visible row operations, return to the online simultaneous equations solver. Spreadsheet users can follow the Excel guide.