solving for 5 variables using 6 linear equation using numpy
linear-algebra, numpy, python
Solution
You are correct in your calling sequence, though I would pull out the last column of `A`:
A = np.array([
[-14, 1, 0, 0, 1],
[2, -14, 0, 1, 0],
[0, 1, -14, 2, 0],
[0, 0, 0, -15, 1],
[0, 0, 2, 0, -14],
[1, 1, 1, 1, 1 ]])
b = np.array([0, 0, 0, 0, 0, 1])
sol = np.linalg.lstsq(A, b)
As everyone else has mentioned, your system is overdetermined. This means that any fit is likely to be a bad one. Indeed, `np.linalg.lstsq` returns the residuals:
residuals : {(), (1,), (K,)} ndarray Sums of residuals; squared Euclidean 2-norm for each column in b - a*x. If the rank of a is < N or > M, this is an empty array. If b is 1-dimensional, this is a (1,) shape array. Otherwise the shape is (K,).
Which in this case is:
print sol[1]
>>> array([0.96644295])
This indicates that the fit is very poor (and there is no approximate linear solution here). We can see that by again checking:
print (b - np.dot(A, sol[0])).sum()
>>> 1.36912751678
Which would be zero in the `NxN` case.
Problem
I'm trying to solve following system of equations= ``` -14a + b + e = 0 2a - 14b + d = 0 b -14c +2d = 0 -15d + e = 0 + 2c -14e = 0 a + b + c + d + e = 1 ``` I appended required zeroes to matrices formed from above equations . I used numpy.linalg.solve function. I always get this error:: numpy.linalg.linalg.LinAlgError: Singular matrix. I know that I have created a singular matrix by making one row elements zero. My matrices & code :: ``` a= np.array([ [-14, 1, 0, 0, 1, 0], [2, -14, 0, 1, 0, 0], [0, 1, -14, 2, 0, 0], [0, 0, 0, -15, 1, 0], [0, 0, 2, 0, -14, 0], [1, 1, 1, 1, 1, 0] ]) b=np.array( [0, 0, 0, 0, 0, 1] ) x = np.linalg.solve(a, b) ``` Is there another way to solve this ? Using np.linalg.lstsq returns :: ``` (array([ 0.00674535, 0.00713199, 0.00709352, 0.00582019, 0.006766 , 0. ]), array([], dtype=float64), 5, array([ 15.88397122, 15.68586038, 14.59368088, 13.14182044, 12.12312981, 0. ])) ``` How am I supposed to get my solutions from above array ??.. None of the no. in above array is the solution..