% Kalman Filter for Beginners - Temperature Tracking Example clear; clc; close all;% True state (constant temperature) true_temp = 25;
% Simulation parameters dt = 1; % time step (seconds) T = 50; % total time steps
% Noise parameters process_noise_std = 0.5; % uncertainty in model (e.g., window opens) measurement_noise_std = 2; % sensor noise kalman filter for beginners with matlab examples download
% Initial guess x_est = 20; % initial estimate (wrong on purpose) P_est = 5; % initial uncertainty (high)
% Storage x_history = zeros(1,T); meas_history = zeros(1,T); Output figure : Smoother curve than raw measurements
for k = 1:T % --- Simulate measurement (with noise) --- z = true_temp + measurement_noise_std * randn; meas_history(k) = z;
% --- Prediction step --- % For constant temperature, prediction = previous estimate x_pred = x_est; P_pred = P_est + process_noise_std^2; % --- Kalman gain --- K = P_pred / (P_pred + measurement_noise_std^2); % --- Update step --- x_est = x_pred + K * (z - x_pred); P_est = (1 - K) * P_pred; x_history(k) = x_est;end
% Plot results time = 1:T; plot(time, true_temp*ones(1,T), 'k--', 'LineWidth', 2); hold on; plot(time, meas_history, 'ro', 'MarkerSize', 4); plot(time, x_history, 'b-', 'LineWidth', 1.5); legend('True Temp', 'Noisy Measurements', 'Kalman Filter Estimate'); xlabel('Time step'); ylabel('Temperature (°C)'); title('Kalman Filter for Beginners: Temperature Tracking'); grid on;
You will see intimidating algebra online. Let’s demystify it. There are only 5 equations.