Rewrite equation as polynomial - python

from sympy import *
K, T, s = symbols('K T s')
G = K/(1+s*T)
Eq1 =Eq(G+1,0)
I want to rewrite equation Eq1 with sympy as polynomial: 1+K+T*s==0
How would I do this?
I spent some hours of searching and trying simplifications methods but could not find a elegant, short solution.
The actual problem in SymPy:
from IPython.display import display
import sympy as sp
sp.init_printing(use_unicode=True,use_latex=True,euler=True)
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
G1=sp.Eq(G0+1,0)
display(G1)
How to tell Sympy to rewrite equation G1 as polynomial in shape s^3*(...)+s^2*(...)+s*(...)+(...)=... ?
The actual problem from textbook: http://i.imgur.com/J1MYo9H.png
How it should look like: http://i.imgur.com/RqEDo7H.png
The two equations are equivalent.

Here's what you can do.
import sympy as sp
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
Throw away the denominator
eq = (G0 + 1).as_numer_denom()[0]
Expand the equation and collect terms with powers of s.
eq = eq.expand().collect(s)
Final Equation
Eq(eq, 0)
Eq(K_e*K_f**2 + T_d0^'*T_e*T_v*s**3 + s**2*(T_d0^'*T_e + T_d0^'*T_v + T_e*T_v) + s*(T_d0^' + T_e + T_v) + 1, 0)

Related

Using sympy to rearrange equation?

Im trying to rearrange a supply curve equation for calculation the price elasticy.
The equation is log P = -2 + 1.7 * log Q.
Im trying to rearrange the equation so that log Q is in terms of log P. Is there a way sympy can handle these rearrangements?
p, q = sp.symbols('p, q', real=True, positive=True)
eq = sp.Eq(-2+1.7*sp.log(q))
sp.solve(eq,sp.log(p))
Using sympy solve will take care of the arrangements. The equations just must be set up properly and the function parameters must be correct.
import sympy as sp
p, q = sp.symbols('p, q', real=True, positive=True)
eq = sp.Eq(-2+1.7*sp.log(q), p) # set equation = to p
ans = sp.solve(eq,q) #solve equation for q in terms of p
This solution simply saves your equation to eq and solves eq for q so the answer is in terms of only p. In the end ans holds the value:
[3.24290842192454*exp(0.588235294117647*p)]
I highly reccomend reading this website before coming to SO.
Although you showed log P in the written text, you did not include it in the code. If you do so, you can then request that solve isolate log(q) for you:
>>> eq = Eq(log(p), -2 + 10*log(q)/17)
>>> solve(eq, log(q))
[17*log(p)/10 + 17/5]

Solving ODEs with SymPy

I've just started trying out SymPy. Unfortunately, I am already stumped. Behold this:
from sympy import *
t, G, M = symbols('t G M', real = True)
x = Function('x')
y = Function('y')
print(Eq(Derivative(x, t, 2), G * M * x(t) / (x(t) * x(t) + y(t) * y(t))**1.5))
...it simply prints False. The documentation says that this means the relationship can be proven to be false.
I know that I can prevent evaluation by using evaluation = False, but eventually I want to solve my system of differential equations, and then this assumption will come into play again.
So, can anyone see what I did wrong here?
Addendum:
What I am trying to do is play around with the two-body-problem and orbital mechanics. With the gravitational constant G and the mass of the primary M at the origin, this and the symmetric equation for y(t) describe the gravitational acceleration on the secondary.
The solution, Kepler tells us, should be an ellipse for reasonable starting conditions.
I found it now, the solution is rather simple: SymPy needs to be told the x is a function of t, so
print(Eq(Derivative(x(t), t, 2), G * M * x(t) / (x(t) * x(t) + y(t) * y(t))**1.5))
does the trick.

How to proof with Sympy that a given Cartesian equation can be written as a given polar equation

i have an assignment on sympy and am struggling with the following question:
"Prove with the help of Sympy that 4*(x2 + y2 -ax)3 = 27a2(x2+y2)2 can be written using r = 4a*cos(theta/3)3".
I have tried to substitute x = r*cos(theta) and y = r*sin(theta).
Then I tried sp.solveset(eq, r) but I only got a very longset of {}, nothing like the given polar equation.
Does anyone know how to do this (I can use sympy and numpy)?
The following code builds the equation from its left hand side and right hand side. Then the change of variables to polar coordinates is performed using substitution.
The resulting trigonometric expression is then simplified, and it turns out to be zero after simplification. So any pair/tuple (x,y)=(r*cos(theta),r*sin(theta)) is a solution.
from sympy import *
a,x,y,theta = symbols('a x y \Theta', real=True)
init_printing(use_latex=True)
lhs = 4 * (x**2 + y**2 - a*x) ** 3
rhs = 27 * a**2 * (x**2 + y**2)**2
f = lhs - rhs
r = 4 * a * cos(theta/3)**3
display(f,"----")
f = f.subs(x,r*cos(theta))
f = f.subs(y,r*sin(theta))
display(f,"----")
f1 = f
display(simplify(f))
# format for wolframalpha
t = symbols('t')
f1 = f1.subs(theta,t)
import re
f1 = re.sub("\*\*","^",str(f1))
print("----")
print("wolframalpha expression: solve ", str(f1)," over the reals")
To double-check this, at the end, a wolframalpha query is also generated, which confirms the solutions.

How to solve this equation with sympy?

Recently I got a long equation to solve that looks like that.
I've tried to solve this using sympy.solveset(), but it returned ConditionSet which means it couldn't handle this equation. How can I solve this equation using simpy library and if not at least in python? The code that I used:
import sympy as sp
t = sp.symbols('t')
a = 1.46
b = 1.2042 * 10**-4 * ((1.2275 * 10**-5 + t) * sp.ln(1.2275 * 10**-5 + t) - t)
result = sp.solveset(sp.Eq(a, b), t)
print(result)
This is a transcendental equation. It possibly has an analytic solution in terms of the Lambert W function but I'm not sure. I'll assume that you just want a numerical solution which you can get using nsolve:
In [42]: nsolve(a - b, t, 1)
Out[42]: 1857.54700584719

Expected square matrix?

Kindly help in finding out the error.
import sympy as sp
from scipy.linalg import solve
x, y, z,w = sp.symbols('x,y,z,w')
alpha,beta, gamma, delta = sp.symbols('alpha,beta,gamma,delta')
f1 = alpha*y*z - beta*w
f2= gamma*z - delta*x
f3=-alpha*y*z + beta*w + x
f4= -alpha*y*z + beta*w - gamma*z + delta*x
K=solve(f1,f2,f3,f4)
print K
The error showing is
Expected square matrix
scipy.linalg.solve does not solve SymPy equations. Use a SymPy solver. See, for example, http://docs.sympy.org/dev/modules/solvers/solvers.html

Categories