None Notebook

This notebook contains material from cbe30338-2021; content is available on Github.

< 4.4 Estimation and Anamoly Detection | Contents | Tag Index | 4.7 Observer Synthesis using Linear Matrix Inequalities >

Open in Colab

Download

4.6 Lab Assignment 6: Anomaly Detection

This lab assignment consists of two exercises which, together, demonstrate the tuning of an observer for the purpose of state and disturbance estimation. The disturbance is a proxy for any variable or modeling that may affect the operation of the Temperature Control Lab. Monitoring the disturbance estimate provides a means for detecting operational anomalies.

4.6.1 Part 1. Determing the Observer Gains $L$

4.6.1.1 State Space Model

The state-space model for the Temperature Control Laboratory was previously developed and included here for reference.

\begin{align} \frac{d}{dt}\underbrace{\left[\begin{array}{c} T_{H,1} \\ T_{S,1} \\ T_{H,2} \\ T_{S,2} \end{array}\right]}_x & = \underbrace{\left[\begin{array}{cccc} -(\frac{U_a+U_b+U_c}{C^H_p}) & \frac{U_b}{C^H_p} & \frac{U_c}{C^H_p} & 0 \\ \frac{U_b}{C^S_p} & -\frac{U_b}{C^S_p} & 0 & 0 \\ \frac{U_c}{C^H_p} & 0 & -(\frac{U_a+U_b+U_c}{C^H_p}) & \frac{U_b}{C^H_p} \\ 0 & 0 & \frac{U_b}{C^S_p} & -\frac{U_b}{C^S_p} \end{array}\right]}_A \underbrace{\left[\begin{array}{c}T_{H,1} \\ T_{S,1} \\ T_{H,2} \\ T_{S,2}\end{array}\right]}_x + \underbrace{\left[\begin{array}{cc}\frac{\alpha P_1}{C_p} & 0 \\ 0 & 0 \\ 0 & \frac{\alpha P_2}{C_p} \\ 0 & 0 \end{array}\right]}_{B_u} \underbrace{\left[\begin{array}{c}u_1 \\ u_2\end{array}\right]}_u + \underbrace{\left[\begin{array}{c}\frac{U_a}{C^H_p} \\ 0 \\ \frac{U_a}{C^H_p} \\ 0 \end{array}\right]}_{B_d} \underbrace{\left[\begin{array}{c}T_{amb}\end{array}\right]}_{d} \end{align}\begin{align} \underbrace{\left[\begin{array}{c} T_1 \\ T_2 \end{array}\right]}_y & = \underbrace{\left[\begin{array}{cccc} 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right]}_C \underbrace{\left[\begin{array}{c}T_{H,1} \\ T_{S,1} \\ T_{H,2} \\ T_{S,2}\end{array}\right]}_x \end{align}

where

\begin{align} \frac{dx}{dt} & = A x + B_u u + B_d d \\ y & = C x \end{align}

The process variables are gathered and organized into the state vector $x$, manipulable inputs $u$, disturbance inputs $d$, and measured process variables $y$.

$$x = \left[\begin{array}{c}T_{H,1} \\ T_{S,1} \\ T_{H,2} \\ T_{S,2}\end{array}\right] \qquad u = \left[\begin{array}{c}u_1 \\ u_2\end{array}\right] \qquad d = \left[\begin{array}{c}T_{amb}\end{array}\right] \qquad y = \left[\begin{array}{c} T_1 \\ T_2 \end{array}\right] $$

Coefficients are organized into matrices $A$, $B_u$, $B_d$ and $C$.

$$ A = \left[\begin{array}{cccc} -(\frac{U_a+U_b+U_c}{C^H_p}) & \frac{U_b}{C^H_p} & \frac{U_c}{C^H_p} & 0 \\ \frac{U_b}{C^S_p} & -\frac{U_b}{C^S_p} & 0 & 0 \\ \frac{U_c}{C^H_p} & 0 & -(\frac{U_a+U_b+U_c}{C^H_p}) & \frac{U_b}{C^H_p} \\ 0 & 0 & \frac{U_b}{C^S_p} & -\frac{U_b}{C^S_p} \end{array}\right] $$$$ B_u = \left[\begin{array}{cc}\frac{\alpha P_1}{C_p} & 0 \\ 0 & 0 \\ 0 & \frac{\alpha P_2}{C_p} \\ 0 & 0 \end{array}\right] \qquad B_d = \left[\begin{array}{c}\frac{U_a}{C^H_p} \\ 0 \\ \frac{U_a}{C^H_p} \\ 0 \end{array}\right] $$$$ C = \left[\begin{array}{cccc} 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right] $$

4.6.1.2 Observer

At each time step $t_k$ there are two calculations to perform:

\begin{align} \hat{x}_k^{pred} & = \hat{x}_{k-1} + (t_k - t_{k-1}) ( A \hat{x}_{k-1} + B_u u_{k-1} + B_d \hat{d}_{k-1}) \\ \hat{y}_k^{pred} & = C \hat{x}_k^{pred} \end{align} $$\hat{x}_{k} = \hat{x}_{k}^{pred} - (t_k - t_{k-1})L (\underbrace{\hat{y}_{k}^{pred} - y_k}_{y_k^{err}})$$

The following cell presents an implementation of an observer for the Temperature Control Lab. This version of the observer assumes $\hat{d}_{k} = \hat{d}$ is constant in time.

4.6.1.3 Testing the Observer

4.6.1.4 Eigenvalues and Eigenvectors

For a linear system with dynamics governed by a constant coefficient matrix $A$, the eigenvalues of $A$ determine stability. A system is asymptotically stable if all of the eigenvalues have negative real part. Here we demonstrate the calculation and display of the eigenvalues for the model governing the Temperature Control Lab which has all real eigenvalues.

4.6.1.5 Placing the eigenvalues of $A-LC$

As has been previously demonstrated, the dynamics of the state error $e = \hat{x} - x$ are described by the equation

$$\frac{de}{dt} = (A - LC) e + B_d (\hat{d} - d)$$

The coefficient matrix $A - LC$, where we get to choose $L$, determines the evolution of the error in the state estimate. We would like for the error to decay quickly to a minimal value which implies choosing $L$ so that eigenvalues of $A - LC$ are negatives values.

Let's compare the eigenvalues of $A$ to the eigenvalues of $A-LC$ using the $L$ found in the example above.

Pole placement describes a family of algorithms which, given $A$, $C$, and a desired set of eigenvalues, computes a matrix $L$ such that $A-LC$ has the specified set of eigenvalue, or else reports that no such $L$ exists. The following cell shows how to use scipy.signal.place_poles() to compute an $L$ where the eigenvalues are 3 times larger (i.e, more negative) than the eigenvalues of $A$ alone.

What is notable about this version of $L$ is that we are now using both sensors to update every estimated state.

4.6.1.6 Exercise 1.

Test the observer described above for your device. Perform the following calculations:

  1. Revise the parameters and coefficient matrices $A$, $B_u$, $B_d$ to incorporate the best estimates available for your particular device.

  2. Compute $L$ where the eigenvalues are placed multiples of 1, 2, 5, and 10 times the eigenvalues of $A$ alone. Test each $L$ using your particular device.

  3. Report the results of each experimental run for your device. Which of these $L$ matrices would you recommend for production use? Why?

4.6.2 Part 2. Estimating Disturbances

Our model for the Temperature Control Lab is given by

\begin{align} \frac{dx}{dt} & = A x + B_u u + B_d d \\ y & = C x \end{align}

where $d$ is an unmeasured disturbance. Up to this point we have used an apriori constant estimate $\hat{d}$ for real-time control calculations. Here we show how, in principle, $d$ can be estimated in real-time.

We assume $d$ is constant or changing so slowly in time that the dynamics can be modeled by

$$\frac{dd}{dt} = w_d$$

where $w_d$ is a external signal causiing changes in $d$. This additional model equation can be incorporated into the state space framework.

\begin{align} \begin{bmatrix}\frac{dx}{dt} \\ \frac{dd}{dt} \end{bmatrix} & = \underbrace{\begin{bmatrix} A & B_d \\ 0 & 0 \end{bmatrix}}_{A^{aug}} \underbrace{\begin{bmatrix}x \\ d \end{bmatrix}}_{x^{aug}} + \underbrace{\begin{bmatrix}B_u \\ 0 \end{bmatrix}}_{B_u^{aug}} u + \underbrace{\begin{bmatrix} 0 \\ 1\end{bmatrix}}_{B_d^{aug}} w_d\\ y & = \underbrace{\begin{bmatrix}C & 0 \end{bmatrix}}_{C^{aug}} \begin{bmatrix}x \\ d \end{bmatrix} \end{align}

4.6.2.1 Testing Disturbance Estimator

4.6.2.2 Exercise 2.

Implement a disturbance estimator for your copy of the Temperature Control Lab.

  1. Using the matrix parameters used for Exercise 1 of this assignment, compute and report numerical values for augmented matrices $A_{aug}$, $B_{aug}$, $C_{aug}$ described above.

  2. Using the results of exercise 1, choose an initial value for $L_{aug}$. Place four of the eigenvalues to match the eigenvalues placed in exercise 1. The fifth eigenvalue should have a negative value corresponding to a time constant chosen to between 20 and 100 seconds. Test your code as shown above.

  3. Test your disturbance estimator on your copy of the Temperature Control Lab. You will need to adapt the testing code presented above for this purpose. Try to detect changes in the environment surrounding your device, such placing in cooler, warmer, or breezy place. A hair dryer would certainly do the job. Does your disturbance estimator detect the change? If needed, choose a different time constant in part 2.

< 4.4 Estimation and Anamoly Detection | Contents | Tag Index | 4.7 Observer Synthesis using Linear Matrix Inequalities >

Open in Colab

Download