% mass_spring_script.m -->  with mass_spring.mdl
%
% script for Simulink tutorial for mass-spring-damper example
% For EGR 326 class example, Feb 2017
% 
% Author: J Cardell
% Date: Spring 2017

% DEFINE CONSTANTS, INITIAL CONDITIONS and SYSTEM MATRICES
% You must open the associated block in your Simulink model and enter the
% variable name, such as 'k', 'm', 'x1_0' etc. These variables then obtain
% their assigned values in the Matlab script.

% Constants / Parameters
k = 2;   % spring constant
m = 1;   % mass
b = 0.5; % damping coefficient
Fin = 1;

% Initial conditions
x1_0 = 0;
x2_0 = 0;

% SIMULATE THE DYNAMIC SYSTEM
% run the simulink model, mass_spring.mdl, defining the start and stop
% times for the simulation
sim('mass_spring');
set_param('mass_spring','StartTime','0','StopTime','25')

% PLOT SYSTEM BEHAVIOR
figure
hold on
% Define vectors to be used for output plot
% NOTE that to obtain the 'time' vector you must select the "structure with
% time" option from the appropriate "To Workspace" block in your model
t2 = sim_in.time;
u2 = sim_in.signals.values;
y2 = sim_pos.signals.values;

% Create and label plot of system behavior
plot(t2, y2, 'LineWidth', 2)    
plot(t2, u2, 'r-', 'LineWidth', 2)
title('Response of Mass-Spring System')
xlabel('t (sec)')
ylabel('y (m)')
grid
hold off


% ** EGR 326 TASK **
% Add plots of velocity and acceleration
%
