Maik
Maik

Reputation: 209

Solve Equation for specific variable

I would like to rearrange the following equation

c1 + (c2*a) + (c3*b) +(c4*a*a) + (c5*a*b) + (c6*b*b) + (c7*x) + (c8*x*x) = R

to x = ... . Is there a way with pythons sympy? I am not a mathematical expert, so a basic question is, is it possible at all to rearrange an equation like this.

Thanks for any hints or solutions!

Cheers

Upvotes: 0

Views: 101

Answers (3)

mathfux
mathfux

Reputation: 5949

Let say you have defined your equation:

import sympy as sp
a,b,c1,c2,c3,c4,c5,c6,c7,c8,x,R = sp.symbols('a,b,c1,c2,c3,c4,c5,c6,c7,c8,x,R')
eq = sp.Eq(R, c1+c2*a+c3*b+c4*a*a+c5*a*b+c6*b*b+c7*x+c8*x*x)

You can access its hand sides and manipulate it:

subtrahend = c1+c2*a+c3*b+c4*a*a+c5*a*b+c6*b*b + c8*x*x
dividend = c7
eq = sp.Eq(eq.lhs - subtrahend, eq.rhs - subtrahend)
eq = sp.Eq(eq.rhs / dividend, eq.lhs / dividend)

And your equation should become Eq(x, (R - a**2*c4 - a*b*c5 - a*c2 - b**2*c6 - b*c3 - c1 - c8*x**2)/c7) now.

Upvotes: 1

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

I think you need to use the symbols() function prior to using it in an eq.

from sympy import *
a, b, c1, c2, c3, c4, c5, c6, c7, c8, x, R = symbols('a b c1 c2 c3 c4 c5 c6 c7 c8 x R')
eqn=Eq(R,c1+c2*a+c3*b+c4*a*a+c5*a*b+c6*b*b+c7*x+c8*x*x)
solve(eqn,x)

Upvotes: 1

Amir Iskandar
Amir Iskandar

Reputation: 11

Try this:

from sympy import *
var('a, b, c1, c2, c3, c4, c5, c6, c7, c8, x, R')

eqn=Eq(R,c1+c2*a+c3*b+c4*a*a+c5*a*b+c6*b*b+c7*x+c8*x*x)

solve(eqn,x)

I do recommend using an array for variable c in case you wish to expand the equation.

Upvotes: 1

Related Questions