forked from idaohang/borre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrls.m
58 lines (48 loc) · 1.33 KB
/
rls.m
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
function rls(A,b,Sigma)
%RLS Recursive Least Squares
% A is the coefficient matrix, b the observations and
% Sigma a vector containing the diagonal entries of
% the covariance matrix for the problem.
% We include one additional observation for increasing i by 1
%Copyright (c) by Kai Borre
%$Revision: 1.0 $ %Date:1999/10/24 $
if nargin == 0
A = [1 1;1 2;-1 1];
b = [2;1;0];
Sigma = diag([1,.5,1]);
end
% Initial weight
P = A(1,:)'*Sigma(1,1)*A(1,:);
if rcond(P) == 0
P = 1.e10*eye(size(A,2));
else
P = inv(P);
end
P
% Initial solution
x = pinv(A(1,:)'*Sigma(1,1)*A(1,:))*A(1,:)'*Sigma(1,1)*b(1)%;
for i = 1:size(b,1)
K = P*A(i,:)'*inv(A(i,:)*P*A(i,:)'+Sigma(i,i))%;
P = (eye(size(A,2))-K*A(i,:))*P%;
x = x+K*(b(i)-A(i,:)*x)%;
% fprintf('\nSolution:\n');
% for j = 1:size(A,2)
% fprintf(' x(%2g) = %6.3f\n',j,x(j));
% end
end
break
dof = size(b,1)-size(A,2);
if dof ~= 0
P = (norm(b-A*x))^2*P/dof;
else
P = (norm(b-A*x))^2*pinv(A'*Sigma*A);
end
fprintf('\nFinal Covariance matrix:\n');
for j = 1:size(A,2)
for k = 1:size(A,2)
fprintf('%12.7f',P(j,k));
end
fprintf('\n');
end
fprintf('\nTrace of Covariance matrix: %12.7f\n',trace(P));
%%%%%%%%%%%%%%%%% end rls.m %%%%%%%%%%%%%%%%%%%