Rank of Matrix | Assignement | Code Question

Hi, I solved this linear equation
[7 5 3 ] [120]
[2 2 5] [70]
[1 2 1] [20]

I wanted to reproduce it with code as demonstrated in the notebook for solving equations with three variable.

A = np.array([
        [7, 5, 3],
        [3, 2, 5],
        [1, 2, 1]
    ], dtype=np.dtype(float))

b = np.array([120, 70, 20], dtype=np.dtype(float))

print("Matrix A:")
print(A)
print("\nArray b:")
print(b)

x = np.linalg.solve(A, b)

print(f"Solution: {x}")

The result confuses me – especially the solution for y:

Solution: [1.50000000e+03 8.63506797e-14 5.00000000e+02]

The solution for x and z looks somewhat like my solution – but I don’t know if this is by accident. Because I don’t know how to read this numbers. Clarification would be great. Thanks

Hi @Sven2!

Thanks for your post. What np.linalg.solve returns is the vector

\left[ \begin{array}{c}x \\ y \\ z \end{array}\right]^T = \left[ x \ y \ z \right]

By solving the linear system

\left[ \begin{array}{ccc} 7 & 5 & 3 \\ 2 & 2 & 5 \\ 1 & 2 & 1 \end{array} \right] \left[ \begin{array}{c}x \\ y \\ z \end{array}\right] = \left[ \begin{array}{c} 120 \\ 70 \\ 20 \end{array}\right]

One thing, I have runned this code and obtained 1.50000000e+01 and not 1.50000000e+03, and the same pattern in the other values. -16 and +00.

In python notation, that e that appears is related to scientific notation. So whenever you see it, it is just 10^something

So, the last value, 5.00000000e+02 is just 5.00000000\cdot10^{2}, which is just 500 in that case.

The value related to y, if you see it, it is 10^{-16} which is a value very close to zero. It sometimes happens because on the way Python handles floating numbers. You can always round the solution to make it look better.

You can do the following:

Instead of

x = np.linalg.solve(A, b)

Try doing

x = np.linalg.solve(A, b).round(4)

That should avoid that weird value realted to y.

Thanks,
Lucas