% Main file to estimate the speed and distance profiles of 
% a mass-spring-damper system

clear
close all
clc

% Solution to a set of two ODE equations of the form:
%
% ydot(1) = y(2);
% ydot(2) = F(t)/m - k/m y(1) - b/m y(2)
%
% subject to initial conditions:
%
% y (t=0) = yo
%
% where: 

global m b k 

% Define Initial Conditions of the Problem

yo = [0 0];                     % yo are the initial displacement and velocity
to = 0.0;                       % to is the initial time to solve this equation
tf = 15.0;                       % tf is the final time

% define m and b

m = 3000;			% mass (kg)
b = 2000;			    % damper constant (N / (m/s))
k = 40000;			% spring constant (N/m)
tspan =[to tf];

[t,y] = ode23('fy1',tspan,yo);	% call ODE solver

% Plot the results of the numerical integration procedure

figure
plot(t,y(:,1))
xlabel('Time (seconds)','fontsize',24)
ylabel('Position (m)','fontsize',24)
title('Displacement of a Spring-Mass-Damper System')
grid

figure
plot(t,y(:,2))
xlabel('Time (seconds)','fontsize',24)
ylabel('Speed (m/s)','fontsize',24)
title('Speed Response of a Spring-Mass-Damper System')
grid