c4dynamics.filters.kalman.kalman.update#
- kalman.update(z: ndarray, R: ndarray | None = None)[source]#
Updates (corrects) the state estimate based on the given measurements.
- Parameters:
z (numpy.ndarray) – Measurement vector.
R (numpy.ndarray, optional) – Measurement noise covariance matrix. Defaults to None.
- Returns:
K (numpy.ndarray) – Kalman gain.
- Raises:
ValueError – If the number of elements in z does not match the number of rows in the measurement matrix H.
ValueError – If R is missing (neither provided during construction nor passed to update).
Examples
For more detailed usage, see the examples in the introduction to the
filters
module and thekalman
class.Import required packages:
>>> from c4dynamics.filters import kalman
Plain update step (update in steady-state mode where the measurement covariance matrix remains and is provided once during filter initialization):
>>> kf = kalman({'x': 0}, P0 = 0.5**2, F = 1, H = 1, Q = 0.05, R = 200, steadystate = True) >>> print(kf) [ x ] >>> kf.X [0] >>> kf.P [[3.187...]] >>> kf.update(z = 100) # returns Kalman gain [[0.0156...]] >>> kf.X [1.568...] >>> kf.P [[3.187...]]
Update with modified measurement noise covariance matrix:
>>> kf = kalman({'x': 0}, P0 = 0.5**2, F = 1, G = 150, H = 1, R = 200, Q = 0.05) >>> kf.X [0] >>> kf.P [[0.25]] >>> K = kf.update(z = 150, R = 0) >>> K [[1]] >>> kf.X [150] >>> kf.P [[0]]