forked from yihui/knitr-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
042-highlight-matlab.Rnw
60 lines (50 loc) · 1.49 KB
/
042-highlight-matlab.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
\documentclass{article}
<<adjust-preamble, include=FALSE>>=
library(knitr)
knit_theme$set('solarized-dark')
@
\usepackage{url}
\begin{document}
Normal R chunks.
<<test-R>>=
1+1
x=rnorm(5)
@
Highlight matlab chunks. Note you need to install highlight from \url{http://www.andre-simon.de}, and you probably need to put its binary path into PATH; otherwise just use the engine.path option, e.g.
\noindent\verb|<<engine='highlight', engine.path = 'full/path/to/highlight'>>=|
<<highlight, engine='highlight', engine.opts='-S matlab -O latex'>>=
function Y = kalmanM(pos)
dt=1;
%% Initialize state transition matrix
A=[ 1 0 dt 0 0 0;... % [x ]
0 1 0 dt 0 0;... % [y ]
0 0 1 0 dt 0;... % [Vx]
0 0 0 1 0 dt;... % [Vy]
0 0 0 0 1 0 ;... % [Ax]
0 0 0 0 0 1 ]; % [Ay]
% Initialize measurement matrix
H = [ 1 0 0 0 0 0; 0 1 0 0 0 0 ];
Q = eye(6);
R = 1000 * eye(2);
x_est = zeros(6, 1);
p_est = zeros(6, 6);
numPts = size(pos,1);
Y = zeros(numPts, 2);
for idx = 1:numPts
z = pos(idx, :)';
%% Predicted state and covariance
x_prd = A * x_est;
p_prd = A * p_est * A' + Q;
%% Estimation
S = H * p_prd' * H' + R;
B = H * p_prd';
klm_gain = (S \ B)';
%% Estimated state and covariance
x_est = x_prd + klm_gain * (z - H * x_prd);
p_est = p_prd - klm_gain * H * p_prd;
%% Compute the estimated measurements
Y(idx, :) = H * x_est;
end % of the function
end % of the function
@
\end{document}