Reputation: 11
I've been trying to make an optimization calculator for a project. I'm doing a basic example with the area of a square. To get an equation that I need to derivate I must solve P=XY so the expression I want it to display is Y =P/X. I'm planning on it to be more accesible than just allowing integers as input, so I'm trying the solver methods like this:
from sympy import *
x, y, p = symbols('x y z')
AExp = x*y
print(solve((x*y),p))
But I get the error
ValueError:
Since there is more than one variable in the expression, the
variable(s) of differentiation must be supplied to differentiate
Other solvers appear to be used for more complicated expressions so I'm at doubt on if I should use them and how.
Upvotes: 0
Views: 130
Reputation: 80329
Not sure about the error message, but the code from the question has some issues:
x, y, p = symbols('x y z')
creates x
and y
which are printed as 'x' and 'y' and p
which will be printed as 'z'AExp = x*y
: creates an expression being the product of x
and y
solve((x*y),p)
: tries to find a p
for the equation x*y = 0
Here is some code to find a y
such that p = x*y
:
from sympy import *
x, y, p = symbols('x y p', real=True) # telling sympy which type of solutions to search for helps prevent misunderstandings
my_expr = x*y
my_eq = Eq(my_expr, p) # equation x*y = p
print(solve(my_eq, y)) # find a y for the equation
print(solve(Eq(x*y, p), y)) # shorthand for the same
This outputs: [p/x]
Upvotes: 1