Let us see how to solve a system of linear equations in MATLAB. Here are the various operators that we will be deploying to execute our task :

**\ operator :**is the matrix division of A into B, which is roughly the same as`A \ B`

. If A is an NXN matrix and B is a column vector with N components or a matrix with several such columns, then`INV(A) * B`

is the solution to the equation`X = A \ B`

. A warning message is printed if A is badly scaled or nearly singular.**A * X = B**`A\EYE(SIZE(A))`

**linsolve operator :**

solves the linear system A * X = B using LU factorization with partial pivoting when A is square, and QR factorization with column pivoting. A warning is given if A is ill conditioned for square matrices and rank deficient for rectangular matrices.**X = LINSOLVE(A, B)**

**Example 1 : **Non-homogeneous System Ax = b, where A is a square and is invertible. In our example we will consider the following equations :

2x + y - z = 7 x -2y + 5z = -13 3x + 5y - 4z = 18

We will convert these equations into matrices A and b :

`% declaring the matrices based on the equations ` `A = [2 1 -1; 1 -2 5; 3 5 -4] ` `b = [7; -13; 18] ` |

*chevron_right*

*filter_none*

**Output :**

A = 2 1 -1 1 -2 5 3 5 -4 b = 7 -13 18

Now we will create an augmented matrix Ab. We will compare the ranks of Ab and A, if the ranks are equal then a unique solution exists.

`% creating augmented matrix ` `Ab = [A b] ` ` ` `% checking the ranks ` `if` `rank(A) == rank(Ab) ` ` ` `display(` `"Unique solution exists"` `) ` `else` ` ` `display(` `"Unique solution does not exist"` `) ` `end` |

*chevron_right*

*filter_none*

**Output :**

Ab = 2 1 -1 7 1 -2 5 -13 3 5 -4 18 Unique solution exists

Now we can find the solution to this system of equations by using 3 methods:

- conventional way :
`inv(A) * b`

- using mid-divide routine :
`A \ b`

- using linsolve routine :
`linsolve(A, b)`

`% conventional way of finding solution ` `x_inv = inv(A) * b ` ` ` `% using mid-divide routine of MATLAB ` `x_bslash = A \ b ` ` ` `% using linsolve routine of MATLAB ` `x_linsolve = linsolve(A, b) ` |

*chevron_right*

*filter_none*

**Output :**

x_inv = 2.0000e+00 8.8818e-16 -3.0000e+00 x_bslash = 2.0000e+00 9.6892e-16 -3.0000e+00 x_linsolve = 2.0000e+00 9.6892e-16 -3.0000e+00

We can verify the correctness of the solution by finding the error using `A * x - b`

. The error should be 0.

`% check for errors ` `Er1 = A * x_inv - b ` `Er2 = A * x_bslash - b ` `Er3 = A * x_linsolve - b ` |

*chevron_right*

*filter_none*

**Output :**

Er1 = -8.8818e-16 -3.5527e-15 0.0000e+00 Er2 = -1.7764e-15 -1.7764e-15 0.0000e+00 Er3 = -1.7764e-15 -1.7764e-15 0.0000e+00

As all the errors are close to 0, we can say that the solution is correct.

**Example 2 :** Non-homogeneous system Ax = b, where A is a square and it is not invertible. In our example we will consider the following equations :

2x + 4y + 6z = 7 3x -2y + 1z = 2 1x + 2y + 3z = 5

`% declaring the matrices based on the equations ` `A = [2 4 6; 3 -2 1; 1 2 3] ` `b = [7; 2; 5] ` ` ` `% creating augmented matrix ` `Ab = [A b] ` ` ` `% checking the ranks ` `if` `rank(A) == rank(Ab) ` ` ` `display(` `"Unique solution exists"` `) ` `else` ` ` `display(` `"Unique solution does not exist"` `) ` `end` ` ` `% conventional way of finding solution ` `% gives warning and wrong answer. ` `x_inv = inv(A) * b ` ` ` `% using mid-divide routine of MATLAB ` `% this too gives warning and wrong answer. ` `x_bslash = A \ b ` ` ` `% check for errors ` `Er1 = A * x_inv - b ` `Er2 = A * x_bslash - b ` |

*chevron_right*

*filter_none*

**Output :**

A = 2 4 6 3 -2 1 1 2 3 b = 7 2 5 Ab = 2 4 6 7 3 -2 1 2 1 2 3 5 Unique solution does not exist warning: matrix singular to machine precision warning: called from testing at line 17 column 7 x_inv = Inf Inf Inf warning: matrix singular to machine precision warning: called from testing at line 21 column 10 x_bslash = -Inf -Inf Inf Er1 = Inf NaN Inf Er2 = NaN NaN NaN

**Example 3 : **Non-homogeneous system Ax = b where A is not a square. In our example we will consider the following equations :

2a + c - d + e = 2 a + c - d + e = 1 12a + 2b + 8c + 2e = 12

`% declaring the matrices based on the equations ` `A = [2 0 1 -1 1; 1 0 1 -1 1; 12 2 8 0 2] ` `b = [2; 1; 12] ` ` ` `% creating augmented matrix ` `Ab = [A b] ` ` ` `% checking the ranks ` `if` `rank(A) == rank(Ab) ` ` ` `display(` `"Solution exists"` `) ` `else` ` ` `display(` `"Solution does not exist"` `) ` `end` ` ` `% checking for unique solution ` `if` `rank(A) == 5 ` ` ` `display(` `"Unique solution exists"` `) ` `else` ` ` `display(` `"Unique solution does not exist"` `) ` `end` |

*chevron_right*

*filter_none*

**Output :**

A = 2 0 1 -1 1 1 0 1 -1 1 12 2 8 0 2 b = 2 1 12 Ab = 2 0 1 -1 1 2 1 0 1 -1 1 1 12 2 8 0 2 12 Solution exists Unique solution does not exist

**Example 4 : **Homogeneous system Ax = 0 where A is a square and is invertible. In our example we will consider the following equations :

6x + 2y + 3z = 0 4x - y + 2z = 0 2x + y + 5z = 0

`% declaring the matrices based on the equations ` `A = [6 2 3; 4 -1 2; 2 1 5] ` `b = [0; 0; 0] ` ` ` `% checking for unique solution ` `if` `rank(A) == 3 ` ` ` `display(` `"Unique solution exists"` `) ` `else` ` ` `display(` `"Unique solution does not exist"` `) ` `endif ` ` ` `% trivial solution ` `x = A \ b ` ` ` `% getting a null set. ` `% this is obvious as A is invertible. ` `% so its null space contains only zero vector ` `x = null(A) ` |

*chevron_right*

*filter_none*

**Output :**

A = 6 2 3 4 -1 2 2 1 5 b = 0 0 0 Unique solution exists x = 0 0 0 x = [](3x0)

**Example 5 : **Homogeneous system Ax = 0 where A is a square and is not invertible. In our example we will consider the following equations :

1x + 2y + 3z = 0 4x + 5y + 6z = 0 7x + 8y + 9z = 0

`% declaring the matrices based on the equations ` `A = [1 2 3; 4 5 6; 7 8 9] ` `b = [0; 0; 0] ` ` ` `% checking for unique solution ` `if` `rank(A) == 3 ` ` ` `display(` `"Unique solution exists"` `) ` `else` ` ` `display(` `"Unique solution does not exist"` `) ` `endif ` ` ` `% trivial solution with warning ` `x = A \ b ` ` ` `% this will return a set containing ` `% only one basis vector of null space of A ` `% the null space of A is spanned by this vector ` `% hence this vector or its scalar multiple ` `% is the solution of the given homogeneous system ` `x = null(A) ` ` ` `% finding the errors ` `Err = A*x - b ` |

*chevron_right*

*filter_none*

**Output :**

A = 1 2 3 4 5 6 7 8 9 b = 0 0 0 Unique solution does not exist warning: matrix singular to machine precision, rcond = 1.54198e-18 warning: called from testing at line 13 column 3 x = 0 0 0 x = 0.40825 -0.81650 0.40825 Err = -1.3323e-15 -4.4409e-16 4.4409e-16

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the **CS Theory Course** at a student-friendly price and become industry ready.