Engee documentation
Notebook

Linearisation of the dynamical system

In this demo we will identify an LTI system (linear and stationary) from the inputs-outputs of the dynamic model. This will allow us to linearise a selected segment of the model and then treat it as a regular linear stationary system.

Model description

The object of linearisation for us will be a model consisting of two RC filters through which rectangular pulses with a period of 0.01 s will pass.

image.png

Each filter consists of a 10 ohm resistor and a 1e-04 F capacitor. The input signal u comes from the block Pulse Generator, and the output signal y is the voltage across the last capacitor of the stage, measured with a voltmeter Voltage Sensor.

In [ ]:
Pkg.add(["ControlSystemIdentification", "ControlSystems"])
In [ ]:
gr()
modelName = "rc_circuit"
if !(modelName in [m.name for m in engee.get_all_models()]) engee.load( "$(@__DIR__)/$(modelName).engee"); end;
data = engee.run( modelName )
Out[0]:
SimulationResult(
    "u" => WorkspaceArray("rc_circuit/u"),
    "y" => WorkspaceArray("rc_circuit/y")
)

If we plot the variables u and y, we can estimate how much the filter chain modifies the input signal. It is this behaviour that we will try to approximate using a linear model.

In [ ]:
t,u,y = collect(data["u"]).time, collect(data["u"]).value, collect(data["y"]).value
plot( t, [u y], label=["u" "y"] )
Out[0]:

It can be expected that the aperiodic link may not be sufficient to approximate the behaviour of the system accurately enough.

Linearisation process

We will perform the linearisation using the function subspaceid from the library ControlSystemIdentification:

In [ ]:
using ControlSystems, ControlSystemIdentification

By default, subspaceid returns the system in a state space of the following form:

$$\begin{aligned} x^+ &= Ax + Bu + Ke\\ y &= Cx + Du + e \end{aligned}$$

There are more than ten other functions available in the package for linearising systems, but it is recommended to use this one first if the system is defined in open loop form. For closed-loop systems, we recommend newpem.

In [ ]:
# Получим частоту дискретизации системы из вектора времени
Ts = t[2] - t[1]

# Проведем линеаризацию системы
sys = subspaceid( iddata( y, u, Ts ));

The step response graph will allow us to evaluate the quality of linearisation (we have deliberately set the pulse generator amplitude equal to 1, otherwise it would be necessary to normalise it).

In [ ]:
plot( t, y, label="Исходный сигнал" )
plot!( step( sys, 0.03 ), label="Реакция системы на ступеньку" )
Out[0]:

It can be seen that the response to a single step very accurately repeats the simulation results - the response of the original system to rectangular pulses. If desired, we could limit the degree of the linear system by the second argument subspaceid and obtain a simpler, less accurate approximation.

Studying the stability of the system

Of course, the observed system is stable. But in the case of a more complex system, we could make such statements after analysing the response, the AFC or the hodographs.

A simple plot of the linear amplitude-phase-frequency response of a system can be constructed using a short function:

In [ ]:
bodeplot( sys )
Out[0]:

But the following code allows you to have much more control over the nature of the graph display:

In [ ]:
mag, phase, w = bode( sys )

plot( [w./(2pi)],
      [20*log10.( mag[:]) phase[:]], xscale=:log10, xticks=exp10.(1:8),
      linecolor=[1 2], layout=(2,1))
vline!( [1/(2Ts)  1/(2Ts)] )
plot!( title=["ЛАФЧХ системы" ""], xlabel=["" "Частота (Гц)"] )
plot!( ylabel=["Амплитуда (дБ)" "Фаза (град)"], leg=:false )
Out[0]:

The red vertical line marks the frequency equal to half the sampling frequency. The phase graph is limited to 360 degrees, but beyond that the LAFCC values drop sharply.

Conclusion

This linearisation method will allow you to build an LTI model from a wide range of dynamical systems or simply tabular data. The resulting simplified dynamical system can be placed in a block Transfer Function and used as a surrogate model that is fast to compute and, for example, allows for a first approximation study of the stability of the dynamical system.