# Demo entry 6714359

-

Submitted by anonymous on Feb 12, 2018 at 23:21
Language: Matlab. Code size: 1.4 kB.

```% Essential data has been imported before running this script.
%% Defining fundamental parameters
sz = [length(x), 1];

%% Calculating Q
q = std(x-x_);
Q = q^2;

%% Initializing R
r = std(x-x_);
R = r^2;

%% Calculating the best results under the given Q and R
xhat = myKalmanCore(x, x_, sz, Q, R);

%% Iteration
R_last = R;
r = std(x-xhat);
R = r^2;
xhat = myKalmanCore(x, xhat, sz, Q, R);
if abs(R-R_last)/R_last > 0.02
figure();
grid on
end
while(abs(R-R_last)/R_last > 0.2 && R > 1e-4)
R_last = R;
xhat = myKalmanCore(x, xhat, sz, Q, R);
r = std(x-xhat);
R = r^2;
plot(xhat)
hold on
%     abs(R-R_last)/R_last
end
Q_last = Q;
q = std(x-xhat);
Q = q^2;
while(abs(Q-Q_last)/Q_last > 0.2 && Q > 1e-4)
Q_last = Q;
xhat = myKalmanCore(x, xhat, sz, Q, R);
q = std(x-xhat);
Q = q^2;
plot(xhat)
hold on
end

%% Displaying the results
FontSize=14;
LineWidth=3;
figure();
plot((1960+50-length(x)):2009, x_, 'k+')
grid on
hold on
plot((1960+50-length(x)):2009, xhat, 'b-', 'LineWidth', LineWidth)
hold on
plot((1960+50-length(x)):2009, x, 'g-', 'LineWidth', LineWidth)
legend('Prediction', 'Correction', 'Statistics')
% axis([1960, 2010, 3e5, 9e5])
xtext = xlabel('Year');
ytext = ylabel('Billion Btu');
title('Renewable Energy Use in CA')
set(gca, 'FontSize', FontSize)
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.