Spring Mass system in TikZ: Short Drawing Guide

  • This short guide is about drawing a simple spring mass system in LaTeX using TikZ package. At the end of this tutorial, you will be able to fill shapes with patterns, create and customize a coil shape and position nodes using anchors

Packages and Libraries

The spring mass system that we would like to create in TikZ is shown below. Mainly, it has three parts: 1) the support, 2) the spring and 3) the mass.

Spring Mass system in TikZ
  • What packages and/or libraries are needed to draw a simple spring-mass system?

We need to load TikZ as it's the main drawing package. We need patterns which is a TikZ library for the support. In addition, we need to load decorations.pathmorphing which is also a TikZ library for drawing the spring!

Step 1: draw the support

The support corresponds to a rectangle shape filled with north west lines pattern.

Details: Let's consider a rectangle of 3cm width and 0.2cm height. The rectangle has no border and filled with north west lines pattern. We will add a thick line at the bottom side of the rectangle to get the following illustration: 

Supporting structure

and here is the corresponding LaTeX code:


\documentclass[border=0.2cm]{standalone}

% Required package and libraries
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}

\begin{document}
	
	
\begin{tikzpicture}[black!75,thick]

% Supporting structure
\fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2);
\draw[thick] (-1.5,0) -- ++(3,0);

\end{tikzpicture}

\end{document}


Step 2: draw the spring

As mentioned above, the spring can be drawn using decorations.pathmorphing library. Check the following line code which highlights different coil shape options:


\draw
[
	decoration={
		coil,
		segment length = 1mm,
		amplitude = 2mm,
		aspect = 0.5,
		post length = 3mm,
		pre length = 3mm},
	decorate] (0,0) -- ++(0,-2.5)

Coil options

Amplitude coil TikZ
Segment length coil TikZ
Aspect coil TikZ
Pre Post lines coil TikZ

Details: In our case, we will draw the coil from (0,0) to (0,-3) with the following parameters: 

- aspect=0.3,
- segment length=1.2mm,
- amplitude=2mm,
- pre length=3mm,
- post length=3mm

Here is the corresponding code:


\documentclass[border=0.2cm]{standalone}

% Required package and libraries
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}

\begin{document}
	
	
\begin{tikzpicture}[black!75,thick]

% Supporting structure
\fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2);
\draw[thick] (-1.5,0) -- ++(3,0);

% Spring 
\draw
[
	decoration={
		coil,
		aspect=0.3, 
		segment length=1.2mm, 
		amplitude=2mm, 
		pre length=3mm,
		post length=3mm},
	decorate
] (0,0) -- ++(0,-3) 
	node[midway,right=0.25cm,black]{$k$}; 

\end{tikzpicture}

\end{document}


Compiling this code yields: 

Draw a spring in TikZ

We have added a text node to the right of the middle point of the spring.

Step 3: draw the mass

The mass corresponds to a rectangle filled with yellow color and has a label m. We will draw it using rectangle node shape (more details). Here is the corresponding LaTeX code:


\documentclass[border=0.2cm]{standalone}

% Required package and libraries
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}

\begin{document}
	
	
\begin{tikzpicture}[black!75,thick]

% Supporting structure
\fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2);
\draw[thick] (-1.5,0) -- ++(3,0);

% Spring 
\draw
[
	decoration={
		coil,
		aspect=0.3, 
		segment length=1.2mm, 
		amplitude=2mm, 
		pre length=3mm,
		post length=3mm},
	decorate
] (0,0) -- ++(0,-3) 
	node[midway,right=0.25cm,black]{$k$}; 

% Mass
\node[draw,
	fill=yellow!60,
	minimum width=1cm,
	minimum height=0.75cm,
	anchor=north,
	label=east:$m$] at (0,-3) {};

\end{tikzpicture}

\end{document}


Compiling this code yields:

Simple mass spring in TikZ

Comments:

- The node by default has a rectangle shape that can be highlighted by adding draw option (draws the node shape border). 

-  fill=yellow!60: adds a light yellow color to the rectangle shape

- minimum width and minimum height sets the minimum size of the rectangle.

-anchor=north: by default the node center will be positioned on the provided coordinates (0,-3). Adding this option will position the top of the rectangle on the coordinates (0,-3). More details can be found in the tutorial: draw a rectangle in TikZ.

- label=east:$m$ : adds the text label $m$ on the right of the node shape. 

Spring Mass system LaTeX code

Here is the final LaTeX code of a simple spring mass system in TikZ. We have added arrows to the previous code and for more details you can check the post: TikZ arrows.

\documentclass[border=0.2cm]{standalone}

% Required package and libraries
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}

\begin{document}
	
	
\begin{tikzpicture}[black!75,thick]

% Supporting structure
\fill [pattern = north west lines] (-1.5,0) rectangle ++(3,.2);
\draw[thick] (-1.5,0) -- ++(3,0);

% Spring 
\draw
[
	decoration={
		coil,
		aspect=0.3, 
		segment length=1.2mm, 
		amplitude=2mm, 
		pre length=3mm,
		post length=3mm},
	decorate
] (0,0) -- ++(0,-3) 
	node[midway,right=0.25cm,black]{$k$}; 

% Mass
\node[draw,
	fill=yellow!60,
	minimum width=1cm,
	minimum height=0.75cm,
	anchor=north,
	label=east:$m$] at (0,-3) {};

% x1 arrow
\draw[very thick,
	red,
	|-latex] (-2,0) -- ++(0,-1)
	node[midway,left]{\small $x_1$};

% x2 arrow
\draw [very thick,
	blue,
	-latex
] (-0.8,-3.4) -- ++(-1,0) ++(0.25,0) -- ++ (0,-1)
	node[midway,left]{\small $x_2$};

\end{tikzpicture}

\end{document}
Spring Mass system in TikZ

Extension of the spring Mass system

  • At this level, you know how to draw a simple spring mass system. Copy and paste the above code inside a scope environment and and shift it to the right. Modify the shifted spring mass system as follows: 1) draw a longer coil, 2) increase the value of segment length of the coil, 3) change the size of the mass. That's it!
Extension of a coil spring Mass system in TikZ LaTeX
  • We've reached the end of this tutorialIf you have any questions or remarks, leave me a comment below or reach me via e-mail at admin@latexdraw.com, I will be happy to hear from you!