% Define the system model equations
function x_k = systemModel(x_k_prev,u_k_prev)
x_k = [x_k_prev(1) + cos(x_k_prev(3))*u_k_prev(1);
x_k_prev(2) + sin(x_k_prev(3))*u_k_prev(1);
x_k_prev(3) + u_k_prev(2)];
end
% Define the measurement model equations
function z_k = measurementModel(x_k)
z_k = [sqrt(x_k(1)^2 + x_k(2)^2);
atan2(x_k(2),x_k(1))];
end
% Initialize the state vector, process noise covariance matrix, and measurement noise covariance matrix
x = [0;0;0];
Q = diag([0.1,0.1,0.01]);
R = diag([0.1,0.1]);
% Create the extended Kalman filter object
ekf = extendedKalmanFilter(@systemModel,@measurementModel,x,Q,R);
% Input the measurement data and run the filter
measurements = [1.2, 0.5;
1.6, 1.2;
2.1, 2.0;
2.6, 3.0;
3.2, 4.5];
num_measurements = size(measurements,1);
x_est = zeros(3,num_measurements);
P_est = zeros(3,3,num_measurements);
for k = 1:num_measurements
z = measurements(k,:)';
[x,P] = predictAndUpdate(ekf,z);
x_est(:,k) = x;
P_est(:,:,k) = P;
end
% Plot the results
t = 1:num_measurements;
figure
subplot(311)
plot(t,x_est(1,:),'-b',t,sqrt(measurements(:,1).^2 + measurements(:,2).^2),'--r')
ylabel('Position (m)')
legend('Estimated','True')
subplot(312)
plot(t,x_est(2,:),'-b',t,atan2(measurements(:,2),measurements(:,1)),'--r')
ylabel('Heading (rad)')
legend('Estimated','True')
subplot(313)
plot(t,x_est(3,:),'-b')
ylabel('Speed (m/s)')
xlabel('Time (s)')
In this implementation, the system model equations describe a simple motion model for a robot moving in a 2D plane with a constant speed and rotational velocity. The measurement model equations describe how the robot's position and heading can be measured using range and bearing sensors. The extended Kalman filter object is created using the system model and measurement model functions, and is then run using a series of measurements. The results are plotted to show how the filter estimates the robot's position, heading, and speed over time.