Jupyter, SymPy, and floating blocks

I had a few New Year’s resolutions related to Python:

2016:

1. conda instead of pip
2. Python 3 instead of 2
3. Jupyter notebooks instead of IPython console

Decreasing order of likelihood.

Dr. Drang (@drdrang) Dec 31 2015 4:22 PM

I’ve kept Resolution #1. I have Anaconda running on both of my Macs, and I’m using conda to install new libraries and keep the old ones up to date.

I’m behind on Resolution #2, although I do have a Python 3 environment installed on my iMac. I just haven’t gotten around to using it for anything other than a bit of testing here and there. Still, I have half the year left.

As for Resolution #3, my adoption of Jupyter notebooks has been slow, but I have been using them more frequently—mostly as a kind of scratchpad for my own reference than as something to distribute.

Missing from my list in December was a fourth resolution: to learn SymPy and use it when I need to do symbolic instead of numerical analysis. I know SymPy isn’t as powerful as dedicated symbolic math programs, but my needs for symbolic math are fairly modest. I just want to do elementary algebra and calculus without making sign errors and copying mistakes.

To get comfortable with any software, you need to use it frequently. Projects at work that are natural for Jupyter and SymPy have been sparse, so I’ve been casting about for interesting side analyses to develop my skills. A few days ago, Nathan Grigg tweeted a link to this page by Nick Berry. It’s an analysis of how a long block of wood with a square cross-section floats.

wood

Nick’s analysis is largely numerical, and he has a fun simulation applet embedded in his page that lets you set the density of the block, drop it in the water, and watch it bob around until it comes to a stable equilibrium position. It’s a cute problem with some non-intuitive results, and I wanted to reproduce Nick’s results but to do it analytically instead of numerically. I started writing equations on paper and soon realized both that it was going to take several pages and that I was unlikely to get through it without making some careless algebraic bookkeeping error. A good opportunity to use SymPy in a Jupyter notebook.

Jupyter notebook for floating block

I’m not going to reproduce the work here, but I have created a static HTML page where you can see the results. If you’re a Jupyter user yourself, you can download a zip file with my original notebook (.ipynb) file and the graphics that go with it.

One thing I should mention: After I’d done most of the analysis, I found this set of hydrostatics course notes by Richard Fitzpatrick of the University of Texas. He solves the same problem—albeit using a different coordinate system than I do—and also provides a much richer background on the general problem of floating bodies. I stole a variable substitution trick from him that simplified my analysis considerably.