Can anyone tell me the python code to solve the equation:
2w + x + 4y + 3z = 5
w - 2x + 3z = 3
3w + 2x - y + z = -1
4x - 5z = -3
I have the following code but it isn't working:
A2 = np.array([[2,1,4,3],[1,-2,3],[3,2,-1, 1],[4, -5]])
b2 = np.array([[5,3,-1, -3]]).T
print('A\n',A2)
print('b\n',b2)
v2 = np.linalg.solve(A2,b2)
print('v')
print(v2)
To solve the two equations for the two variables x and y , we'll use SymPy's solve() function. The solve() function takes two arguments, a tuple of the equations (eq1, eq2) and a tuple of the variables to solve for (x, y) . The SymPy solution object is a Python dictionary.
Sympy is a package for symbolic solutions in Python that can be used to solve systems of equations. The same approach applies to linear or nonlinear equations.
The problem is how you formatted the missing variables for the equations, remember that you should use 0 instead of nothing, otherwise the arrays (equations) get misinterpreted and provide you a wrong answer/error:
This should work for you now:
import numpy as np
A = [[2,1,4,3],[1,-2,0,3],[3,2,-1,1],[0,4,0,5]]
Y = [5,3,-1,3]
res = np.linalg.inv(A).dot(Y)
print(res)
Output:
[-0.15384615 -0.30769231 0.76923077 0.84615385]
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