Reputation: 181
I want to solve a system of 4 equation that is the following:
x1 = b1*a1 + b2*a2 + b3*a3 + b4*a4
x2 = b1*a5 + b2*a6 + b3*a7 + b4*a8
x3 = b1*a9 + b2*a10 + b3*a11 + b4*a12
x4 = b1*a13 + b2*a14 + b3*a15 + b4*a16
Where my goal is to find a solution such that b1,b2,b3,b4 are defined by the other parameters/symbols.
My attempt is:
import sympy as sy
sy.init_printing()
x1, x2, x3, x4, b1, b2, b3, b4, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
a15, a16 = sy.symbols('x1, x2, x3, x4, b1, b2, b3, b4, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
a11, a12, a13, a14, a15, a16')
print(sy.solve(((x1-b1*a1-b2*a2-b3*a3-b4*a4, x2-b1*a5-b2*a6-b3*a7-b4*a8,
x3-b1*a9-b2*a10-b3*a11-b4*a12,x4-b1*a13-b2*a14-b3*a15-b4*a16), [b1,b2,b3,b4])))
The problem is that I get the following error:
print(sy.solve(((x1-b1*a1-b2*a2-b3*a3-b4*a4, x2-b1*a5-b2*a6-b3*a7-b4*a8, x3-b1*a9-b2*a10-b3*a11-b4*a12,x4-b1*a13-b2*a14-b3*a15-b4*a16), [b1,b2,b3,b4])))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\sympy\solvers\solvers.py", line 858, in solve
symbols = set().union(*[fi.free_symbols for fi in f])
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\sympy\solvers\solvers.py", line 858, in <listcomp>
symbols = set().union(*[fi.free_symbols for fi in f])
AttributeError: 'list' object has no attribute 'free_symbols'
How can I fix the code?
Upvotes: 0
Views: 60
Reputation: 14530
You have put extra brackets around the equations that shouldn't be there. Here's a simpler example:
import sympy as sym
x, y = sym.symbols('x, y')
print(sym.solve(((x + y, x - y - 1), [x, y])))
The fix is to remove those brackets:
print(sym.solve((x + y, x - y - 1), [x, y]))
This way you are passing two arguments to solve
. One is a tuple of equations and the other is a list of symbols. Previously you were passing one argument as a tuple.
Fixing that and tidying up your code gives:
import sympy as sym
sym.init_printing()
(
x1, x2, x3, x4,
b1, b2, b3, b4,
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16,
) = sym.symbols('x1:5 b1:5 a1:17')
equations = [
x1 - b1*a1 - b2*a2 - b3*a3 - b4*a4,
x2 - b1*a5 - b2*a6 - b3*a7 - b4*a8,
x3 - b1*a9 - b2*a10 - b3*a11 - b4*a12,
x4 - b1*a13 - b2*a14 - b3*a15 - b4*a16,
]
unknowns = [b1, b2, b3, b4]
solution = sym.solve(equations, unknowns)
print(solution)
Note that you can also use Eq
like Eq(x1, b1*a1 + ...)
and that for linear systems it is better to use linsolve
rather than solve
.
Upvotes: 1