MAE 4314

MATLAB Example for time response of a single degree of freedom, spring-mass-damper system.

Instructions

Create the MATLAB instructions shown below and store them in '*.m files'.

When you create your 'm-files', make sure the files have the extension 'm'. If you use notepad, remember to change the extension from 'txt' to '*' before saving, and after saving check the file name using windows explorer or DOS dir.

If you use the MATLAB file editor, these details are taken care of for you.

For this second order system example two 'm-files' are needed; one to setup the problem (sdofdr.m), and one to define the differential equations to be solved (sdof.m). These files are shown below.

In the MATLAB interactive window type 'sdofdr' to run the program. The plot is shown below.


% driver mfile 'sdofdr.m' for sdof example. 
%set time time span for solution [t0, tfinal]
tspan = [0; 10];
%  initial conditions on x
x0 = [0; 0];
% Call Runge-Kutta integration routine. Store time in vector t, 
% solution in vector x.
[t,x] = ode45('sdof', tspan, x0);
%Plot results
%plot(t,x)
%To show the displacement in dashed black and the velocity in red use:
  plot(t, x(:,1), 'k--')
  hold
  plot(t, x(:,2), 'r')
  hold
grid on
title('Single DOF System Subject to a Step Input')
xlabel('Time (s)')
ylabel('Displacement, Velocity')

function xdot = sdof(t, x)
% file sdof.m  describes the state space representation of a 
% spring-mass-damper, single degree of freedom system with 
% a unit step input.
%parameters
m = 1.0;
c = 0.75;
k = 14.0;
p = 1.0;

xdot(1) = x(2);
xdot(2) = (p - c*x(2) - k*x(1))/m;
% store xdot as a vector
xdot = [xdot(1); xdot(2)];

The first plot commands  give.

sdof1.bmp (692178 bytes)

The second plotting option gives.

sdof2.bmp (692178 bytes)