Written by Dave Martin
  • 7/23/2018

How Math Can Help You Outsmart a Hungry Shark

The oh-so-popular Shark Week starts on July 22nd on the Discovery Channel. In honor of that event, let’s take a look at how we can apply math to the behavior of sharks.

It turns out that the motion of sharks in water can be described by two different models from math and physics, both of which are connected to random numbers: Brownian motion and Levy flights.

When food is abundant in their environments, sharks follow Brownian motion when searching for prey. When particles that don’t dissolve in a fluid are impacted by particles in that fluid, their motion is chaotic and random. Examples include the way that food coloring disperses in water or the way a car’s exhaust expands through the air. When sharks move in this way, they make a lot of quick turns and changes in direction.

When food is scarce, sharks abandon Brownian motion for Levy flights. This type of motion is characterized by mostly short, random turns and movements – like Brownian motion – interspersed with occasional long legs of motion along a straight line.

Levy flights are considered to be the optimal method for searching in an environment, say if you were looking for a lost hiker in the woods or an escaped prisoner.

Simulate Brownian Motion with PTC Mathcad

Let’s simulate this motion in three dimensions. We’ll take advantage of three main capabilities: (1) built-in functions including those for random numbers; (2) programming; and (3) graphing capability.

First, take a look at a program that simulates Brownian motion:

 Programming a Brownian equation in PTC Mathcad

This program generates random numbers for distance f, azimuthal angle θ, and polar angle φ. Using trigonometry, we convert these values into distances in the x, y, and z directions, and add those values to the previous location. These new coordinates are added as entries in a matrix.

Here’s an example of a plot in two dimensions, of the x- and y- coordinates:


But the ocean has depth as well. In PTC Mathcad, we can generate 3D plots of the matrix:


The results change every time we hit the Calculate button, since the random numbers are regenerated. Here is an example of another random solution:

I captured a bunch of images and strung them together into a GIF:

Calculating Levy Walk with PTC Mathcad

Now let’s examine how the shark’s motion changes when disaster strikes and food is much harder to find. The shark searches with a bunch of random turns and motions, but to maximize the search volume and chance of coming across prey, it randomly travels long legs to wind up in a completely different location.

So how do we get those additional random long legs? Based on some help I found on the Mathematics Stack Exchange , we can accomplish this by using an inverse cumulative distribution function.


Here is an example of a random solution for the shark’s Levy walk:


Just as before, by hitting the Calculate button, we can generate a number of solutions to see how different the motion can be under the Levy walk compared to Brownian motion:

What do we learn from a shark’s natural motion to help us survive? Since the shark makes a lot of short twists and turns, your best bet is to travel in a straight line, as fast as you can, out of the shark’s habitat. Better yet, follow this advice to maximize your chances of avoiding a shark attack altogether.

Enjoy Shark Week!


Got a hungry predator on your tail? Save yourself. Download PTC Mathcad Express for free. 

download mathcad express free


About the Author

Dave Martin is a Creo, Windchill, and PTC Mathcad instructor and consultant. He is the author of the books “Top Down Design in Creo Parametric,” “Design Intent in Creo Parametric,” and “Configuring Creo Parametric,” all available at amazon.com. He can be reached at dmartin@creowindchill.com.

Dave currently works as the configuration manager for Elroy Air, which develops autonomous aerial vehicles for middle-mile delivery. Previous employers include Blue Origin, Amazon Prime Air, Amazon Lab126, and PTC. He holds a degree in Mechanical Engineering from MIT and is a former armor officer in the United States Army Reserves.