Engee documentation
Notebook

Time domain responses of multiple models

This example shows how to compare the step response of multiple models on a single graph using step. This example compares the step response of an uncontrolled object with a closed loop and two different PI controllers. You can use similar methods with other response commands, such as impulse or lsim, to get plots of the response of multiple models.

In this example, get two models whose time characteristics you want to compare and plot them together. For example, you might compare a third-order system G and the response of a closed-loop system G with controller C1.

In [ ]:
Pkg.add(["ControlSystems"])
In [ ]:
import Pkg; 
Pkg.add("ControlSystems")
   Resolving package versions...
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
using ControlSystems
In [ ]:
G = zpk([], [-5, -5, -10], 100)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Int64}}
             1
100----------------------
   (s + 5)(s + 5)(s + 10)

Continuous-time transfer function model
In [ ]:
C1 = pid(0, 4.4; form=:parallel)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoRational{Float64}}
4.4
----
1.0s

Continuous-time transfer function model
In [ ]:
CL1 = feedback(G*C1)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Float64, ComplexF64}}
                                                                  1.0
440.00000000000006----------------------------------------------------------------------------------------------------
                  (1.0s^2 + 18.182441807113936s + 86.88838599897278)(1.0s^2 + 1.8175581928860574s + 5.063967927833328)

Continuous-time transfer function model

The function step takes a description of the system as input and generates an object of type ControlSystemsBase.SimResult as output. If this object is passed to the function plot, the transient graph will be plotted.

In [ ]:
step(CL1)
Out[0]:
ControlSystemsBase.SimResult{Matrix{Float64}, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Matrix{Float64}, Matrix{Float64}, StateSpace{Continuous, Float64}}([0.0 1.1101474535917157e-7 … 0.9999999999999879 0.9999999999999879], 0.0:0.0089:55.9988, [0.0 6.459039729988163e-8 … 0.5818181818181747 0.5818181818181747; 0.0 1.4386184767184292e-5 … 5.620737843258297e-15 5.620604012052631e-15; 0.0 0.000597208113779645 … -1.6263032587282567e-18 -2.0599841277224584e-18; 0.0 0.016278755147748875 … -6.938893903907228e-18 -6.938893903907228e-18], [1.0 1.0 … 1.0 1.0], StateSpace{Continuous, Float64}
A = 
  0.0                  2.0                   0.0                  0.0
  0.0                  0.0                   8.0                  0.0
  0.0                  0.0                   0.0                  8.0
 -3.4375000000000044  -3.9062500000000004  -15.625000000000002  -19.999999999999993
B = 
 0.0
 0.0
 0.0
 2.0
C = 
 1.7187500000000002  0.0  0.0  0.0
D = 
 0.0

Continuous-time state-space model)
In [ ]:
plot([step(G), step(CL1)], label=["G" "CL1"])
xlims!(0,7)
hline!([1], linecolor=:black, style=:dash, label=:false)
Out[0]:

If you send several objects to the function stepas input arguments to plot, a single graph will display the responses of the specified models If you do not specify a time range for plotting, step tries to select a time range that illustrates the dynamics of the model.

Compare the response of a model step with feedback to the response of another controller. Specify colours and plot styles for each response.

In [ ]:
C2 = pid(2.9, 7.1; form=:parallel)
CL2 = feedback(G * C2)

plot(step(G,7), label="G", color=:blue)
plot!(step(CL1,7), label="CL1", color=:green)
plot!(step(CL2,7), label="CL2", color=:red)

hline!([0.4, 1], linestyle=:dash, color=:black, label=:false)
Out[0]:

You can specify your own colour and graph style for each transient by changing the parameters linestyle and color.