brraap
brraap

Reputation: 3

Lost with Sympy solving

I'm approaching this problem with little math experience and moderate python experience, any help appreciated. I have these values and equations and need to find x and y:

x+y == a
a = 32.8
b = 19.3
c = 82
d = 12
e = 8
f = 69
f == ((((b+e)+x)*c)+(d*y))/(b+x+y)

Using sympy, I wrote the following code:

from sympy import symbols, Eq, solve, init_printing

a,b,c,d,e,f,x,y = symbols('a b c d e f x y')
init_printing(use_unicode=True)
    
expr = ((((b+e)+x)*c)+(d*y))/(b+x+y)

#I think this is x in terms of y
xiny = solve(expr.subs([(b,19.3), (c,82),(d,12),(e,8),(f,71)]),x)

# and I think this is y in terms of x
print(solve(eq.subs(a,32.8),y))

#But how to sub them in and continue?
Eq(f,expr)
eq = Eq(x+y,a)
solution = solve((eq.subs(a,32.8),expr.subs([(b,19.3), (c,82),(d,12),(e,8),(f,71)]) ),(x,y))
print(solution)

Using sympy I think I've managed to find x in terms of y, and y in terms of x but can't tie it all together. I'm getting negative numbers which don't make sense to me(especially for volumes which is the use case). What's the best way to approach this, especially as the a-f variables will be input by the user. Any help appreciated.

Upvotes: 0

Views: 103

Answers (2)

Roman_N
Roman_N

Reputation: 195

Interesting... Let's see. As I got you need to solve this system with a as parameter:

SoE.
(It's made with https://codecogs.com/latex/eqneditor.php. May be it will help you to explain your tasks in future)

And code:

from sympy import *

a,b,c,d,e,f,x,y = symbols('a b c d e f x y')
init_printing(use_unicode=True)

a = 32.8 # you can put here a input() command
b = 19.3
c = 82
d = 12
e = 8
f = 69

# Note that "==" operator always returns bool, so this row does nothing
f == (((b+e+x)*c)+(d*y))/(b+x+y) 

expr = ((((b+e)+x)*c)+(d*y))/(b+x+y)

eq1 = Eq(f,expr)

eq2 = Eq(x+y,a)

solve([eq1, eq2], (x,y))

{𝑥:13.7528571428571, 𝑦:19.0471428571429}

I found that you have defined your variables twice: in the begining ang in the subs method.
When you write solve(), it solves system of equations.

Upvotes: 0

Thibault Cimic
Thibault Cimic

Reputation: 365

I've giving up the sympy syntaxe to focus on the math problem, so your system of equation you wanna solve is :

x+y = 32.8

((19.3+8+x)*82+12y)/(19.3+x+y) = 69

And I got the solution x = 9627/700 and y = 13333/700

If this solution is not correct than I guess there is a problem with the equation, or of course I can have solve it wrong

And in your sympy code, shouldn't it be more something like this :

expr = ((((b+e)+x)*c)+(d*y))/(b+x+y)
eq1 = Eq(f,expr)
eq2 = Eq(x+y,a)
solution = solve((eq2.subs(a,32.8),eq1.subs([(b,19.3), (c,82),(d,12),(e,8),(f,71)]) ),(x,y))
print(solution)

Upvotes: 1

Related Questions