The Extended Kalman Filter (EKF) is a state estimation algorithm that enables estimation of nonlinear systems. In this answer, we will cover how to implement the EKF in MATLAB for state estimation of nonlinear systems.
The implementation of the EKF can be divided into two parts: the prediction step and the update step.
In the prediction step, the state of the system is predicted using the system model. The state is predicted using the following equation:
x(k|k-1) = f(x(k-1|k-1), u(k-1))
where x(k|k-1)
is the predicted state at time k
, x(k-1|k-1)
is the estimated state at time k-1
, and u(k-1)
is the input at time k-1
.
The covariance matrix of the state estimate is also predicted using the following equation:
P(k|k-1) = F(k-1)P(k-1|k-1)F(k-1)' + Q(k-1)
where F(k-1)
is the Jacobian matrix of the system model at x(k-1|k-1)
and Q(k-1)
is the process noise covariance matrix.
In the update step, the predicted state estimate is corrected using the measurement z(k)
. The corrected state estimate is computed using the following equation:
x(k|k) = x(k|k-1) + K(k)(z(k) - h(x(k|k-1)))
where x(k|k)
is the updated state estimate, z(k)
is the measurement, h
is the measurement model, and K(k)
is the Kalman gain.
The Kalman gain is computed using the following equation:
K(k) = P(k|k-1)H(k)'(H(k)P(k|k-1)H(k)' + R(k))^-1
where H(k)
is the Jacobian matrix of the measurement model at x(k|k-1)
and R(k)
is the measurement noise covariance matrix.
The covariance matrix of the updated state estimate is also computed using the following equation:
P(k|k) = (I - K(k)H(k))P(k|k-1)
The following code implements the EKF in MATLAB for state estimation of a nonlinear system:
main.m1844 chars72 lines
This code applies the EKF to estimate the state of a nonlinear system with two inputs and two measurements. The state of the system is estimated with a reasonable accuracy, as shown in the plots.
gistlibby LogSnag