Harmonic Oscillator - Maple Programming Help

Home : Support : Online Help : Applications and Example Worksheets : Applications : applications/HarmonicOscillator

Harmonic Oscillator

 1. System Definition 2. Design of a P controller 3. Design of a PI controller 4. Design of a PID controller

1. System Definition



The model of a harmonic oscillator corresponds to a second order system with $u\left(t\right)$ as the input and $y\left(t\right)$ as the output. The system is defined by the angular frequency $\mathrm{ω}$, the attenuation $\mathrm{θ}$, and the gain $A$.

 Parameters Variables Attenuation $\left(\mathrm{θ}\right)$ Input $\left(u\left(t\right)\right)$ Angular frequency $\left(\mathrm{ω}\right)$ Output $\left(y\left(t\right)\right)$ Gain $\left(A\right)$ 

The system is defined with the following differential equation

 ${\mathrm{sysde}}{≔}\stackrel{{\mathbf{..}}}{{y}}{}\left({t}\right){+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}\stackrel{{\mathbf{.}}}{{y}}{}\left({t}\right){+}{{\mathrm{\omega }}}^{{2}}{}{y}{}\left({t}\right){=}{{\mathrm{\omega }}}^{{2}}{}{A}{}{u}{}\left({t}\right)$ (1)

The transfer function that results from this differential equation can be obtained using the DynamicSystems[TransferFunction] command.



$\mathrm{with}\left(\mathrm{DynamicSystems}\right):$



$\mathrm{systf}≔\mathrm{TransferFunction}\left(\mathrm{sysde},\mathrm{inputvariable}=\left[u\left(t\right)\right],\mathrm{outputvariable}=\left[y\left(t\right)\right]\right):\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{PrintSystem}\left(\mathrm{systf}\right)$

 $\left[\begin{array}{l}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable}}{=}\left[{u}{}\left({s}\right)\right]\\ {\mathrm{outputvariable}}{=}\left[{y}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{=}\frac{{{\mathrm{\omega }}}^{{2}}{}{A}}{{{s}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{{\mathrm{\omega }}}^{{2}}}\end{array}\right$ (2)



The step response for the corresponding system can be observed by changing the slider values for θ and ω in the following application.

In this example, $\mathrm{\theta }$ controls the damping, such that a system with $\mathrm{θ}<1$ results in a system that is under damped and results in an overshoot.  For the cases with $\mathrm{θ}>1$the system is over damped and the response has no overshoot.  If $\mathrm{θ}=1$ the system is critically damped, resulting in the fastest rise time of the system without overshooting the final value. The parameter $\mathrm{\omega }$ is the natural frequency of the system.

The amplitude $A$ is set to 1 for this example.



Step Response Plot Routine





 Uncontrolled Step Response set $\mathrm{θ}$ value set $\mathrm{ω}$ value 



2. Design of a P controller



$\mathrm{Pcontroller}≔\mathrm{Kp}:$

$\mathrm{OpenLoopEq}≔\mathrm{Pcontroller}\cdot \mathrm{systf}:-\mathrm{tf}\left[1,1\right]$

 ${\mathrm{OpenLoopEq}}{≔}\frac{{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}}{{{s}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{{\mathrm{\omega }}}^{{2}}}$ (3)
 ${}$ (4)

$\mathrm{ClosedLoopEq}≔\mathrm{simplify}\left(\frac{\mathrm{OpenLoopEq}}{1+\mathrm{OpenLoopEq}}\right)$

 ${\mathrm{ClosedLoopEq}}{≔}\frac{{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}}{{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{{\mathrm{\omega }}}^{{2}}{+}{{s}}^{{2}}}$ (5)

$\mathrm{ClosedLoopSys}≔\mathrm{TransferFunction}\left(\mathrm{ClosedLoopEq}\right):$

$\mathrm{PrintSystem}\left(\mathrm{ClosedLoopSys}\right)$

 $\left[\begin{array}{l}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable}}{=}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable}}{=}\left[{\mathrm{y1}}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{=}\frac{{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}}{{{s}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}{+}{{\mathrm{\omega }}}^{{2}}}\end{array}\right$ (6)

The response to the system to chaging proportional gain controller can be seen below. It is important to note that the P controller is not able to get the system to actually reach the desired final value. The controller has an offset error.

 Controlled Step Response - P controller $\mathrm{θ}$ value $\mathrm{ω}$ value  $\mathrm{Kp}$ value 



3. Design of a PI controller



$\mathrm{PIcontroller}≔\mathrm{Kp}+\frac{\mathrm{Ki}}{s}$

 ${\mathrm{PIcontroller}}{≔}{\mathrm{Kp}}{+}\frac{{\mathrm{Ki}}}{{s}}$ (7)

$\mathrm{OpenLoopEq1}≔\mathrm{PIcontroller}\cdot \mathrm{systf}:-\mathrm{tf}\left[1,1\right]$

 ${\mathrm{OpenLoopEq1}}{≔}\frac{\left({\mathrm{Kp}}{+}\frac{{\mathrm{Ki}}}{{s}}\right){}{{\mathrm{\omega }}}^{{2}}{}{A}}{{{s}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{{\mathrm{\omega }}}^{{2}}}$ (8)

$\mathrm{ClosedLoopEq1}≔\mathrm{simplify}\left(\frac{\mathrm{OpenLoopEq1}}{1+\mathrm{OpenLoopEq1}}\right)$

 ${\mathrm{ClosedLoopEq1}}{≔}\frac{\left({\mathrm{Kp}}{}{s}{+}{\mathrm{Ki}}\right){}{{\mathrm{\omega }}}^{{2}}{}{A}}{{A}{}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{{s}}^{{2}}{+}{s}{}{{\mathrm{\omega }}}^{{2}}{+}{{s}}^{{3}}}$ (9)

$\mathrm{ClosedLoopSys1}≔\mathrm{TransferFunction}\left(\mathrm{ClosedLoopEq1}\right):$

$\mathrm{PrintSystem}\left(\mathrm{ClosedLoopSys1}\right)$

 $\left[\begin{array}{l}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable}}{=}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable}}{=}\left[{\mathrm{y1}}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{=}\frac{{A}{}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}}{{{s}}^{{3}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{{s}}^{{2}}{+}\left({\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}{+}{{\mathrm{\omega }}}^{{2}}\right){}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}}\end{array}\right$ (10)



The effects of adjusting the proportional controller gain $\left(\mathrm{Kp}\right)$ and integral controller gain $\left(\mathrm{Ki}\right)$ values is displayed below. In most cases, the offset error can be eliminated by adding an integral component to the proportional controller.

It is important to note, that a system may become unstable and begin to oscillate out of control if the value of $\mathrm{Ki}$ is too large and the value of $\mathrm{Kp}$ too small (relative to $\mathrm{Ki}$).



 Controlled Step Response - PI controller $\mathrm{θ}$ value $\mathrm{ω}$ value  $\mathrm{Kp}$ value $\mathrm{Ki}$ value  

4. Design of a PID controller



 ${\mathrm{PIDcontroller}}{≔}{\mathrm{Kp}}{+}\frac{{\mathrm{Ki}}}{{s}}{+}{\mathrm{Kd}}{}{s}$ (11)

$\mathrm{OpenLoopEq2}≔\mathrm{PIDcontroller}\cdot \mathrm{systf}:-\mathrm{tf}\left[1,1\right]$

 ${\mathrm{OpenLoopEq2}}{≔}\frac{\left({\mathrm{Kp}}{+}\frac{{\mathrm{Ki}}}{{s}}{+}{\mathrm{Kd}}{}{s}\right){}{{\mathrm{\omega }}}^{{2}}{}{A}}{{{s}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{s}{+}{{\mathrm{\omega }}}^{{2}}}$ (12)

$\mathrm{ClosedLoopEq2}≔\mathrm{simplify}\left(\frac{\mathrm{OpenLoopEq2}}{1+\mathrm{OpenLoopEq2}}\right)$

 ${\mathrm{ClosedLoopEq2}}{≔}\frac{\left({\mathrm{Kd}}{}{{s}}^{{2}}{+}{\mathrm{Kp}}{}{s}{+}{\mathrm{Ki}}\right){}{{\mathrm{\omega }}}^{{2}}{}{A}}{{A}{}{\mathrm{Kd}}{}{{\mathrm{\omega }}}^{{2}}{}{{s}}^{{2}}{+}{A}{}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{}{{s}}^{{2}}{+}{s}{}{{\mathrm{\omega }}}^{{2}}{+}{{s}}^{{3}}}$ (13)



$\mathrm{ClosedLoopSys2}≔\mathrm{TransferFunction}\left(\mathrm{ClosedLoopEq2}\right):$

$\mathrm{PrintSystem}\left(\mathrm{ClosedLoopSys2}\right)$

 $\left[\begin{array}{l}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable}}{=}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable}}{=}\left[{\mathrm{y1}}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{=}\frac{{A}{}{\mathrm{Kd}}{}{{\mathrm{\omega }}}^{{2}}{}{{s}}^{{2}}{+}{A}{}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}}{{{s}}^{{3}}{+}\left({\mathrm{Kd}}{}{{\mathrm{\omega }}}^{{2}}{}{A}{+}{2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}\right){}{{s}}^{{2}}{+}\left({\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}{+}{{\mathrm{\omega }}}^{{2}}\right){}{s}{+}{A}{}{\mathrm{Ki}}{}{{\mathrm{\omega }}}^{{2}}}\end{array}\right$ (14)

The system response to changes in the proportional, integral and derivative gains are shown below.

 ${}$ (15)
 $\left[\begin{array}{c}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable =}}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable =}}\left[{\mathrm{y1}}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{\mathrm{=}}\frac{{{\mathrm{\omega }}}^{{2}}{}{A}{}{\mathrm{Kd}}{}{{s}}^{{2}}{+}{{\mathrm{\omega }}}^{{2}}{}{A}{}{\mathrm{Kp}}{}{s}{+}{{\mathrm{\omega }}}^{{2}}{}{A}{}{\mathrm{Ki}}}{{{s}}^{{3}}{+}\left({2}{}{\mathrm{\omega }}{}{\mathrm{\theta }}{+}{\mathrm{Kd}}{}{{\mathrm{\omega }}}^{{2}}{}{A}\right){}{{s}}^{{2}}{+}\left({{\mathrm{\omega }}}^{{2}}{+}{\mathrm{Kp}}{}{{\mathrm{\omega }}}^{{2}}{}{A}\right){}{s}{+}{{\mathrm{\omega }}}^{{2}}{}{A}{}{\mathrm{Ki}}}\end{array}\right$ (16)



Controlled Step Response - PID controller

$\mathrm{θ}$ value

$\mathrm{ω}$ value



$\mathrm{Kp}$ value

$\mathrm{Ki}$ value





 $\mathrm{Kd}$ value