ODE45 solver solution : main program


%
%   Mass-Spring-Damper system 
%   ODE45 solver solution 
%   ODE45 main program
%

clear all;

%
%   constant inputs
%

m = 2;                      %  mass of the system in meter
k = 5000;                   %  stiffness of the spring (N/m)
c = 30;                     %  damping coefficient
wn = sqrt(k/m);             %  natural frequency (rad/sec)
zeta = c/(2*wn*m)           %  viscous damping factor

t_final = 3;                %  calculation time

%
%   initial conditions
%
x_0 = 0;                 %  initial displacement
xdot_0 = 0;              %  initial velocity

X_0 = [x_0,xdot_0];        %  form a vector(array) of initial conditions
tol = 1e-6;                 %  tolerence of error for ODE45 solver 

%
%   ODE45 solver
%

[t,y] = ode45('forcedsub',0,t_final,X_0,tol);

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

%
%   plotting the results 
%

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