You are looking at the website for a prior offering of this course. Here is the current website:
Contents
- State space models
- The matrix exponential function
- Asymptotic stability
- Optimization and Optimal Control
- State estimation
- Optimal Observer Derivation (a.k.a the Kalman Filter, a very famous thing)
State space models
What is a state space model?
A state-space model is a set of ordinary differential equations that can be written in this form:
There are two variables:
is the state is the input
Both variables are functions of time, so we could have written
The state and input may have more than one element — that is to say, they may be vectors (represented by a matrix of numbers) and not scalars (represented by a single number). Suppose, in particular, that:
- the state
has elements - the input
has elements
Then, we would represent them as column matrices:
Taking the time derivative of a matrix is equivalent to taking the time derivative of each element of that matrix, so we would write
The state-space model also has two constants:
is a constant matrix of size is a constant matrix of size
The state space model has two key properties:
- it is linear because both
is a linear function of and - it is time-invariant because
and are constant
How do I put a system in state space form?
Suppose we are given a description of a dynamic system. We would like to describe this same system with a state space model.
Remember that every state space model is linear. The equations of motion that describe a dynamic system are often nonlinear. So apparently, we will have to accept that state space models can only approximate some of the systems we want to describe.
We will use linearization to arrive at this approximation, in four steps.
Step 1. Rewrite the system as a set of first-order ordinary differential equations:
In this expression, the variables
Step 2. Find an equilibrium point
A solution to this equation is called an equilibrium point because if
then
and so
This equation may have no solutions, in which case no equilibrium point exists for the system. This is bad. We will ignore this situation for now.
This equation also may have many solutions, in which case you have to make a choice. A good choice is whatever equilibrium point you would like your system to achieve.
Step 3. Define the state and input as follows:
Note that
Step 4. Compute
Why does this make any sense? Look again at the ODEs that describe the original system:
First, because
then
So the left-hand side of the ODEs can simply be replaced with
Now, suppose we want a linear approximation to the right-hand side of the ODEs — the function
There you have it: “
Example (first-order)
Consider the system with dynamics that are described by the following equation of motion:
Let’s apply our method to put this system in state space form.
We begin by rewriting it as a set of first-order ODEs. Lucky us, the system is already described by just one first-order ODE, so all we need to do is solve for
Next, we find an equilibrium point by solving
In this case, there are many solutions. Suppose we pick this one:
Then, we define the state and input based on this choice of equilibrium point:
Finally, we compute
The resulting state space model is
Note that the original system was linear, so there was no approximation here. We could probably have skipped most of these steps and written the system in state-space form by inspection. On the other hand, it is nice to know that the process of “linearization” still works even in this simple case.
Example (second-order)
Consider the system with dynamics that are described by the following equation of motion:
Let’s apply our method to put this system in state-space form.
We begin by rewriting it as a set of first-order ODEs:
- We find the time-derivative of
with highest order — in this case, , of order 2. - We define new variables for each time-derivative of
with lower order — in this case, , the only time-derivative with order between 0 and 2. We might choose the following name for this time-derivative:
- We rewrite the original ODEs (in this case, just one) in terms of these new variables:
- We add one extra ODE for each new variable (in this case, just one extra) — this is trivial, coming from the way we defined these new variables:
- We collect the original ODEs and the extra ODEs together, if necessary solving for all of the time-derivatives (that’s not necessary here):
- Finally, we rewrite our result in the form
by collecting things in column matrices as follows:
Note that, as desired, these rewritten ODEs have time derivatives that are at most of first order. Also note that all of these time-derivatives are on the left-hand side of the equations — none appear on the right-hand side.
Next, we find an equilibrium point by solving
There are many solutions. Suppose we pick this one:
In this case, there are many solutions. Suppose we pick this one:
Then, we define the state and input based on this choice of equilibrium point:
Finally, we compute
The resulting state space model is
The original system was nonlinear and the state space model is linear, so there must be some approximation here! As we will see, this approximation is good near the equilibrium point but can be very bad elsewhere.
The matrix exponential function
What is the matrix exponential?
The matrix exponential of a square matrix
where
How do I use the matrix exponential to solve a linear system?
The solution to the set of linear ODEs
with the initial condition
is
How do we know that this solution is correct? First, let’s check that this solution satisfies the ODEs:
Apparently, it does. Second, let’s check that this solution satisfies the initial condition:
Again, it does. (We might wonder if this is the only solution to the original ODEs — it is, although a proof would require more work.)
How do I use the matrix exponential to solve state space models?
Consider the state space model
This model does not look the same as
Indeed, without specifying
and so we are right back at a linear system that can be solved with the matrix exponential. Another common choice of
for some constant matrix
and so — just like before — we are right back at a linear system that can be solved with the matrix exponential. Although this result will get us a long way, we will see how to solve state space models for other choices of input later on.
Asymptotic stability
What are eigenvalues and eigenvectors?
Consider a square matrix
then we call
One way to find eigenvalues is to solve the equation
where “
for
Apparently, if
How do I diagonalize a square matrix?
Suppose we have found the eigenvalues
with an eigenvector in each column, and also the matrix
with the eigenvalues along the diagonal.
Two things are true.
First, the following equality holds:
You could easily verify this result for yourself.
Second, if
The key consequence of
In this case — if all eigenvalues are distinct and so the matrix of eigenvectors is invertible — we say that
What is the matrix exponential of a diagonal matrix?
It is easy to find the matrix exponential of a diagonal matrix, starting from the definition:
What is the solution to a linear system that is diagonalizable?
We have seen that the solution to
with the initial condition
is
Suppose
where
is a diagonal matrix that contains the eigenvalues of
is a matrix of the corresponding eigenvectors. Then, applying the definition of matrix exponential again, we have
where the last step comes from what we just found out about the matrix exponential of a diagonal matrix. In this expression, the terms
that appear in the diagonal of
Therefore, we can infer the behavior of
Euler’s formula tells us that
Apparently, as time gets large, one of three things is true about each of these terms:
- if
, then grows quickly - if
, then is constant ( ) or is sinusoidal with constant magnitude ( ) - if
, then decays quickly to zero
It is possible to show that (more or less) the same result holds for any system
where
See the reference textbook for details.
When is a linear system asymptotically stable?
The system
is called asymptotically stable if
Based on our observations about the solution to linear systems that are diagonalizable, we can state the following important result:
In particular, we now have a test for whether or not a controller “works.” Suppose we apply linear state feedback
to the state space system
so that
The controller “works” when this system is asymptotically stable, i.e., when
We may not have a systematic way of finding a matrix
Optimization and Optimal Control
These notes were originally written by T. Bretl and were transcribed for this reference page by S. Bout.
Optimization
The following thing is called an optimization problem:
The solution to this problem is the value of
-
We know that we are supposed to choose a value of
because “ ” appears underneath the “minimize” statement. We call the decision variable. -
We know that we are supposed to minimize
because “ ” appears to the right of the “minimize” statement. We call the cost function.
In particular, the solution to this problem is
-
We could plot the cost function. It is clear from the plot that the minimum is at
.
-
We could apply the first derivative test. We compute the first derivative:
Then, we set the first derivative equal to zero and solve for
:Values of
that satisfy the first derivative test are only “candidates” for optimality—they could be maxima instead of minima, or could be only one of many minima. We’ll ignore this distinction for now. Here’s a plot of the cost function and of it’s derivative. Note that, clearly, the derivative is equal to zero when the cost function is minimized:
In general, we write optimization problems like this:
Again,
Here is another example:
The solution to this problem is the value of both
Second, there are two decision variables instead of one. But again, there are at least two ways of finding the solution to this problem:
-
We could plot the cost function. The plot is now 3D—the “x” and “y” axes are
and , and the “z” axis is . The shape of the plot is a bowl. It’s hard to tell where the minimum is from looking at the bowl, so I’ve also plotted contours of the cost function underneath. “Contours” are like the lines on a topographic map. From the contours, it looks like the minimum is at .
-
We could apply the first derivative test. We compute the partial derivative of
with respect to both and :Then, we set both partial derivatives equal to zero and solve for
and :As before, we would have to apply a further test in order to verify that this choice of
is actually a minimum. But it is certainly consistent with what we observed above. Here is a plot of each partial derivative as a function of and . The shape of each plot is a plane (i.e., a flat surface). Both planes are zero at :
An equivalent way of stating this same optimization problem would have been as follows:
You can check that the cost function shown above is the same as the cost function we saw before (e.g., by multiplying it out). We could have gone farther and stated the problem as follows:
We have returned to having just one decision variable
This example is exactly
the same as the previous example, except that the two decision variables
(now renamed
Then, we plug this result into the cost function:
By doing so, we have shown that solving the constrained optimization problem
is equivalent to solving the unconstrained optimization problem
and then taking
The point here was not to show how to solve constrained optimization problems in general, but rather to identify the different parts of a problem of this type. As a quick note, you will sometimes see the example optimization problem we’ve been considering written as
The meaning is exactly
the same, but
“Among all choices of
for which there exists an satisfying , find the one that minimizes .”
Minimum vs. Minimizer
We have seen three example problems. In each case, we were looking for the minimizer, i.e., the choice of decision variable that made the cost function as small as possible:
-
The solution to
was
. -
The solution to
was
. -
The solution to
was
.
It is sometimes useful to focus on the minimum instead of on the minimizer, i.e., what the “smallest value” was that we were able to achieve. When focusing on the minimum, we often use the following “set notation” instead:
-
The problem
is rewritten
The meaning is—find the minimum value of
over all choices of . The solution to this problem can be found by plugging in what we already know is the minimizer, . In particular, we find that the solution is . -
The problem
is rewritten
Again, the meaning is—find the minimum value of
over all choices of and . We plug in what we already know is the minimizer to find the solution—it is . -
The problem
is rewritten
And again, the meaning is—find the minimum value of
over all choices of for which there exists satisfying . Plug in the known minimizer, , and we find that the solution is 15.
The important thing here is to understand the notation and to understand the difference between a “minimum” and a “minimizer.”
Optimal Control
Statement of the problem
The following thing is called an optimal control problem:
Let’s try to understand what it means.
-
The statement
says that we are being asked to choose an input trajectory
that minimizes something. Unlike in the optimization problems we saw before, the decision variable in this problem is a function of time. The notation is one way of indicating this. Given an initial time and a final time , we are being asked to choose the value of at all times in between, i.e., for all . -
The statement
is a constraint. It implies that we are restricted to choices of
for which there exists an satisfying a given initial conditionand satisfying the ordinary differential equation
One example of an ordinary differential equation that looks like this is our usual description of a system in state-space form:
-
The statement
says what we are trying to minimize—it is the cost function in this problem. Notice that the cost function depends on both
and . Part of it— —is integrated (i.e., “added up”) over time. Part of it— —is applied only at the final time. One example of a cost function that looks like this is
The HJB equation (our new “first-derivative test”)
As usual, there are a variety of ways to solve an optimal control problem. One way is by application of what is called the Hamilton-Jacobi-Bellman Equation, or “HJB.” This equation is to optimal control what the first-derivative test is to optimization. To derive it, we will first rewrite the optimal control problem in “minimum form” (see “Minimum vs Minimizer Section”):
Nothing has changed here, we’re just asking for the minimum and not the minimizer. Next, rather than solve this problem outright, we will first state a slightly different problem:
The two changes that I made to go from the original problem to this one are:
-
Make the initial time arbitrary (calling it
instead of ). -
Make the initial state arbitrary (calling it
instead of ).
I also made three changes in notation. First, I switched from
You should think of the problem (2)
as a function itself. In goes an initial time
We call
The reason this equation is called a “recursion” is that
it expresses the function
from the first part and the cost
from the second part (where “
We now proceed to approximate the terms in (4) by first-order series expansions. In particular, we have
and we also have
If we plug both of these into (4), we find
Notice that nothing inside the minimum depends on anything other than
Also, notice that
does not depend
on
To simplify further, we can
subtract
The equation is called the Hamilton-Jacobi-Bellman Equation, or simply the HJB equation. As you can see, it is a partial differential equation, so it needs a boundary condition. This is easy to obtain. In particular, going all the way back to the definition (3), we find that
The importance of HJB is that if you can find a
solution to (5)-(6))—that is, if you can find a function
Solution approach
The optimal control problem
can be solved in two steps:
-
Find
: -
Find
:
LQR
Statement of the problem
Here is the linear quadratic regulator (LQR) problem:
It is an optimal control problem—if you define
then you see that this problem has the same form as (1). It is
called “linear” because the dynamics are those of a linear (state space)
system. It is called “quadratic” because the cost is quadratic (i.e.,
polynomial of order at most two) in
The matrices
What this notation
means is that
Solution to the problem
The “Solution approach” Section tells us to solve the LQR problem in two steps.
First, we find a function
What function
This function has the form
for some symmetric matrix
This is matrix calculus (e.g., see
https://en.wikipedia.org/wiki/Matrix_calculus or Chapter A.4.1 of
http://web.stanford.edu/~boyd/cvxbook/bv_cvxbook.pdf). The result on
the left should surprise no one. The result on the right is the matrix
equivalent of
To evaluate the minimum, we apply the first-derivative test (more matrix calculus!):
This equation is easily solved:
Plugging this back into (7), we have
In order for this equation to be true for any
In summary, we have found that
solves the HJB equation, where
backward in time, starting from
Now that we know
for
LQR Code
Let’s take a look at how to implement LQR using Python. The basic method is shown below:
import numpy as np
from scipy import linalg
def lqr(A, B, Q, R):
P = linalg.solve_continous_are(A, B, Q, R) # Solves the continuous algebraic Riccati equation
K = linalg.inv(R) @ B.T @ P # Generates gain matrix
return K
State estimation
What is a state-space model with output?
All state-space models we have seen so far have had this form:
From now on, we will consider state-space models with this form instead:
We have added one more variable (along with the state
is the output
Like the state and the input, the output is a function of time — we will write
- the output
has elements
Then, we would represent it as a column matrix:
Like
is a constant matrix of size is a constant matrix of size
The state-space model remains both linear and time-invariant, because
Outputs can be used to model a variety of different things. We will use them to model sensor measurements for the purpose of state estimation.
How do I linearize a sensor model?
You already know how to linearize a dynamic model:
- Rewrite the dynamic model as a set of first-order ODEs:
- Choose an equilibrium point
among the solutions to this equation:
- Define the state and input in terms of the equilibrium point:
- Compute
and as the Jacobian of with respect to and respectively, evaluated at the equilibrium point:
The result is a linear approximation
to the nonlinear dynamic model
that is accurate near the equilibrium point.
We can use the same process to linearize a sensor model:
Step 1. Rewrite the sensor model as follows:
In this expression, the variable
Step 2. Define the output as follows:
Note that
Step 3. Compute
Why does this make sense? Just like we took a first-order Taylor’s series expansion about
So, with this choice of
to the nonlinear sensor model
that is accurate near the equilibrium point.
Example (linear sensor model)
Consider again a system with the following dynamic model:
We already showed how to rewrite this dynamic model as
and how to linearize it about the equilibrium point
to produce the state-space model
where
and
Now suppose we have sensors that allow the measurement of
Let’s apply our method to put this measurement in state-space form.
First, we rewrite the measurement in the form
Then, we define the output as the difference between the value of this function and what the value would be at equilibrium:
Finally, we compute
The resulting state-space model is
Note that the original sensor model was linear, so there was no approximation here. We could probably have skipped the entire process of linearization and written the system in state-space form bny inspection. However, just as was true when putting dynamic models in state-space form (see example), it is nice to know that “linearization” still works even in this simple case.
Example (nonlinear sensor model)
Consider a system with the same dynamic model as before, but now suppose we have sensors that allow the measurement of
Again, let’s apply our method to put this measurement in state-space form.
First, we rewrite the measurement in the form
Then, we define the output as the difference between the value of this function and what the value would be at equilibrium:
Finally, we compute
The resulting state-space model is
Optimal Observer Derivation (a.k.a the Kalman Filter, a very famous thing)
These notes were originally written by T. Bretl and were transcribed for this reference page by S. Bout.
Statement of the problem
Here is the deterministic, finite-horizon, continuous-time Kalman Filter (KF) problem—i.e., the optimal control problem that one would solve to produce an optimal observer:
The interpretation of this problem is
as follows. The current time is
The matrices
Just as
with the LQR problem, this notation means is that
By plugging in the expression for
It is an optimal control problem, just like LQR—if you define
then you see that this problem has the general form
There are four differences between this form and the one we saw when solving the LQR problem:
-
The “input” in this problem is
, not . -
The “current time” is
and not . -
The final state—i.e., the state at the current time—is not given. Indeed, the point here is to choose a final state
that best explains and . -
The functions
, , and vary with time (because they have parameters in them— and —that are functions of time).
Because of these four differences, the HJB equation for a problem of this form is
Note the change in sign of both the first term outside the minimum and
the first term inside the minimum—this is because we are effectively
solving an optimal control problem in which time flows backward (from
the current time
Solution to the problem
As usual, our first step is to find a function
Expand the boundary condition:
This function has the form
for some symmetric matrix
Let’s “guess” that this form of
Here again—just as for LQR—we are applying matrix calculus. Plug these partial derivatives into HJB and we have
To evaluate the minimum, we apply the first-derivative test (more matrix calculus!):
This equation is easily solved:
Plugging this back into (1), we have
where the last step is because
In order
for this equation to be true for any
In summary, we have found that
solves the HJB equation,
where
The optimal choice of state
estimate at time
We can find the solution to this problem by application of the first derivative test, with some matrix calculus:
implying that
Let’s call this solution
Suppose we take the time derivative of
this expression, plugging in what we found earlier for
For this equation to hold for any
Behold! This is our expression for an optimal observer, if we define
Finally, suppose we take the limit as
It is customary to write
these last two equations in a slightly different way. In particular,
suppose we pre- and post-multiply both sides of this last equation by
Then, we have
and
Summary
An optimal observer—a deterministic, infinite-horizon, continuous-time Kalman Filter—is given by
where
and
Comparison between LQR and KF (i.e., why you can use “LQR” in Python to compute an optimal observer)
An optimal controller is given by
where
and
An optimal observer is given by
where
and
Take the transpose of (5) and—remembering that
Take the transpose of (6)
and—remembering that
Compare (3) and (4) with (7) and (8). They are exactly the same if we make the following replacements:
-
replace
with -
replace
with -
replace
with -
replace
with -
replace
with
This is the reason why
L = lqr(A.T, C.T, linalg.inv(Ro), linalg.inv(Qo)).T
produces an optimal observer, just like
K = lqr(A, B, Qc, Rc)
produces an optimal controller. WWWWWOOOOOWWWWW!!!!!!!
Remember that:
import numpy as np
from scipy import linalg
def lqr(A,B,Q,R):
P=linalg.solve_continuous_are(A,B,Q,R)
K=linalg.inv(R) @ B.T @ P
return K