i would like to use numpy.linalg.solve to solve a linear algebra equation, but i got an error message saying 'Last 2 dimensions of the array must be square'. Please shed some light thanks a lot !! here's my code:
import numpy as np
from numpy. linalg import solve
A = np.array([[3,-1,-1,0,0,0], [-1,4,-1,-1,0,0], [0,0,-1,-1,4,-1], [0,0,0,-1,-1,3]],float)
w = np.array([5,5,0,0],float)
v = solve(A,w)
print(v)
As igavriil already wrote numpy.linalg.solve
can only be used to find (the exact) solution for a well-determined system (i.e sqare coefficient matrix).
If your system is under- or over-determined, there is usually no exact solution.
If you want to find an approximate solution, you can use numpy.linalg.lstsq
. It uses a method called "least-squares-fitting" to find a solution that minimizes the overall error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With