% Initialize the state and covariance x0 = [0; 0]; P0 = [1 0; 0 1];
% Plot the results plot(t, x_true(1, :), 'b', t, x_est(1, :), 'r') legend('True state', 'Estimated state') % Initialize the state and covariance x0 =
% Plot the results plot(t, x_true(1, :), 'b', t, x_est(1, :), 'r') legend('True state', 'Estimated state') P0 = [1 0
% Implement the Kalman filter x_est = zeros(2, length(t)); P_est = zeros(2, 2, length(t)); x_est(:, 1) = x0; P_est(:, :, 1) = P0; for i = 2:length(t) % Prediction step x_pred = A * x_est(:, i-1); P_pred = A * P_est(:, :, i-1) * A' + Q; % Measurement update step K = P_pred * H' / (H * P_pred * H' + R); x_est(:, i) = x_pred + K * (z(i) - H * x_pred); P_est(:, :, i) = (eye(2) - K * H) * P_pred; end % Plot the results plot(t