Examples of Stoke’s Theorem and the Divergence Theorem Using Python

Rhett Allain
12 min readMar 1, 2022
Image: Rhett Allain. Stoke’s and Divergence Theorems

You might not realize that they are important in physics — but you pretty much need both Stoke’s Theorem and the Divergence Theorem for vector stuff (like Maxwell’s Equations). However, they can be a little difficult to comprehend.

I’m not going to derive these two equations, instead I am just going to show you that both sides are equivalent. But wait! Instead of showing the equations with surface and volume integrals, I’m going to use Python and random numbers. Trust me, this will be fun.

Stoke’s Theorem

I’m going to start with Stoke’s Theorem. I think it’s a little easier to use since you only need a path integral and a surface integral. Here’s what it looks like.

In short, Stoke’s Theorem (I’m just going to call it “Stokes” now because we are close friends and give each other nicknames) gives a relationship between a path integral and a surface integral for a vector field (I’m using the vector field F). There’s so much here, so let’s just get to it.

What is a vector field? In physics, we have LOTS of vector fields. Force, electric field, magnetic field, gravitational field are the most common ones. But basically, at every point in space (x,y,z) there is a vector value F(x,y,z). That’s it. Oh, for this example we are…



Rhett Allain

Physics faculty, science blogger of all things geek. Technical Consultant for CBS MacGyver and MythBusters. WIRED blogger.