forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlsdata.m
95 lines (74 loc) · 1.4 KB
/
rlsdata.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function out = lsqdata( )
%A demo to show recursive least squares parameter estimation and prediction.
clf;
numpts = 200;
order = 2;
noise = 2.0;
theta = [0.6;0.4];
disp('True parameter vector = ')
disp(theta');
y=10 + noise*(rand(1,order)-0.5);
for i=order+1:numpts,
for j=1:order,
phip(j) = y(i-j);
end
phi=phip';
y(i)=theta'*phi + noise*(rand(1)-0.5);
end
x=[1:numpts];
plot(x,y,'r-');
disp('Press any key to plot predicted signal');
pause
hold on;
trsize = floor(numpts/20);
for i=1:order+1,
ytr(:,i)=y(i:trsize+i-1)';
end
for i=1:order,
A(:,i)=ytr(:,order-i+1);
end
b = ytr(:,order+1);
thetahat=(A'*A)\(A'*b);
disp('Estimate of parameter vector = ')
disp(thetahat');
for j=1:order,
yhat(j)=y(j);
end
for i=order+1:numpts,
for j=1:order,
phip(j) = y(i-j);
end
phi=phip';
yhat(i)=thetahat'*phi;
end
x=[1:numpts];
plot(x,yhat,'b-.');
disp('Press any key to plot RLS predicted signal');
pause
for i=1:order+1,
ytr2(:,i)=y(i:order+i-1)';
end
for i=1:order,
A2(:,i)=ytr2(:,order-i+1);
end
b2 = ytr2(:,order+1);
P=inv(A2'*A2);
that=P*A2'*b2;
for j=1:order,
yhat(j)=y(j);
end
for i=order+1:numpts,
for j=1:order,
phip(j) = y(i-j);
end
phi=phip';
yhat(i)=that'*phi;
P=P-(P*phi*phi'*P)/(1+phi'*P*phi);
that=that+P*phi*(y(i)-phi'*that);
%disp('that =');
%disp(that');
end
disp('Estimate of parameter vector = ')
disp(that');
x=[1:numpts];
plot(x,yhat,'g-');