Free Falling Body Kalman Filter Matlab

I have this data. The table shows free falling body examples. I have to plot a graph for "True values" Position, x1, Estimates Position x1(k), and Measurement z(k). How can I plot this table in MATLAB?

 Accepted Answer

plot(t, x1True, 'b.-', 'LineWidth', 2);
plot(t, x1Estimate, 'r.-', 'LineWidth', 2);
ylabel('x1', 'FontSize', 18);
xlabel('t', 'FontSize', 18)
grid on;
legend('x1 True', 'x1 Estimate');
I'm not sure what you're calling Measurement z, but you'd use the plot() function like above.

4 Comments

Thank you for your answer. Act, I want the graph to look like this.
Why I don't get the same as the above graph? Help. Here I attach my Matlab coding.
%% Simulate the true signal and measurement
% Define the system
N = 6; % number of time steps
dt = 1; % Sampling time (s)
t = dt*(1:N); % time vector (s)
F = [1, dt; 0, 1]; % system matrix - state
G = [-1/2*dt^2; -dt]; % system matrix - input
H = [1 0]; % observation matrix
Q = [0, 0; 0, 0]; % process noise covariance
u = -1; % input = acceleration due to gravity (m/s^2)
I = eye(2); % identity matrix
% Define the initial position and velocity
y0 = 100; % m
v0 = 0; % m/s
% Initialize the state vector (true state)
xt = zeros(2, N); % True state vector
xt(:, 1) = [y0; v0]; % True intial state
% Loop through and calculate the state
for k = 2:N
% Propagate the states through the prediction equations
xt(:, k) = F*xt(:, k-1) + G*u;
end
% Generate the noisy measurement from the true state
R = 1; % m^2/s^2
v = sqrt(R)*randn(1, N); % measurement noise
z = H*xt + v; % noisy measurement
%% Perform the Kalman filter estimation
% Initialize the state vector (estimated state)
x = zeros(2, N); % Estimated state vector
x(:, 1) = [105; 0]; % Guess for initial state
% Initialize the covariance matrix
P = [10, 0; 0, 0.01]; % Covariance for initial state error
% Loop through and perform the Kalman filter equations recursively
for k = 2:N
% Predict the state vector
x(:, k) = F*x(:, k-1) + G*u;
% Predict the covariance
P = F*P*F' + Q;
% Calculate the Kalman gain matrix
K = P*H'/(H*P*H' + R);
% Update the state vector
x(:,k) = x(:,k) + K*(z(k) - H*x(:,k));
% Update the covariance
P = (I - K*H)*P;
end
%% Plot the results
% Plot the states
figure(1);
plot(t, z, 'g-', t, x(1,:), 'b--', 'LineWidth', 2);
hold on; plot(t, xt(1,:), 'r:', 'LineWidth', 1.5)
xlabel('t (s)'); ylabel('x_1 = h (m)'); grid on;
legend('Measured','Estimated','True');
I'd guess that the first graph was made by an entirely different program. Do you have that code? Why do you think yours should look the same?
Its because the first graph is the exact graph for the question (data given in the table). I don't have the code for the question.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!