Chapter 4
Mass-Spring-Damper Oscillations
4.1 Introduction
The purpose of this chapter is to familiarize MATLAB® users to the process of
animating a physical system that has more than one component in a virtual reality
environment. This chapter will walk the reader through a complete exercise on how
to animate a physical problem that consists of two or more objects governed by an
equation(s) of motion in VRML environment using M-script commands.
In Sect. 4.2, a mass-spring-damper problem will be presented where the mass is
attached to a spring and subjected to an initial displacement. Newton’s second law
will be used to derive the equation of motion.
In Sect. 4.3, step-by-step instructions will guide the reader through drawing the
virtual scene in VRML. The components of the system that will be drawn are the
spring, the mass, the floor, and the wall.
In Sect. 4.4, the mass will be colored, and a new texture will be added to the floor
and wall.
Section 4.5 will include a description for the M-script files that will numerically
integrate for the equation of motion.
Section 4.6 will include a description of the M-script file that will animate the
virtual scene based on the numerical solution from the previous section. In addition,
the script will record the animation into a video file.
The chapter concludes by an application problem of two masses connected by a
spring. The problem will include constructing the virtual scene in addition to ani-
mating the physical system based on a set of governing differential equations.
The electronic version of all the M-script files and VRML models in addition to
the recorded movie for the mass-spring-damper and the oscillations of the two
masses can be downloaded from Springer’s web site [Link]
Matlab® is a registered trademark of The Mathworks, Inc.
N. Khaled, Virtual Reality and Animation for MATLAB® and Simulink® Users: 21
Visualization of Dynamic Models and Control Simulations,
DOI 10.1007/978-1-4471-2330-9_4, © Springer-Verlag London Limited 2012
22 4 Mass-Spring-Damper Oscillations
4.2 Mass-Spring-Damper Problem
Given a cube of mass m = 1 (kg) and of dimensions 2 ´ 2 ´ 2 (m 3 ) (Fig. 4.1). The
cube is connected to a spring of stiffness K = 1 (N/m) and a damping constant
(friction) C = 0.1 (Ns/m).
The motion of the system is constrained in the horizontal plane of the ground.
The cube oscillates along the x-direction (horizontal direction of the screen). Using
Newton’s second law, the following second-order differential equation for the x-dis-
placement of the mass can be developed:
K C
mx(t ) + Cx (t ) + Kx(t ) = 0 Þ
x (t ) = - x(t ) - x (t ) (4.1)
m m
The purpose of this exercise is to animate the solution of this equation for
(0)
x = 2 (m/s) and x(0) = 0 (m). Note that the mass is oscillating about the equi-
librium position shown in Fig. 4.1 (nonextended position of the spring).
4.3 Creating the Virtual Scene for the Mass-Spring-Damper
The building blocks for this system are the mass, the spring, the wall (where the
spring is attached), and the floor (Fig. 4.1). Since there are some shapes that are not
available in the VRML, one might need to draw these shapes in more capable 3D
software (such as 3ds Max® or AutoCAD®) and import these shapes into VRML.
The spring shape was drawn in 3ds Max® and exported as a VRML file (the spring
shape can be downloaded from Springer’s web site [Link] with
the material of Chap. 4; the name of the file is spring_shape.wrl). The spring’s
length is 5. Before constructing the virtual scene, it is worth mentioning that all the
objects (included in the library of VRML) will have their center positioned by
default at the origin (0,0,0). To create the virtual scene for the mass-spring-damper
system, follow the following steps:
1. Open the downloaded file, spring_shape.wrl, using VRML.
2. To create the wall where the spring is attached, click on Insert Box (Fig. 4.2).
3. Click once on the title Transform to select it. Click another time on Transform
and change the text to a meaningful name such as Wall (Fig. 4.3).
4. To change the size of the wall, double-click on the Box layer (Fig. 4.4). Then
double-click the size layer. Tick the box of the x-axis, and then change the value
to 0.1. Similarly, change the y- and z-axes values to 4. Click OK when done.
5. Click on the New World layer to select the top layer under which all the compo-
nents should be positioned (Fig. 4.5).
6. Similar to step 2, click on Insert Box to create the mass.
7. Similar to step 3, change Transform to a meaningful name such as Mass.
8. Similar to step 2, click on Insert Box to create the floor and rename the Transform
a meaningful name such as Floor.
4.3 Creating the Virtual Scene for the Mass-Spring-Damper 23
Fig. 4.1 Initial length of the spring in addition to the width of the mass
Fig. 4.2 Insert Box button is shown inside the rectangle
9. Repeat step 4 to resize the floor to 14, 0.1, and 10 in the x-, y- and z-directions,
respectively. Click OK when done. The virtual scene should look like Fig. 4.6.
Figure 4.6 does not look like the final virtual scene. Elements such as the Mass,
Floor, and Wall are centered by default at the point (0,0,0). The top surface of
Fig. 4.3 Change the name of the cube from Transform to Wall
Fig. 4.4 Box is shown inside the rectangle
Fig. 4.5 New World layer
Fig. 4.6 Mass, Spring, Wall, and Floor
26 4 Mass-Spring-Damper Oscillations
Fig. 4.7 The translation property is shown inside the rectangle
Floor should be touching the bottom surface of Mass. Thus, Floor should be
translated in the y-direction (vertical direction) by half the height of the mass (the
height of the mass is 2) plus half the thickness of the floor (which is 0.1). The overall
translation is 1.05 downward. Double-click on translation (Fig. 4.7). Tick the box
of the y-axis, and then change the value to −1.05. Click OK when done (Fig. 4.8).
Mass should be translated to the other end of the spring, but since its position will
be changed based on the solution of the differential Eq. 4.1, its translation will be
handled by the M-script.
Click on File > Save as, and save it in the same directory where you will have
your M-script. Name the file virtual_scene. The extension of the virtual reality file
will be .wrl.
4.4 Adding Material and Changing the Color Properties
of the Virtual Objects
To add a blue color to Mass, click on the Color Mode button (Fig. 4.9). The Color
Mode Painter will show up. Double-click on the Diffusive Color area and then
click to choose the desired color from the Edit Diffusive Color area (Fig. 4.10).
Click OK when done. Notice that when you pass the cursor over the virtual scene,
Fig. 4.8 Virtual scene with the translated Floor
Fig. 4.9 Color Mode button is shown inside the rectangle
28 4 Mass-Spring-Damper Oscillations
Fig. 4.10 Diffusive Color and Edit Diffusive Color areas in the rectangle and circle, respectively
it will change to a paint brush. Click on Mass in the virtual scene to color it. Close
the Color Mode Painter.
To add a texture to Floor and Wall, click on the Texture Library button
(Fig. 4.11). Choose the Brick (Small) texture (Fig. 4.12). Apply the texture on
Floor by dragging the displayed texture inside the square to anywhere over Floor
object in the virtual scene (click and hold the left mouse button to drag the texture,
and then unclick it over the object to apply the texture). Similarly, apply the same
texture on the wall. Click Close and save your file when done.
4.5 M-script File for Solving the Differential Equation
To numerically integrate for the solution of the differential equation given in Eq. 4.1,
use the ode45 function in MATLAB®. For more information about this function,
type the following in the MATLAB® command window: help ode45.
The following M-script file, MSD_solver.m, will numerically integrate the dif-
ferential equation given in Eq. 4.1. Some comments have been added to briefly
explain the M-script file. The M-script can be found in Chap. 4 folder of the extra
material of the book. The folder can be downloaded by following [Link]
[Link]/ and then searching for the book by entering the ISBN or the title.
Fig. 4.11 Texture Library button is shown inside the rectangle
30 4 Mass-Spring-Damper Oscillations
Fig. 4.12 Brick (Small) texture
The above M-script file, MSD_solver.m, calls myfunc.m which computes the
derivative vector [x(t ) x (t )]. The following is the script for myfunc.m:
4.6 M-script File for Animating the Virtual Scene 31
4.6 M-script File for Animating the Virtual Scene
In what follows, the virtual scene, virtual_scene.wrl, will be animated based on the
solution given by MSD_solver.m, and the animation will be recorded into a movie
file, [Link] (the reader should run MSD_solver.m before running MSD_
animate.m). The parameters that will be changed in the virtual world are the trans-
lation property of the mass and the length of the spring. The default position of the
center of the mass point is at (0,0,0). The instantaneous x-position of the center of
the mass is equal to the original (unextended) length of the spring plus half the
length of the mass plus the oscillation of the mass around its equilibrium position
given by Eq. 4.1. Thus, the instantaneous x-position of the mass is equal to
5 + 2 / 2 + x(t ). Note that the wall is positioned at point (0,0,0). As for the length
of the spring, it will be changed by scaling it in the x-direction according to the
instantaneous position of the mass.
A first look at MSD_animate.m script might seem overwhelming and full of
commands; therefore, the reader is advised to start writing his/her own code by
modifying an existing one instead of starting from scratch every time.
The script that comes before the for-loop consists of three parts. The first (refer
to Part 1 of MSD_animate.m) one deals with opening and viewing the virtual world.
The second (refer to Part 2 of MSD_animate.m) deals with setting the recording of
the animation to a *.avi file. In this example, the movie will be saved as [Link].
As for the third part (refer to Part 3 of MSD_animate.m), it extracts the original
scale of the spring in x-, y-, and z-directions and stores the values in scale_x, scale_y,
and scale_z variables. Originally, the spring has the x-, y-, and z-scales equal to 1.
As for the for-loop, the first parameter that will be changed in the virtual world
is the translation property of the mass. Six is equal to the original length of the
spring plus half the length of the mass in the x-direction (Fig. 4.1). x(Index,1) is the
elongation of the spring x(t ) given by Eq. 4.1:
[Link]=[6+x(Index,1) 0 0];
To vary the length of the spring, its scale in the x-direction should be changed
according to the instantaneous position of the mass. The scale in the x-direction is
given by
scale _ x = (5 + x(t )) / 5 (4.2)
Equation 4.2 is implemented in the for-loop by writing:
[Link] = [(5 + x(Index,1))/5 scale_y scale_z];
After the reader runs MSD_animate.m, a video file [Link] should be created
in the same working directory. The reader should try playing the movie to see a
playback of the animation. To increase or decrease the playtime of the movie, the
reader can change the number of samples used for the numerical integration of the
differential Eq. 4.1.
32 4 Mass-Spring-Damper Oscillations
4.7 Application Problem 33
Fig. 4.13 Dimensions of the two masses and their initial positions
Fig. 4.14 Unextended position of the spring with the two masses
4.7 Application Problem
Given two masses mB1 = 1 (kg) and mB 2 = 1 (kg) of the same dimensions
1 ´ 2 ´ 2 (m 3 ) (Fig. 4.13). The two masses are connected by a spring of stiffness
K = 1 (N/m) and a damping constant (friction) C = 0.15 (Ns/m). The centers
of mB1 and mB 2 are initially situated at x B1 (0) = -1.7 (m) and x B 2 (0) = 1.7 (m),
respectively (Fig. 4.13). Both masses have zero initial velocities. The initial unex-
tended length of the spring is 5 (m) (Fig. 4.14). Assuming no friction with the
ground, solve the following:
1. Reconstruct the virtual scene shown in Fig. 4.13 in VRML assuming that the
bottom faces of the masses are located at y = 0 and that the two boxes are aligned
horizontally. Add an additional box component to represent the ground where
the top face of this box is at y = 0. Represent the spring by a cylinder object or
initial radius r0 = 0.4 (m).
34 4 Mass-Spring-Damper Oscillations
2. Develop the equations of motion of the two masses (Hibbler 1986).
3. Develop the M-script file to compute the numerical solution of the equations of
motion, animate the VRML model, and record the simulation into a video. The
objects that should be animated in the scene are the two masses and the spring. The
spring should be scaled in the x-direction such that its initial volume does not
change. Assume the total time of simulation is 50 s and that the step size is 0.01 s.
The solution of the problem can be downloaded from Springer’s web site http://
[Link]/. The files can be found in folder /Chapter 4/Application
Problem. Run the script Two_masses_solver.m to generate the solution.
Reference
Book
Hibbler RC (1986) Engineering mechanics: dynamics, 4th edn. McMillan, New York