function theta = SequentialLSExample2 (Nmax, sigma2) a = -6; b = -0.6; c = 0.004; theta_prev = zeros(3,1); cov_prev = sigma2*eye(3); for n=1:Nmax x(n) = a + b*n + c*n.^2 + sqrt(sigma2)*randn(); ht = [1 n n^2]; K = (cov_prev*ht')/(sigma2+ht*cov_prev*ht'); % we assume all data points have the same variance theta = theta_prev + K*(x(n)-ht*theta_prev); cov_prev = (eye(3)-K*ht)*cov_prev theta_prev = theta; figure(1);clf;hold on; plot(x,'*-'); xfit = theta(1) + theta(2)*(1:Nmax) + theta(3)*(1:Nmax).^2; plot(xfit,'ro-'); axis([0 Nmax -100 100]); pause(0.1); end;