% Main file to solve two differential equations of motion
% Solution to a set of dynamic equations of the form:
%
% define the following state equations
%
% x(1) = speed (m/s)
% x(2) = position (m)

% xdot(1) = A + B *x(1) + C * x(1) .^2;   % acceleration of train (m/s-s)
% xdot(2) = x(1);                                     % velocity of train (m/s)
%
% subject to initial conditions:
%
% x (t=0) = xo

% xo(1) = 0         % initial speed of the train
% xo(2) = 0         % initial distance traveled by the train
%
% where: 

global A B C 

% Define Initial Conditions of the Problem

xo = [0 0];                     % xo are the initial velocity and distance traveled by the train
to = 0.0;                        % to is the initial time to solve this equation
tf = 200;                        % tf is the final time

% Define the coefficients of the acceleration function (A, B, and C)

% dV/dt = A * V^2 + B * V + C

% where: 
% dV/dt = acceleration of the train asa function of time (m/s-s)

% Previously obtained as: coefficients = 0.0000   -0.0268    2.0997
% from highest order to lowest (A associated with x(1) ^2, B with x(1) and
% C if the constant term)

A = 0.0;		   % coefficient of acceleration function (2nd power)
B = -0.0268;	% coefficient of acceleration function (1st power)
C = 2.0997;		% coefficient of acceleration function (constant)

tspan =[to tf];

[t,x] = ode45('trainDynamics',tspan,xo);	% call ODE solver

% Plot the results of the numerical integration procedure

figure
plot(t,x(:,1))
xlabel('Time (seconds)','fontsize',20)
ylabel('Velocity Profile of the Train (m/s)','fontsize',20)
grid


figure
plot(t,x(:,2))
xlabel('Time (seconds)','fontsize',20)
ylabel('Distance Traveled (m)','fontsize',20)
grid