The below is the drawing
of the mass-spring system. We will solve for the homogeneous solution of
the system, which means the external excitation force is zero. As you all
know, this homogeneous solution corresponds to the transient response of
the system. We will define the natural frequency of the system. After that,
we will set up equation of motion (FBD, force balance), get the closed
form of the solution and try to solve it using Matlab.
In this example, we will
use both closed form solution and ODE45 solver to compare the results.
(If you have no idea what ODE45 solver is, then click
here.)
From the free body digram we can write the force balance equations as follow. Consider the right-going direction as the positive x-direction to get
| (1) |
where m is the mass of the system, k is the stiffness of the spring, and F(t) is the external force acting on the mass.
However, in the current problem we are focusing on the homogeneous solution, thus the external force is zero, i.e. F(t)=0. Then the above equation becomes
| (2) |
and we have an equation of motion for the mass-spring system for the
homogeneous solution. We can define the natural frequency of the system,
,
as
| (3) |
Then the equation of motion becomes
![]() |
(4) |
Now we need to think about the closed form solution of the equation
above. The solution form can be written as a sinusoidal function. Therefore,
| (1) |
| (2) |
where A and
are amplitude and phase angle respectively. (Cosine was an arbitrary choice
and sine would work equally well.)
A and
are unknown and must be determined using the initial conditions x(0)=x0
and
.
In terms of these initial conditions, A and
become
![]() |
(3) |
| (4) |
Therefore, with the initial conditions given, we have a complete closed
form solution for the response of the system.
We can code the closed
form solution into MATLAB. The following is the MATLAB code for closed
form solution of mass-spring system.
At the beginning of the program, we need to write some constant inputs. n is the number of the data points.
m = 1; % mass of the system in meter k = 50; % stiffness of the spring (N/m) wn = sqrt(k/m); % natural frequency (rad/sec) t_final = 2; % calculation time n = 500; % number of data pointsThen, initial conditions are followed.
x_0 = 1; % initial displacement x_dot_0 = 0; % initial velocityCalculate the amplitude and the phase angle with given initial conditions.
X_0 = sqrt(x_0^2+(x_dot_0/wn)^2); phi = acos(x_0/X_0);And we can determine the displacement x(t) and velocity x_dot(t).
t = 0:t_final/n:t_final; x = X_0*cos(wn*t+phi); % displacement x(t) x_dot = -wn*x_0*sin(wn*t+phi); % velocity x_dot(t)Click here for the entire MATLAB source code for closed form solution and plotting.
Here we will use the
ODE45 solver in Matlab to integrate the equations of motion. The following
is the MATLAB code for ODE45 solver solution of mass-spring system. If
you need to see the basic operation of ODE45 solver, see Matlab
Assistance.
Some constant inputs need to be fixed before we proceed.
m = 1; % mass of the system in meter k = 50; % stiffness of the spring (N/m) wn = sqrt(k/m); % natural frequency (rad/sec) t_final = 2; % calculation timeAnd the initial conditions were shown. tol is the tolerance for ODE45 solver. Higher it is, more accurate the answer will be, more time the calculation takes.
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 tol = 1e-6; % tolerance of error for ODE45 solverUsing ODE45 solver, we can solve for the displacement x(t) and velocity x_dot(t). y is used as a dummy variable.
[t,y] = ode45('pms_sol',0,t_final,X_0,tol);
x = y(:,1); % displacement x(t)
x_dot = y(:,2); % velocity x_dot(t)
Here's the subprogram pms_sol.m.
function ydot = pms_sol(t,y) m = 1; % mass of the system in meter k = 50; % stiffness of the spring (N/m) yd1 = y(2); yd2 = -k*y(1)/m; ydot = [yd1;yd2];Here are the complete list of the main program (PC,Mac), main program (Unix) and subprogram. The main programs also include the plotting commands.
Results are given in
2 parts. The first gives the plotting commands and results using the closed
form solution, the second gives the plotting commands and results using
the ODE solver. Just as you may have guessed, both methods should give
the same answers. Check them out.