In [2]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
%pylab inline --no-import-all
from sympy import Symbol, Matrix, eye, latex
from sympy.interactive import printing
printing.init_printing()
In [3]:
def get_matrix(name='M', n=3, m=3):
if n <= 1:
return Matrix([[Symbol(name + '_{ ' + str(i) + '}' ) for i in range(m)]])
elif m <= 1:
return Matrix([[Symbol(name + '_{ ' + str(j) + '}' ) ] for j in range(n)])
else:
return Matrix([[Symbol(name + '_{ ' + str(i) + str(j) + '}' ) for j in range(m)] for i in range(n)])
In [4]:
# State
n = 3
# Inputs
p = 1
# Measurement
m = 1
In [5]:
x = get_matrix('x', n, 1)
x
Out[5]:
In [6]:
u = get_matrix('u', p, 1)
u
Out[6]:
In [7]:
z = get_matrix('z', m, 1)
z
Out[7]:
In [8]:
A = get_matrix('A', n, n)
A
Out[8]:
In [9]:
B = get_matrix('B', n, p)
B
Out[9]:
In [10]:
H = get_matrix('H', m, n)
H
Out[10]:
In [11]:
Q = get_matrix('Q', n, n)
Q
Out[11]:
In [12]:
R = get_matrix('R', m, m)
R
Out[12]:
In [13]:
P = get_matrix('P', n, n)
P
Out[13]:
In [14]:
# Model
dt = Symbol('\Delta t')
A[0,0] = 1
A[0,1] = dt
A[0,2] = 0.5*dt**2
A[1,0] = 0
A[1,1] = 1
A[1,2] = dt
A[2,0] = 0
A[2,1] = 0
A[2,2] = 1
A
Out[14]:
In [15]:
# Input
B[0,0] = 0.5*dt**2
B[1,0] = dt
B[2,0] = 0
B
Out[15]:
In [16]:
# Measurement
H[0,0] = 1
H[0,1] = 0
H[0,2] = 0
# H[1,0] = 1
# H[1,1] = 0
# H[1,2] = 0
# H[2,0] = 1
# H[2,1] = 0
# H[2,2] = 0
H
Out[16]:
In [17]:
# Process covariance
s1 = Symbol('\sigma_{proc}')
v = Matrix([[0.5*dt**2], [dt], [1]])
Q = v*v.T*s1**2
Q
Out[17]:
In [18]:
# Measurement covariance
# remove non-diagonal terms
for i in range(m):
for j in range(m):
if i!=j:
R[i, j] = 0
R
Out[18]:
In [19]:
A*x + B*u
Out[19]:
In [20]:
A*P*A.T + Q
Out[20]:
In [21]:
S = H * P * H.T + R
#print(S.shape)
#S
In [22]:
K = (P*H.T) * S.inv()
#print(K.shape)
#K
In [23]:
y = z - (H*x)
#print(y.shape)
#y
In [24]:
x + (K*y)
Out[24]:
In [25]:
I = eye(n)
(I - (K*H))*P
Out[25]:
In [25]: