Programming Cramer’s Rule for 3 unknows.

Lets say you have three equations with 3 unknowns:

  • x + y + z = -5
  • x -4y + z = 35
  • x – 3y + 4z  = -18

Using Cramer’s rule we can solve each unknown, x, y, z without having to solve each equation. We first have to deduce the coefficient matrix determinate. We can find the by looking at the number in front of the x, y, and z value. The first equation is the first column in the matrix. Eg. x + y + z is 1 1 1.

DeterminateThis is the coefficient matrix determinate.

Important to now. If the determinate of D = 0, then Cramer’s Rule can not be used.

The answer column is simple the value on the right of each linear equation.

AnswerColumn

We need to find the coefficient determinates Dx, Dy, and Dz. They are simply the coefficient matrix determinate with the answer column replacing values in the x, y or z depending on the determinate. E.g Dx has it’s x column replaced with the answer column.

Determinate_x

Determinate_y

Determinate_z

How  to find the determinate of a matrix:

Using the + – + rule for the expansion of a matrix you can solve for the determinate of a valid matrix. What i mean by the rule for expansion is that the first number will have a + the second a – and the third a +. Lets say we want to solve the coefficient matrix determinate giving above. We first look at the x column. Ie. 1, 1, 1 we give each of these numbers a sign based on the expansion rule +-+. So 1, 1, 1 is now +1, -1, +1. The three, 2×2 matrices seen below are deduced by looking at 2 rows and 2 columns. What rows and columns depend on the component. So for the x component. We look at the y & z columns and y & z rows. That gives us -4 1 over -3 1.

DeterminateSolving_1

When multiply out the matrices it is important to keep note of the signs. As guided in the solution below.

matrixMulti

The answer is then simply giving by multiplying out.

DeterminateSolving_2

DeterminateSolving_3

To solve for each component x, y & z. It is giving by:

Component_xComponent_yComponent_z

The Data:

If you don’t understand the data, you don’t understand the problem” – Mike Acton.

I written down the representation of the data in my programming notebook. Each number is stored in a array of 12 elements. It is important to remember when calculating Dx for example you access the correct elements for the calculations.

wpid-wp-1443300182743.jpg

int matrix_Dx[9]{ matrix[3], matrix[1] , matrix[2],
		matrix[7] , matrix[5] , matrix[6] ,
		matrix[11] , matrix[9] , matrix[10] };
	int deter_Dx = determinateOfMatrix(matrix_Dx);

C++ source code for using Cramer’s Rule is available on my GitHub. Here is a link to the repository: Cramers-Rule

2 thoughts on “Programming Cramer’s Rule for 3 unknows.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s