Can sympy solve this equation?

91 views
Skip to first unread message

Carl Sandrock

unread,
Feb 23, 2016, 12:43:24 AM2/23/16
to sympy
I am attempting to work a problem from a textbook in sympy, but sympy fails to find a solution which appears valid. For interest, it is the design of a PID controller using direct synthesis with a second order plus dead time model.

The whole problem can be reduced to finding K_C, tau_I and tau_D which will make

K_C*(s**2*tau_D*tau_I + s*tau_I + 1)/(s*tau_I) = (s**2*tau_1*tau_2 + s*tau_1 + s*tau_2 + 1)/(K*s*(-phi + tau_c))

for given tau_1, tau_2, K and phi.

I have tried to solve this by matching coefficients:

import sympy

s, tau_c, tau_1, tau_2, phi, K = sympy.symbols('s, tau_c, tau_1, tau_2, phi, K')

target = (s**2*tau_1*tau_2 + s*tau_1 + s*tau_2 + 1)/(K*s*(-phi + tau_c))

K_C, tau_I, tau_D = sympy.symbols('K_C, tau_I, tau_D', real=True)
PID = K_C*(1 + 1/(tau_I*s) + tau_D*s)

eq = (target - PID).together()
eq *= sympy.denom(eq).simplify()
eq = sympy.poly(eq, s)

sympy.solve(eq.coeffs(), [K_C, tau_I, tau_D])

This returns an empty matrix. However, the textbook provides the following solution:

booksolution = {K_C: 1/K*(tau_1 + tau_2)/(tau_c - phi),
                tau_I: tau_1 + tau_2,a
                tau_D: tau_1*tau_2/(tau_1 + tau_2)}

Which appears to satisfy the equations I'm trying to solve: 

[c.subs(booksolution).simplify() for c in eq.coeffs()]

returns

[0, 0, 0]

Can I massage this into a form which sympy can solve? What am I doing wong?

Carsten Knoll

unread,
Feb 23, 2016, 11:36:16 AM2/23/16
to sy...@googlegroups.com
Hi,

I already had similar problems. The system of equation is of order 3
which might be too hard. But there is a linear part and if this is
solved first and then plugged into the remaining 2 equations, sympy can
manage it.

I documented my attempt here:

https://gist.github.com/cknoll/c03dcf8443c0409d37da


(Generally, I think sympy.solve could sometimes be a little bit smarter,
to use such structural properties.)
> --
> You received this message because you are subscribed to the Google
> Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sympy+un...@googlegroups.com
> <mailto:sympy+un...@googlegroups.com>.
> To post to this group, send email to sy...@googlegroups.com
> <mailto:sy...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/4d922cfe-9f54-4196-b7d8-15abb053e091%40googlegroups.com
> <https://groups.google.com/d/msgid/sympy/4d922cfe-9f54-4196-b7d8-15abb053e091%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Carl Sandrock

unread,
Feb 23, 2016, 11:46:44 AM2/23/16
to sy...@googlegroups.com
Thank you for your response.

Coincedentally I was just progressing along a similar route myself. What I came up with was

eqs = eq.coeffs()

solution = {}
solution[K_C] = sympy.solve(eqs[1], K_C)[0]
solution[tau_D] = sympy.solve(eqs[0], tau_D)[0].subs(solution)
solution[tau_I] = sympy.solve(eqs[2], tau_I)[0].subs(solution).simplify()

This matches your method. So this has saved me some tedious algebra, but not the effort of finding the order in which to evaluate the equations. I wish that sympy could do this automatically. I seem to remember Sage being able to solve this set of equations, but I can’t find the worksheet now.
> You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/JJVkM7Cs9MA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/56CC8A6B.4080107%40gmx.de.

Carsten Knoll

unread,
Feb 23, 2016, 12:31:40 PM2/23/16
to sy...@googlegroups.com, kshitij...@gmail.com
On 02/23/2016 05:46 PM, Carl Sandrock wrote:

> ...
> This matches your method. So this has saved me some tedious algebra,
> but not the effort of finding the order in which to evaluate the
> equations. I wish that sympy could do this automatically.

I agree.

Maybe this could be added to

https://github.com/sympy/sympy/wiki/gsoc-2016-ideas#solvers

Casually, there is a parallel thread on this list on that topic.


On 02/23/2016 03:26 PM, Kshitij Saraogi wrote:
> Hello,
>
> I am Kshitij Saraogi and I will be a GSoC applicant this year under SymPy.
>
> I wanted to discuss about the Solvers project.[1] <
> https://github.com/sympy/sympy/wiki/gsoc-2016-ideas#solvers>
>
> After going through the discussions mentioned on the Ideas page, I would
> like to get inputs on a few ideas for this project:
>

> ...
> 3. Implementing more equation solvers: [3]
> <https://github.com/sympy/sympy/issues/10006>
>
> * System of multivariate linear equations.
>
> * Nonlinear multivariate equation solver.
>
> * Equations solvable by LamberW function (Transcendental equation solver)
>
> * Nested trignometric expressions.
> ...


@ Kshitij: Do you think our problem/feature request is already covered by issue 10006?

Carsten




Aaron Meurer

unread,
Feb 23, 2016, 3:48:14 PM2/23/16
to sy...@googlegroups.com
In master I get the solution

In [46]: sympy.solve(eq.coeffs(), [K_C, tau_I, tau_D])
Out[46]:
⎡ ⎧ -(τ₁ + τ₂) τ₁⋅τ₂ ⎫⎤
⎢{K_C: 0, τ_I: 0}, ⎨K_C: ───────────, τ_D: ───────, τ_I: τ₁ + τ₂⎬⎥
⎣ ⎩ K⋅(φ - τ_c) τ₁ + τ₂ ⎭⎦

It looks like whatever fixed it will be included in 1.0 (it's also
fixed in the 1.0 release branch).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/4d922cfe-9f54-4196-b7d8-15abb053e091%40googlegroups.com.

Carl Sandrock

unread,
Feb 23, 2016, 4:05:29 PM2/23/16
to sy...@googlegroups.com
That's great news. I will include the direct method in my notes as well.

--
This message may contain unintentional errors, as it was typed on a phone
> You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/JJVkM7Cs9MA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2B1HHFtcfF5XBzKDEAR3KAyE1L42CymgM8LreOKnyNz%3DA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages