ODE45 solver solution : main program (Unix)

%
%   Mass-Spring system 
%   ODE45 solver solution
%   ODE45 main Program
%

%
%   constant inputs
%

m = 1;                      %  mass of the system in meter
k = 50;                     %  stiffness of the spring (N/m)
wn = sqrt(k/m);             %  natural frequency (rad/sec)

tspan = [0,2];              %  calculation time

%
%   initial conditions
%

x_0 = 1;                    %  initial displacement
x_dot_0 = 0;                %  initial velocity

X_0 = [x_0,x_dot_0];        %  form a vector(array) of initial conditions

%
%   ODE45 solver
%

[t,y] = ode45('pms_sol',tspan,X_0);

x = y(:,1);                 %  displacement x(t)
x_dot  = y(:,2);            %  velocity x_dot(t)
   
%
%   plotting the results 
%

figure(1); clf; orient tall;
subplot(2,1,1),plot(t,x);
xlabel('Time (sec.)');
ylabel('Displacement (m)');
title('Mass-Spring system : ODE45 solver result')

subplot(2,1,2),plot(t,x_dot);
xlabel('Time (sec.)');
ylabel('Velocity (m/sec.)');