# Finding x1, x2, x3, x4 in code

Hi, Could someone explain the logic behind finding x1, x2, x3 in practice lab?

x_3 = -2
x_2 = (A_ref[1,3] - A_ref[1,2] * x_3) / A_ref[1,1]
x_1 = (A_ref[0,3] - A_ref[0,2] * x_3 - A_ref[0,1] * x_2) / A_ref[0,0]

print(x_1, x_2, x_3)

I used this logic to solve the locked lab exam, but I don’t understand how this calculation works

If you follow along with the “row reduction” steps, then the original system of three equations is simplified into this form:

The first three columns are the coefficients of x_1, x_2, and x_3. The fourth column are the ‘y’ values.

So, what code in that cell does is, starting from the bottom row of the reduced matrix, starts doing substitutions to solve for each of the values.

The bottom row equates to:
1 * x_3 = -2.
So you already have x_3 = -2.
Technically, the code there should be x_3 = A_ref([2,3], rather than hard-coding the value -2.

At this point, it just becomes an algebra exercise:

• In the 2nd row: `(5*x2 - 7*x3 = 34)`, you substitute in x_3 and solve for x_2.
• Then in the top row, you substitute in x_3 and x_2, and solve for x_1.