Systems of Linear Equations

106 Views Asked by At

I am using the following code to find x and y in this linear equation. I was wondering if there is a way to add two more constrains to the following equation? For example, how can we add x>0 , and y>0 to the following equation(3x+4y=7 and 5x+6y=8)to get a positive output?

from sympy import *
x, y = symbols(['x', 'y'])
system = [Eq(3*x + 4*y, 7), Eq(5*x + 6*y, 8)]
soln = solve(system, [x, y])
print(soln)
1

There are 1 best solutions below

2
Oscar Benjamin On

You can declare the symbols x and y to be positive:

In [4]: from sympy import *
   ...: x, y = symbols(['x', 'y'])
   ...: system = [Eq(3*x + 4*y, 7), Eq(5*x + 6*y, 8)]
   ...: soln = solve(system, [x, y])
   ...: print(soln)
{x: -5, y: 11/2}

In [5]: from sympy import *
   ...: x, y = symbols(['x', 'y'], positive=True)
   ...: system = [Eq(3*x + 4*y, 7), Eq(5*x + 6*y, 8)]
   ...: soln = solve(system, [x, y])
   ...: print(soln)
[]