# Using Solve Within Manipulate to Model Feedback Control System Problem

This post is extremely long and I do apologize - but please bear with me with all the explanation of the context of the question - due to how long the code is I have included a hyperlink where you can see the text as it looks in Mathematica to make everything easier to read and understand. It might help if you are familiar with Classical Control as that is what I am modelling.

First let me explain what I am trying to do:

I am writing a problem to automate solutions to a control systems assignment. I have had success with questions one and two, and am having some trouble in completing question three (I have code that works but not inside a manipulate [which is what I am trying to do]) - I will explain in more detail below.

First it is worth looking at the assignment:

                  Question one has been succesfully modelled.


This is question two:

                  Question two has been succesfully modelled.


The problem lies here, with the first part of question three:

                  Question three is where the problem lies.


Now I am going to include images of my hand-written solutions to these parts:

This is my hand-written solution to question one:

This is my hand-written solution to question two:

This is my hand-written solution to the first bit of question three:

Now I have actually written Mathematica code which successfully calculates the value of k, but this code is not all embedded in a single manipulate -> let me start by introducing this previously written code (which is background to my actual code question) here:

First let us create block a for the transfer function and block b for the gain, connect them in series to gain an OL expression I will call OLblockab, and then feed this back upon itself to gain CL expression Clblockab.

blocka = TransferFunctionModel[{5/((4 + s) (200 + s))}, s];

blockb = TransferFunctionModel[k, s];

OLblockab = SystemsModelSeriesConnect[blocka, blockb]

TransferFunctionModel[{{{5 k}}, {{5 k + (4 + s) (200 + s)}}}, s,
SystemsModelLabels -> {{None}, {None}}]


Now we feed this back upon itself to get a closed loop representation:

CLblockab = SystemsModelFeedbackConnect[OLblockab]

TransferFunctionModel[{{{5 k}}, {{5 k + (4 + s) (200 + s)}}}, s,
SystemsModelLabels -> {{None}, {None}}]


So we can see that thus far all is well, the old Mathematica code is consistent with the handwritten solution up to this point.

Now what I did was I copied and pasted from within the Transfer Function to gain a normal Algebraic expression that I can use commands on in Mathematica, and I expanded the denominator:

 (5 k)/(5 k + (4 + s) (200 + s)) // ExpandDenominator

(5 k)/(800 + 5 k + 204 s + s^2)


The reason I did this is that I need to compare the equation I have to a standard form of equation. This is the standard form of the equation:

So the assignment actually gives us a value of (1/Sqrt2) for zeta, hence I can set up two equations between the standard form of equation I need and the one I have. The two equations equate the numerator and denominators to solve the system for zeta and for Omegan (I think these are called the damping ratios and natural frequency of the system in Control Systems terms). This is the code that I wrote to do this:

N[Solve[Subscript[\[Omega], n]^2 == 800 + 5 k &&
204 == 2 (1/Sqrt[2]) Subscript[\[Omega], n]]] // FullSimplify

{{k -> 4001.6, Subscript[\[Omega], n] -> 144.25}}


So this matches with the handwritten solution, and all is well.

So then this is when my question actually starts: Then I thought - why don't I put everything that I have done in a single Manipulate function? I can just input values for a, b, c, d and it will work everything out for me. I started programming this Manipulate - and it works until I get to question 3 part one.

Here is a link to a page on the control website I am building where you can download the notebook http://www.controlmanchester.com/462-2/

    Manipulate[
Column[
{
"                           ",
Row[{a, b, c, d}]            ,
"                           ",
"                           ",
"                           ",
Subscript[G, (s)] = (a + 1)/((s + (b + 1)/2) (s + 20 (c + 1))) ,
"                           ",
"                           ",
"This is your Subscript[C, PI](s) ",
Subscript[C, PI (s)] = 1 + (b + 1)/(2 s),
"                           ",
"                           ",
"This is your Subscript[C, PD](s) ",
Subscript[C, PD (s)] = 1 + (s/(4 (6 + d) (1 + c)))
"                           ",
"                           ",
" This is your tf for G(s)  ",
tf1 =  TransferFunctionModel[Subscript[G, (s)], s],
"                           ",
"                           ",
" This is your unit step time function for G(s)  ",
"                           ",
or1 = OutputResponse[tf1, UnitStep[t], t]   ,
"                           ",
"                           ",
" This is the step response of G(s)" ,
Plot[{or1}, {t, 0, 5}, ImageSize -> 300],
"                           ",
"                           ",
" This is the bode plot of G(s)" ,
BodePlot[tf1, ImageSize -> 300] ,
"                           ",
"                           ",
" This is the gain block k    ",
kblock = TransferFunctionModel[k],
"                           ",
"                           ",
" This is the open loop  G(s)k  combo"
GsOLblock = SystemsModelSeriesConnect[ tf1, kblock],
"                           ",
"                           ",
" This is the closed loop  G(s)k/1+kG(s)  combo ",
GsCLblock = SystemsModelFeedbackConnect[GsOLblock],
"                           ",
"                           ",
"The expressions to compare are  ",
Row[   "\!$$\*FractionBox[\(k \*SuperscriptBox[\((\*SubscriptBox[\(\ \[Omega]$$, $$n$$])\), $$2$$]\), $$\*SuperscriptBox[\(s$$, $$2$$] + 2 \
\[Zeta] $$(\*SubscriptBox[\(\[Omega]$$, $$n$$])\) s + \
\*SuperscriptBox[$$(\*SubscriptBox[\(\[Omega]$$, $$n$$])\), $$2$$]\)]\
\)"  ,
denom = FullSimplify[ (Subscript[G, (s)]  k)/(
1 + Subscript[G, (s)] k)]    ]    ,
"                           ",
"                           ",
"Now we expand the denominator of our function",
denom2 = denom // ExpandDenominator
"                           ",
"                           ",
"Now we solve for k",
Row[{ nom = Numerator[denom2]   ,
"                  =    k(\!$$\*SubscriptBox[\(\[Omega]$$, $$n$$]\)\
\!$$\*SuperscriptBox[\()$$, $$2$$]\)" } ]             ,
Row[{ nom1 = nom/k  ,
"                  =     (\!$$\*SubscriptBox[\(\[Omega]$$, $$n$$]\)\
\!$$\*SuperscriptBox[\()$$, $$2$$]\)" } ]                             \
,
res1 = Solve[  Sqrt[nom1] ==  Subscript[\[Omega], n] ,
Subscript[\[Omega], n]    ] ,
Row[{spec1 = Subscript[\[Omega], n] /. res1    ,
"This is the specific value for \!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)"}, "               " ]      ,
Row[{"We are given a condition that \[Zeta] = 1/\!$$\*SqrtBox[\(2$$]\
\) "  ,       \[Stigma] = 1/Sqrt[2 ] ;}]            ,

Row[{ denom3 = Denominator[denom2] ,
"     =     \!$$\*SuperscriptBox[\(s$$, \
$$2$$]\)+2\[Zeta](\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\))s+(\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)\!$$\*SuperscriptBox[\()$$, $$2$$]\)"   }]          ,
Row[{ denom3 = Denominator[denom2] ,
"     =     \!$$\*SuperscriptBox[\(s$$, \
$$2$$]\)+2(\!$$\*FractionBox[\(1$$, \
SqrtBox[$$2$$]]\))\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)s+(\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)\!$$\*SuperscriptBox[\()$$, $$2$$]\)"   }]      ,
Row[{ denom3 = Denominator[denom2] ,
"     =     \!$$\*SuperscriptBox[\(s$$, \
$$2$$]\)+\!$$\*SqrtBox[\(2$$]\)\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)s+(\!$$\*SubscriptBox[\(\[Omega]$$, \
$$n$$]\)\!$$\*SuperscriptBox[\()$$, $$2$$]\)"   }]      ,
Row[{ denom3 = Denominator[denom2] ,
funct1    =
s^2 + Sqrt[
2] (spec1) s + (spec1)^2   }, "       =          "]      ,
Row[{ denom3 = Denominator[denom2] - s^2 ,
funct2    =     funct1 - s^2  }, "       =          "]      ,
"                           ",
"                           ",
"Now we solve for k"
}
]
, {a, 1, 9, 1}, {b, 1, 9, 1}, {c, 1, 9, 1}, {d, 1, 9, 1}]


So now I guess my question is - how can I implement the method that I previously had for solving k in my current example? I am sorry that I have not made this shorter.

When I just copied and pasted the code I reformatted into a fresh notebook it went into a crazy loop and I had to exit my kernel - so you can take a look at the notebook above but also I am going to provide screenshots of the output of the manipulate function here so you can see the state of play - the values for the variables a, b, c, d are the same as in the handwritten solutions.

Again, apologies for the extremely long post - I find it hard myself getting my head around it all but I have got stuck at this point - I hope that someone knows how I can move forward. Many thanks if you made it reading this far!

Cheers

-
I think you are right. Just writing it all out has helped me think about the problem. Will sleep on it and edit the post tomorrow. Thanks! – AugustCrawl Aug 22 '13 at 2:57
In the code, there should be a comma after the line " This is the open loop G(s)k combo" – Pickett Aug 22 '13 at 7:28
And I'm sorry, I still think it's hard to understand what your problem is. If you could create a version of the manipulate that has nothing but the code that troubles you in it, and write out the expected result as well as what the manipulate shows you, I think that would help a lot. – Pickett Aug 22 '13 at 7:35