% Code to plot simulation results from sdl_suspension_comparison
%% Plot Description:
%
% The plot below shows the position of the sprung masses in three different
% suspension models that are subjected to the same test.

% Copyright 2016 The MathWorks, Inc.

% Generate simulation results if they don't exist
if ~exist('simlog_sdl_suspension_comparison', 'var')
    sim('sdl_suspension_comparison')
end

% Reuse figure if it exists, else create new figure
if ~exist('h1_sdl_suspension_comparison', 'var') || ...
        ~isgraphics(h1_sdl_suspension_comparison, 'figure')
    h1_sdl_suspension_comparison = figure('Name', 'sdl_suspension_comparison');
end
figure(h1_sdl_suspension_comparison)
clf(h1_sdl_suspension_comparison)

% Get simulation results
simlog_t = simlog_sdl_suspension_comparison.Sensor_Sprung_Mass_SA.Motion_Sensor.x.series.time;
simlog_xSprSA = simlog_sdl_suspension_comparison.Sensor_Sprung_Mass_SA.Motion_Sensor.x.series.values('m');
simlog_xSprNL = simlog_sdl_suspension_comparison.Sensor_Sprung_Mass_NL.Motion_Sensor.x.series.values('m');
simlog_xSprVa = simlog_sdl_suspension_comparison.Sensor_Sprung_Mass_Var.Motion_Sensor.x.series.values('m');

% Plot results
plot(simlog_t, simlog_xSprSA, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_xSprNL, 'LineWidth', 1)
plot(simlog_t, simlog_xSprVa, 'LineWidth', 1)
hold off
grid on
title('Sprung Mass Position')
ylabel('Position (m)')
legend({'Shock Absorber','Nonlinear Spring Damper','Variable Spring Damper'},'Location','Best');
xlabel('Time (s)')

% Remove temporary variables
clear simlog_t
clear simlog_xSprSA simlog_xSprNL simlog_xSprVa

