Ungraded Tool: Linear System w/ Three Variables

In this ungraded tool figure, if I use np.linalg.solve. I get x = -0.2, y = 2.4 and z = 0 as the solution, which is a point in R3 space. Why is the tool telling me the solution is a line?

Hi @Hartej_Dhiman,

Since it is ungraded tool, you can paste here your code for np.linalg.solve. This will help us recreate the problem or see if you did some typo errors. Thanks.

Here you go @jonrico



import numpy as np
S = np.array([[4,-3,1],[2,1,3],[2,-4,2]], dtype=float)
R = np.array([-8,2,-10], dtype=float)
X = np.reshape(R, (3,1))
SOL = np.linalg.solve(S, X)
print(SOL)

I see. You have a typo error in the last equation.

It should be -2z instead of 2z

The -2z in the last equation in the image is the value provided by the Coursera example. I didn’t type that in.

Yes. Thus, your Python code should be:

S = np.array([[4,-3,1],[2,1,3],[2,-4,-2]], dtype=float)

If you edit your code into this, you should also get a solution which is a line.

Thanks @jonrico, there was indeed a typo in my matrix definition.

So, the solution is actually this traceback in this case: “LinAlgError: Singular matrix”

Unfortunately, that’s the limitation of using numpy.linalg.solve. It can only solve exact solution and raises an error if the matrix is Singular. Take a look at their documentation for more accurate explanation:

@jonrico I doubt this is a limitation but rather a regular feature in programming languages. So, it will throw and exception and then you will be able to catch it with a try/catch and perform/execute some custom code based on the type of exception.

1 Like

I agree. It is just terminology and context. I called it a “limitation” in the context of what @yjcb22 is trying to do. That is, he is trying to solve a singular matrix and expecting a similar output (which is an equation of a line) as the Coursera visualization tool which outputs a line as the solution.

That is my Filipino master!

1 Like

Yes, exacty. This is ideally handled with a Try Catch block where a custom output can be shown indicating that the matrix is Singular.