1

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?

Jialong Xu
  • 61
  • 9
  • hi, interesting, perhaps use numpy https://stackoverflow.com/questions/13523026/how-can-i-solve-multivariable-linear-equation-in-python – IronMan Nov 26 '20 at 03:34
  • There is an infinite number of solutions, even for `c` and `d` just integers. Should they be just natural numbers? – roadrunner66 Nov 26 '20 at 03:41
  • @roadrunner66 I only need positive results – Jialong Xu Nov 26 '20 at 03:42
  • The solution does not need a computer. `a` can be 0 to 10, then `b` is determined. Same for `c` and `d`. So there are 11 x 11 = 121 solutions. Or are you looking for a general solution? Then you might want to use symbolic calculations, see `SymPy`. https://docs.sympy.org/latest/modules/solvers/solvers.html – roadrunner66 Nov 26 '20 at 03:46
  • Is your full problem also underdetermined (i.e. fewer constraints than variables, matrix not invertible) ? https://en.wikipedia.org/wiki/Underdetermined_system – roadrunner66 Nov 26 '20 at 03:54

1 Answers1

2

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.

Suthiro
  • 1,210
  • 1
  • 7
  • 18
  • Whether it contains 36 variable or 4 the process is same and if your talking about 36 variables then your code needs so much of human interfarance the coder has to define like 18 eqaution the method you are showing is a tactical method – Ibrahim Nov 26 '20 at 03:57