I have been trying for a very long time to solve a system of difficult specific equations, where there are 6 (six) unknown variables X (x1 ....). Using Python and various compute modules. It is not difficult to do it manually - find X (x1 ....). I'm trying to come up with a matrix plot for this. I would be very glad for any help.
x1, x2, x3, x4, x5, x6
-x1 x2 = 0
-x2 x3 x4 30 = 0
-x3 x5 = 0
-x5 25 = 0
-x4 x6 = 0
-x6 15 55 = 0
-1 1 0 0
-1 1 1 30
-1 1 0 0
-1 25 0 0
-1 1 0 0
-1 15 55 0
code
import pandas as pd
import numpy as np
import openpyxl
import math
import scipy.sparse
import scipy.sparse.linalg
from scipy.linalg import solve
A = np.array( [ [-1, 1, 0, 0, 0, 0], [-1, 1, 1, 30, 0, 0], \
[-1, 1, 0, 0, 0, 0], [-1, 25, 0, 0, 0, 0], \
[-1, 1, 0, 0, 0, 0], [-1, 15, 55, 0, 0, 0] ])
Vh = np.linalg.svd(A)
y = np.array( [0, 0, 0, 0, 0, 0] )
# A_inv = np.linalg.lstsq(A)
x = np.matmul(Vh, y)
# x = np.linalg.solve(Vh, y)
print(x)
C:\Dash_board\uchpotkot.py:110: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
x = np.matmul(Vh, y)
Traceback (most recent call last):
File "C:\Dash_board\uchpotkot.py", line 110, in <module>
x = np.matmul(Vh, y)
ValueError: could not broadcast input array from shape (6,6) into shape (6,)
CodePudding user response:
I don't think you're using np.linalg.svd
correctly. Per the docs, it returns 3 separate arrays, u
, s
, and vh
.
Instead of
Vh = np.linalg.svd(A)
write
u, s, vh = np.linalg.svd(A)
CodePudding user response:
You have a lot of red herrings in your code, it's important to focus on just what is needed.
You are trying to solve n equations with n unknowns using a linear algebra matrix solution. The overall approach here is that you set up your equations into a matrix form of:
A * x = b
where A is a matrix of the coefficients of your unknowns, x is a vector of unknowns, and b is a vector of constants. Once you have those defined, you can solve for x by multiplying both sides by the inverse of A:
x = inverse(A) * b
Your code above does not define the coefficient (A) matrix correctly, nor is the constant matrix (b) correct. Your A matrix should be:
[-1, 1, 0, 0, 0, 0]
[ 0, -1, 1, 1, 0, 0]
[ 0, 0, -1, 0, 1, 0]
[ 0, 0, 0, 0, -1, 0]
[ 0, 0, 0, -1, 0, 1]
[ 0, 0, 0, 0, 0, -1]
and your constant (b) vector should be:
[ 0]
[-30]
[ 0]
[-25]
[ 0]
[-70]
Once you've defined both of those, the answer is pretty easy to get using the equation above:
import numpy as np
A = np.array( [ [-1, 1, 0, 0, 0, 0],
[ 0, -1, 1, 1, 0, 0],
[ 0, 0, -1, 0, 1, 0],
[ 0, 0, 0, 0, -1, 0],
[ 0, 0, 0, -1, 0, 1],
[ 0, 0, 0, 0, 0, -1] ])
b = np.transpose(np.array([0,-30,0,-25,0,-15-55]))
unknowns = np.transpose(np.array(['x1','x2','x3','x4','x5','x6']))
result = np.matmul(np.linalg.inv(A),b)
for i in range(len(result)):
print('{} = {}'.format(unknowns[i],result[i]))
result:
x1 = 125.0
x2 = 125.0
x3 = 25.0
x4 = 70.0
x5 = 25.0
x6 = 70.0
CodePudding user response:
richardec is right for the SVD part, but also it might be more simple than what you're doing here. You're just looking for all value of the x variables, right?
import numpy as np
import scipy.sparse.linalg
from scipy.linalg import solve
A = np.array( [[-1, 1, 0, 0, 0, 0],
[0, -1, 1, 1, 0, 0],
[0, 0, -1, 0, 1, 0],
[0, 0, 0, 0, -1, 0],
[0, 0, 0, -1, 0, 1],
[0, 0, 0, 0, 0, -1]])
b = np.array([0, -30, 0, -25, 0, -70])
x = np.linalg.solve(A, b)
print(x)
[125., 125., 25., 70., 25., 70.]
The A matrix is the variable coefficients and b is the value of each expression.