Vintage mechanics

Last week I read Vintage Murder by Ngaio Marsh and was surprised to find that the setup for the murder was very much like the homework problems I used to assign in my sophomore-level mechanics class. Solving the problem—which didn’t help in solving the murder—was a fun little exercise.

Marsh’s hero, Detective Chief Inspector Roderick Alleyn, is on holiday in New Zealand. An English theatrical company happens to be travelling the same route as Alleyn, and they all stop in some small town on the North Island. Because he’s gotten to know the members of the company, Alleyn is invited to a birthday party for the company’s leading lady, Carolyn Dacres.

As a surprise, Dacre’s husband and the manager/owner of the company, Alfred Meyer, has brought along a jeroboam of champagne and has worked in secret with the stagehands to have it magically revealed at the party, which takes place on stage after a show.

Here’s Marsh’s description of the setup:

The giant bottle was suspended in the flies with a counterweight across a pulley. A crimson cord from the counterweight came down to the stage and was anchored to the table. At the climax of her party, Carolyn was to cut this cord. The counterweight would then rise and the jeroboam slowly descend into a nest of maiden-hair fern and exotic flowers, that was to be held, by Mr. Meyer himself, in the centre of the table.

And here’s my sketch of it. I’ve drawn the cord connecting the bottle to the counterweight in blue to help distinguish it from the cord that’s supposed to get cut.

Bottle setup

Alfred and the stagehands practiced the cord-cutting several times to make sure the bottle would come down at the right speed and in the right place. But at some point before the party, the murderer removed the counterweight, connected the red and blue cords, and shifted the pulley a couple of feet sideways. When Carolyn cut the red cord, the jeroboam fell rapidly and smashed into poor Alfred’s head, killing him instantly and ruining the party.

The dynamics of the altered setup aren’t very interesting, but the dynamics of the original setup are. As a homework problem, this could be assigned in two ways:

  1. Given the masses of the jeroboam and the counterweight, determine the downward acceleration of the jeroboam when the red cord is cut.
  2. Given the masses of the jeroboam and the counterweight and the height of the jeroboam above the table, determine the downward velocity of the jeroboam just before it reaches the table.

Let’s solve both. We’ll make the usual sophomore-level assumptions that the following values are negligible and can be ignored in the analysis:

The acceleration problem is best solved using Newton’s Second Law. We’ll start with the free-body diagrams of the bottle (I’m tired of typing jeroboam) and the counterweight.

Free-body diagrams

\(g\) is the acceleration due to gravity. \(m\) is the mass of the bottle. \(\mu\) is the mass ratio of the counterweight to the bottle; it’s a dimensionless parameter that’s less than one. \(T\) is the tension in the blue cord, which is equal on both sides of the pulley because of the assumptions above.

Applying Newton’s Second Law to the bottle FBD, and taking the positive direction to be downward, we get

\[mg - T = ma \quad \Longrightarrow \quad T = m \left( g - a \right)\]

where \(a\) is the downward acceleration of the bottle.

Note that the blue cord provides a kinematic constraint that makes the downward movement—displacement, velocity, and acceleration—of the bottle equal to the upward movement of the counterweight. Using this, Newton’s Second Law applied to the counterweight, with the positive direction upward, is

\[T - \mu m g = \mu ma \quad \Longrightarrow \quad T = \mu m \left( a - g \right)\]

Therefore

\[m \left( g - a \right) = \mu m \left( a - g \right)\]

and

\[a = g \frac{1 - \mu}{1 + \mu}\]

This is an nice, neat little solution, and it makes sense at the two extremes:

You might feel uncomfortable about my flipping the coordinate system around—down is positive for the bottle, up is positive for the counterweight. Don’t. We are in charge of the coordinate systems we use, not the other way around. We just have to stay consistent within the equations for each free-body diagram, which we did.

Now let’s move on to the velocity problem. We could take the acceleration result we just got and do some kinematics, but it’s more fun to use energy principles. Also, when the homework problem has things moving up or down and asks for velocity, it’s a good bet that your professor wants you to solve the problem using energy.

Before the red cord is cut, the bottle and counterweight are at rest and the kinetic energy of the system is zero. When the bottle reaches the level of the table, when both it and the counterweight are traveling at a speed of \(v\), the kinetic energy is

\[\frac{1}{2} m v^2 + \frac{1}{2} \mu m v^2 = \frac{1}{2} m v^2 \left( 1 + \mu \right)\]

Because we start at zero, this is also change in kinetic energy.

There are no springs in the system, so the potential energy is all due to gravity. The bottle loses potential energy as it goes down, and the counterweight gains potential energy as it goes up. The overall change in potential energy of the system is

\[-mgh + \mu mgh = - \left( 1 - \mu \right) m g h\]

I’ve written this with a leading negative sign to emphasize that we have a loss of potential energy because the bottle weighs more than the counterweight.

The gain in kinetic energy must equal the loss in potential energy, so

\[\frac{1}{2} m v^2 \left( 1 + \mu \right) = \left( 1 - \mu \right) m g h\]

and, after a little algebra,

\[v = \sqrt{2gh} \sqrt{\frac{1 - \mu}{1 + \mu}}\]

This solution also makes sense at the extremes. You may not remember that an object dropped from a height of \(h\) hits the ground at a speed of \(\sqrt{2gh}\), but that is the well-known result.

This is fine as far as the homework-style problems are concerned, but Alfred and the stagehands had to solve the inverse problem: What counterweight is necessary to get the bottle to reach the table at a gentle speed? For that, we just rearrange the above solution to solve for \(\mu\):

\[\mu = \frac{2gh - v^2}{2gh + v^2}\]

Let’s apply some numbers to this and see what we get. I would put the original height of the bottle at 4 m above the table, and I doubt Alfred would want the bottle moving faster than 1 m/s when it reaches the table. With those values, and the usual gravitational acceleration, we get

\[\mu = \frac{2\cdot 9.81 \cdot 4 - 1^2}{2\cdot 9.81 \cdot 4 + 1^2} = 0.9748\]

So the counterweight had better be pretty damned close to the weight of the bottle, which is in the 10–11 pound range. You would think the stagehands would have to cobble together a few weights to hit this target, but the book says it was just a single counterweight—an item that was up in the flies and would normally be used to help lift scenery. How the company happened to have a counterweight of exactly this size is a mystery even DCI Alleyn couldn’t solve.


Mail links and percentages

Yesterday, John Voorhees wrote a nice article at MacStories about creating links to specific email messages. His system is in the form of a Shortcut, but the real work is done by an AppleScript. The AppleScript is an extension of one John Gruber wrote 15 years ago.1

The script works by extracting the message ID of the currently selected mail message and assembling it into a URL of this form:

message://<message ID>

The message ID itself will look something like this:

FE3340B0-0AB4-082CBE10887A@company.com

So if you embed a Markdown link of the form

[Message subject](message://<FE3340B0-0AB4-082CBE10887A@company.com>)

into any number of note-taking apps, you’ll get a nice link which will, when clicked on, open that message in Mail. John’s Shortcut has some other niceties, too, but I want to focus on the messages:// URL.

The article prompted a very good question from Michael Tsai:

Haven’t found any messages where the Message-ID needed to be percent-escaped?

John’s answer was no, and Gruber chimed in that he hadn’t run into a problem in the 15 years he’s been using his script. I used to use Gruber’s script (or some variant of it) to link to emails in my work notes, and I hadn’t needed to percent-encode any of my message IDs, either. But was that a sufficient answer? I wanted to find out.

If you’ve been wrangling URLs for any length of time, you’ve run into percent-encoding. Certain characters (reserved characters) have a special meaning in a URL, and to use such a character without invoking its special meaning, you have to convert it to a percent symbol (%) followed by its hex character code. For example, a question mark is changed to %3F, an ampersand is changed to %26, and a percent symbol is changed to %25.

Of course, these are the rules you use for http URLs. Are the same rules needed for message URLs? Given the experiences of the two Johns and me, it seemed unlikely, but I wanted to test it. The questions I needed to answer were:

  1. Are reserved characters used in message IDs?
  2. If so, do they need to be percent-encoded in a message URL?

I started answering the first question by collecting the message IDs from a bunch of emails using this AppleScript:

applescript:
 1:  set msgIDs to {}
 2:  
 3:  tell application "Mail"
 4:    tell account "Fastmail"
 5:      set mboxes to every mailbox
 6:      repeat with m in mboxes
 7:        if (count of messages in m) is greater than 0 then
 8:          set end of msgIDs to message id of every message of m
 9:        end if
10:      end repeat
11:    end tell
12:  end tell
13:  
14:  set text item delimiters to linefeed
15:  get msgIDs as text

This returned a big chunk of text—about 1.4 MB, composed of over 24,000 message IDs, one per line. To figure out whether any reserved characters were used in this corpus, I stole some ideas from Doug McIlroy’s famous word-counting pipeline.

With the output of the above AppleScript on my clipboard, I ran this pipeline:

pbpaste | fold -w 1 | sort | uniq -c

The fold command is the key. It turned the corpus into a new text string with one character per line. The fake message ID up near the top of the post would become

F
E
3
3
4
0
B
0
-
0
A
B
4
-
0
8
2
C
B
E
1
0
8
8
7
A
@
c
o
m
p
a
n
y
.
c
o
m

All 24,000 message IDs in my corpus were converted likewise, creating about 1.4 million one-character lines. These lines were then sorted and compressed by the uniq command into 78 lines, one for each unique character. Because of the -c option to uniq, the characters were preceded by their counts. The results, after some reformatting to make them easier to read, were

   12 !      24201 @       1627 T       4680 k
    2 #      33657 A       1343 U      18814 l
 1333 $      38424 B       1100 V      35900 m
    2 %      31145 C       1398 W      27670 n
    6 &      28671 D       3494 X      39174 o
  722 +      29486 E       1726 Y      14475 p
56536 -      26060 F       1110 Z       1778 q
55152 .       1454 G          3 [      14647 r
  110 /       4295 H          3 ]      13117 s
73638 0       1043 I       2013 _      11391 t
68062 1       2676 J      41330 a      14885 u
58154 2       1392 K      15572 b       4940 v
50054 3       2011 L      39224 c       6854 w
65081 4      10691 M      29364 d       2334 x
51243 5       4299 N      43692 e       3051 y
49928 6       1592 O      23604 f       2145 z
49139 7       6646 P      17034 g          3 |
51868 8       1260 Q       4166 h         16 ~
52855 9       7500 R      32222 i      
  773 =       3826 S       1458 j      

As you can see, lots of reserved characters are included in the message IDs. Which led to the second question: Do they need to be percent-encoded?

I pasted the corpus into BBEdit, and searched for each reserved character in turn. I copied the line with the character in question and tried it out on the command line using open. Here’s an example with a slash:

open 'message://<drdrang/notes/pull/1@github.com>'

The URL is in single quotes to prevent the shell from interpreting any of its special characters. Running this command brought up an old message from GitHub.

None of the reserved characters caused a problem except the percent sign itself. A command like

open 'message://<D22041DC.25F53%person@company.com>'

would fail unless I converted it to

open 'message://<D22041DC.25F53%25person@company.com>'

(I should mention that when I first discovered this, I thought the URL wouldn’t work even after percent-encoding the percent symbol. I thought that because I either mistyped the %25 as %24 or read the ASCII code wrong from the table. However I made the mistake, thanks to Leon Cowle for the tweet that made me realize what I’d done wrong.)

My conclusion is that you probably should be on the lookout for percent signs in your message IDs and change them to %25 if they appear. They’re pretty rare—less than one in a thousand of my emails—but they could screw up your links.

If you have FastScripts (even the free edition), you can do the replacement in AppleScript very easily with this snippet of code:

applescript:
1:  tell application "FastScripts"
2:    set msgID to replace text msgID matching pattern "%" replacement pattern "%25"
3:  end tell

where I’ve assumed the message ID is in the variable msgID.

I suppose there could be other characters—characters that don’t appear in my corpus—that need percent-encoding, but I don’t think so. There’s a logic to the percent sign being the only character that needs encoding. All the other reserved characters can be encoded. For example, that GitHub message could have been opened with

open 'message://%3Cdrdrang%2Fnotes%2Fpull%2F1@github.com%3E'

where I encoded all the slashes and the angle brackets. Because of this, percent signs cannot be interpreted literally. But they are the only character for which this is true.


  1. Ironically, most of the extensions have to do with turning the URL into a Markdown link. 


Octagons and squares

A recent episode of the 99% Invisible podcast, “Octagon House,” had a little math problem embedded in it, and while I was solving that problem I learned something new about Mathematica.

(By the way, I bought the personal edition of Mathematica earlier this year when it was on sale for about half off. I’m on the cusp of retirement, so I thought I’d give myself a gift that would keep me interested in mathematics when work is no longer providing me math problems. You’ll probably be seeing more Mathematica-related stuff here as time goes on.)

The episode is about the short-lived mid-19th century trend of building houses that are octagonal in plan. The main proponent of such houses was Orson Squire Fowler, who wrote the following in his book, The Octagon House: A Home For All.

Now, since a given length of octagon wall will enclose one-fifth more space than the same length of wall in a square shape, of course, you can have the same size wall for one-fifth less money.

Ignore everything after the “of course.” My interest was in the premise:

Given an octagon and a square of the same perimeter, the ratio of their areas is 6:5.

Could that really be true? I doubted it. Seemed way to simple, and I couldn’t believe it didn’t involve irrational numbers.

I was listening to the podcast while out on a walk,1 so I paused the playback and tried to work out the area ratio in my head. I was in some respects successful, in that I got an answer that I was sure didn’t equal 6/5, but I didn’t know what my answer was numerically. I had an expression that included the tangent of 22.5°, which unfortunately isn’t one of the trig functions I have memorized.2

Here’s how I imagined an octagon and a square with the same perimeter:

Octagon and square

To get the area of the octagon, we figure out the area of one of the interior triangles and multiply by eight. The area of each interior triangle is

\[A_{tri} = b \frac{a}{2}\]

where the base of the triangle is

\[b = \frac{a}{2 \tan{22.5°}}\]

So

\[A_{oct} = 8 A_{tri} = \frac{2 a^2}{\tan{22.5°}}\]

and

\[\frac{A_{oct}}{A_{sq}} = \frac{\frac{2 a^2}{\tan{22.5°}}}{4a^2} = \frac{1}{2 \tan{22.5°}}\]

This is where I got stuck. When I got home, Mathematica told me that the area ratio is

\[\frac{1}{2}\;\mathrm{Cot}\left[ \frac{\pi}{8} \right]\]

which, I was happy to see, matched my result.3 And I knew that I could convert this to a number by enclosing it in N[] or by clicking the “numerical value” button under the last output.

Mathematica output

But before getting numbers, I wanted to see what the result was in terms of irrational values. The “convert to radicals” button seemed like the way to go for that. Clicking it gave me

Mathematica further output

It was nice to learn about ToRadicals, but an answer of

\[\frac{1}{2}\; \sqrt{\frac{2 + \sqrt{2}}{2 - \sqrt{2}}}\]

seemed much more complicated than necessary. I clicked the “root reduce” button in hopes of simplifying the expression. It worked.

Mathematica simplified irrational

So I was right that the area ratio wasn’t simply 6/5, but it was also clear—even before I tapped the “numerical value” button—that

\[\frac{1}{2}\; \left( 1 + \sqrt{2} \right)\]

is pretty close to 6/5. It’s 1.2071 (to five digits).

So Fowler was correct, for all practical purposes, and I got to learn Mathematica’s ToRadicals and RootReduce functions.

And in case you’re ever out walking and need to know the tangent of 22.5°, it’s

\[\sqrt{2} - 1 \approx 0.4142\]

  1. The only times I listen to podcasts are while driving or walking, neither of which are conducive to working out math problems with either a paper and pencil or Mathematica. 

  2. And if you’re wondering why I didn’t use the half-angle formulas, it’s because I don’t have them memorized, either. 

  3. Sometime in college, I realized there’s nothing wrong with putting the simpler trig functions in the denominator and stopped using cotangent, secant, and cosecant. Mathematica apparently disagrees with my decision. 


Apple Watch estimates

In the Rumor Roundup segment of this week’s episode of Upgrade (starts about 32 minutes in), Jason and Myke talk about the temperature sensor that’s expected—according to Mark Gurman—to appear on the next generation of Apple Watches. The sensor isn’t going to take the temperature of the air; it’s going to take the temperature of you through contact with your wrist. According to Gurman,

The body-temperature feature won’t give you a specific reading—like with a forehead or wrist thermometer—but it should be able to tell if it believes you have a fever. It could then recommend talking to your doctor or using a dedicated thermometer.

Myke wasn’t happy with this. He wanted the actual temperature. Jason’s argument was that Apple wouldn’t want to give its temperature measurement because it probably wouldn’t match the temperature you’d get with an oral thermometer. Apple can avoid complaints that the Watch’s temperature values are inaccurate by never giving you a specific value, but still provide useful information by tracking your temperature over a period of time and putting up warning whenever is gets unusually high or low.

Jason’s argument is persuasive. It’s true that Apple already uses the Watch to provide estimated values for certain health data and doesn’t seem to worry about backlash. But there’s a reason for that.

Because I was out for a morning walk while listening to Upgrade, the estimated value that came to mind was VO₂ max, which you can see in the Cardio Fitness section of the Health app. VO₂ max is your maximum volume rate of oxygen intake, normalized by your body mass. It’s expressed in milliliters per minute per kilogram, and Apple gives you the value to the nearest tenth.

The proper measurement of VO₂ max involves putting you on a treadmill or stationary bike, covering your face with a mask and hose to measures your oxygen intake, and making you run or pedal at increasing speed until your oxygen intake maxes out. This is known as cardiopulmonary exercise testing, or CPET. Because the Apple Watch can’t do CPET, it estimates your VO₂ max by tracking your speed and heart rate (and possibly some other things) during an outdoor walk or run and then applying some formula to the sensor readings.

What formula? I don’t know. I’m pretty sure it isn’t one of the formulas described in the Wikipedia article on VO₂ max, because if it were, Apple wouldn’t have bothered publishing a paper on the study it ran to develop its estimate.

Basically, Apple gathered a group of about 500 people, tracked their exertion during outdoor walks and runs with an Apple Watch, and periodically measured their VO₂ max via CPET. From this, they came up with a formula that fit the sensor data to the CPET results. Apple’s paper doesn’t tell us the formula, but it does give us a graph that shows how good the fit is.

VO2 design chart

The graph should have a 1:1 ratio, but we’ll ignore that.

With the formula developed through those 500 people, Apple then tested another 200 people to make sure the formula wasn’t super-specific to the original group. The scatter plot for this validation group looks pretty much like the one for the design group.

VO2 validation chart

Presumably, Apple could do—and probably has done—a similar study to correlate properly measured body temperatures with the readings from the upcoming Watch’s temperature sensor. It could use that study to provide an actual temperature value instead of just a high/low warning.

But look at the spread of the data in those graphs. Given the limited information it’s working from, the VO₂ max estimate looks decent as a population average, but for individuals, the spread can’t be ignored. My Watch tells me I’m at about 40 ml/min/kg, but these graphs say I could be ±10 from that. That’s huge.

Which fits in with Jason’s argument. Despite the spread, Apple’s not really inviting criticism by giving a specific value for VO₂ max—it’s an obscure measurement, hard to check, and the people who are interested in it know perfectly well that the elementary data available to the Watch can’t be expected to provide a highly accurate estimate. But body temperature is both familiar to everyone and easily checked. Imagine how many “Tempgate” YouTube videos would appear with young white men in backward baseball caps waving their Watches and thermometers to the camera. “Yo, check this out! Not even close! Hit that bell!”

The thought of it makes me glad my Apple Watch doesn’t monitor blood pressure.