I am trying to solve a simple equation, but this is what is being outputted:
the equation is y = m*x b
,
y
, m
, and b
are already defined
I would like to print b
.
import sympy
while True:
x1 = int(input('x1: '))
y1 = int(input('y1: '))
x2 = int(input('x1: '))
y2 = int(input('y1: '))
m = (y1-y2) / (x1-x2)
print(m)
m = sympy.symbols('m')
x = sympy.symbols('x')
y = sympy.symbols('y')
b = sympy.symbols('b')
a = y, x*m b
print(sympy.solve(a, b))
CodePudding user response:
You can try like:
import sympy
# while True:
x1 = int(input('x1: '))
y1 = int(input('y1: '))
x2 = int(input('x1: '))
y2 = int(input('y1: '))
m = (y1-y2) / (x1-x2)
print(f"m: {m}")
b = sympy.symbols('b')
a = y, x*m b
print(sympy.solve(y1 -x1*m - b, b))
Test:
x1: 1
y1: 7
x1: 2
y1: 12
m: 5.0
# out: [2.00000000000000]
CodePudding user response:
We need to give two equations to sympy, one for each x,y
combination. First, create the general line equation a = Eq(y, x*m b)
. And then substitute the individual values for x
and for y
to find the two equations.
import sympy
x1, y1 = 1, 4
x2, y2 = 5, 8
x, y, m, b = sympy.symbols('x y m b')
a = sympy.Eq(y, x * m b)
eq1 = a.subs({x: x1, y: y1}) # Eq(4, b m)
eq2 = a.subs({x: x2, y: y2}) # Eq(8, b 5*m)
print(sympy.solve([eq1, eq2], (m, b))) # {m: 1, b: 3}