简体   繁体   中英

Sympy - How to solve a system of equations in terms of x

I want to solve a system of equations with multiple variables.
The solution I'm looking for is in this form y = f(z) .

The problem is: sympy won't solve completely my equations.

Here's a small example to illustrate my problem.

x = sympy.symbols("x", real=True)
y = sympy.symbols("y", real=True)
z = sympy.symbols("z", real=True)

eq0 = y - x**2
eq1 = x - z + 4

system = [eq0, eq1]
sympy.solve(system, y)[y]

This code produces y = x^2 , which is mathematically correct, but not what I wanted.
What I want to see is my system solved like this y = f(z) therefore ( y = (z - 4)^2 ).

Is there a way to define input and output in sympy's solvers?
Something like this:

sympy.solve(equations, input=z, ouptut=y)

Small note about substitution :
In this naive example, I could substitute the intermediate variable x with z+4 to solve the problem.
That said, I would prefer an alternative solution because, in my real problem, my system has 36 equations and substituting would be difficult.

You need to tell solve the variables to solve for.

eq0 = y - x**2
eq1 = x - z + 4
system = [eq0, eq1]
# solve your system of equation for x, y
solve(system, [x, y], dict=True)[0][y]
# out: (z - 4)^2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM