## Cramer's Rule

Back to Programming

### Description

This method is used to solve the system of linear equations. It is an explicit formula to solve the linear equations with as many unknowns as equations and this method is valid when the system has a unique solution.  The solution is expressed in terms of determinants of the coefficient matrix and of the matrices which are obtained from it after replacing one column by the columns of its right side of that equation.

DERIVATION

For a matrix A,

then the solution of the equations is unique.

Let consider an equation AX = b

Where

Then multiplying each equation by A1j, A2j, ... , Anj respectively, and adding all equations we get

Since

And

We finally get

Hence,

The total number of multiplications and divisions needed for solving a system of n linear equations by Cramer’s rule is (n + 1)! (n - 1) + n.

### Algorithm

INPUT:

A system of linear equations

OUTPUT:

The values of the unknowns.

PROCESS:

``````Step 1: [taking inputs from the user]
for i = 0 to 2 repeat
for j = 0 to 3 repeat
[End of ‘for’ loop]
[End of ‘for’ loop]

Step 2: [Cramer’s loop]
for i = 0 to 2 repeat
for j = 0 to 2 repeat
Set d[i][j] ← mat[i][j]
[End of ‘for’ loop]
[End of ‘for’ loop]
for i = 0 to 2 repeat
Set d1[i][0] ← mat[i][3]
Set d1[i][1] ← mat[i][1]
Set d1[i][2] ← mat[i][2]
[End of ‘for’ loop]
for i = 0 to 2 repeat
Set d2[i][0] ← mat[i][0]
Set d2[i][1] ← mat[i][3]
Set d2[i][2] ← mat[i][2]
[End of ‘for’ loop]
for i = 0 to 2 repeat
Set d3[i][0] ← mat[i][0]
Set d3[i][1] ← mat[i][1]
Set d3[i][2] ← mat[i][3]
[End of ‘for’ loop]
Set det ← determinant(d)
Set det1 ← determinant(d1)
Set det2 ← determinant(d2)
Set det3 ← determinant(d3)
[determinant is a function defined in step 3]
Print det
Print det1
Print det2
Print det3
If det ≠ 0 then
Set x ← det1/det
Set y ← det2/det
Set z ← det3/det
Print x
Print y
Print z
else
if det1 = 0 and det2 = 0 and det3 = 0 then
Print "Number of solutions are infinite"
else if det1 ≠  0 or det2 ≠ 0 or det3 ≠ 0 then
Print “There are no solution of these linear equations"
[end of ‘if’]
[end of cramer’s rule]

Step 3: [function ‘determinant’]
Set ans ← mat[0][0] × (mat[1][1] × mat[2][2] - mat[2][1] × mat[1][2])
- mat[0][1] × (mat[1][0] × mat[2][2] - mat[1][2] × mat[2][0])
+ mat[0][2] × (mat[1][0] × mat[2][1] - mat[1][1] × mat[2][0])
[End of function ‘determinant’]``````

### Code

1. It helps to find out the unknown variables without knowing about the other variables.

2. If any one of the values of x, y, or z is a fraction, then the fractional part is not needed to calculate the value of the other variables.

1. It can be applied if the matrix is non-singular i.e. |A| ≠ 0

2. If the order of the determinant is large, then it is very much tedious. For n > 4, the method is not suitable for practical purposes.

3. It is inefficient for more than 2 or 3 equations.

APPLICATIONS

1. It is used to solve linear equations with unknown variables.