Jialong Xu
Jialong Xu

Reputation: 63

multivariate equations solution with python

I have trouble with equations like:

I have equations:
a+b=10
a+b+c+d=20

I need the answer(all results must be positive):
a=0 b=10 c=0 d=10
a=0 b=10 c=1 d=9
...

I need all possible solutions, can I use python to solve it?

Upvotes: 1

Views: 656

Answers (1)

Suthiro
Suthiro

Reputation: 1300

There is an infinite number of solutions, but it does not mean variables are independent.

# importing sympy and its friends
import sympy as sm
from sympy import symbols 
from sympy import init_printing
from sympy.printing.mathml import print_mathml
# define symbols
a,b,c,d=symbols('a,b,c,d',real=True)
# equations are in form f(x1,x2,...,xn)=0
eq1 = a+b-10
eq2 = a+b+c+d-20
# solve the system of equations for [a,b,c,d]
print(sm.solve([eq1,eq2],[a,b,c,d]))

Output

{c: 10 - d, a: 10 - b}

Therefore positive solutions are d =[0;10) and b=[0,10). See sympy for further info.

Upvotes: 2

Related Questions