Sinusoidal sunlight

I started my morning walk earlier than usual today, and it was still fairly dark out. I thought about this post from last year and the Daylight line in the graph:

Chicago, IL-2024

I wondered how hard it would be to alter the code to plot the slope of the Daylight line. That would give perhaps a better sense of how rapidly we gain daylight in the spring and lose it in the fall. But I soon decided against trying that for two reasons:

  1. Numerical differentiation (i.e., finding the slope) tends to create noisy data and therefore jagged graphs. I figured this was especially likely with the US Naval Observatory data, since it gives the sunrise/sunset times rounded to the nearest minute.
  2. If the Daylight line really was a sine wave, its slope would also be a sine wave, just shifted by about three months. Not very interesting.

But the second objection got me thinking: how close to a sine wave is the Daylight line? I could try fitting a sine function to the data and see how much the error is. This would give me a chance to learn something new about Mathematica Wolfram.1

Here’s the notebook I made to check the fit:

The first three evaluation cells create a list of (day, daylight) pairs that cover a 16-year period starting on January 1, 2009 and ending on December 31, 2024. I used a multiple of four years so leap years would appear in the same proportion they normally do, at least over the period from 1901 through 2099.

The day is an integer, starting at 0, and the daylight is given in minutes. We usually talk about “daylight hours,” not “daylight minutes,” but because I was going to plot the difference between the actual length of daylight and the best fit sine curve, I thought minutes would be an easier unit to work in.

The daylight list is calculated through the Sunrise and Sunset functions, which give results to at least the nearest second. (They display a time to the nearest second but may keep fractions of a second internally.)

We’re going to fit the data to a curve of this form:

d=m+asin[2π(tf)p]

where

The phase of a sine wave is usually expressed as an angle in radians, but here I’ve rejiggered the equation to give it the same units as time.

The best fit of the data to this equation is found through the FindFit function. Because this is a fit to a nonlinear curve, I thought it best to give decent initial guesses for each parameter,

fit = FindFit[data, model, {{m, 12*60}, {a, 3*60}, {f, 80}, {p, 365}}, t]

The mean daylight value is going to be near 12 hours, so the initial guess for m is 720 minutes. Here in the Chicago area, we go from about 9 hours of daylight in the winter to about 15 hours in the summer, which puts the initial guess for a at 3 hours or 180 minutes. The period has to be about a year, so the initial guess for p is 365 days. Finally, because 12-hour days happen at the equinoxes, our initial guess for f is 80 days, which is about where March 21 lands.

As you can see, the best fit values for the parameters are

{m -> 733.182, a -> 176.722, f -> 79.5656, p -> 365.249}

so our initial guesses were pretty good. Note that the best fit period is 365¼ days, which matches up with the frequency of leap years.

Finally, we get the difference between the actual data and the best fit curve and plot it. The first plot covers the entire 16-year period and is kind of hard to read. The error of each year is basically the same, so we can get a decent idea of how the error varies by looking at just the first year, which is shown in the second plot and reproduced here:

Sine wave error

The largest error (in absolute value) is about 7 minutes at or near the winter solstice. The error varies in a sort of sinusoidal fashion with a period of about 4 months, so the peaks and valleys of the error function are roughly 2 months apart.

You can decide for yourself whether an error like this—7 minutes out of 9 hours—makes the real data “close” to a sine wave. I’d say ~1% isn’t bad for such a simple model. Mostly, though, I’m happy to have learned a little more about the Wolfram Language.


  1. Wolfram Research recently decided to change the name of Mathematica to Wolfram, thereby giving up on decades of branding and ruining Google searches. The name change isn’t complete—you still see Mathematica in several locations in the app—but the executable has the new name. 


Rebar and reinforced concrete

A recent episode of the 99% Invisible podcast, “Brilliantly Boring,” covers a topic near and dear to my heart: reinforced concrete—more specifically, the rebar that does the reinforcing. The show does an excellent job in a short period. I just want to fill in some details.

The show is structured around the Alvord Lake Bridge in Golden Gate Park. Built in 1889, it’s one of the earliest reinforced concrete structures that’s still around. It was designed by Ernest Ransome, the guy who made rebar (almost) what it is today.

Iron and steel bars were used to reinforce concrete before Ransome. Joseph Monier of France is usually credited with starting the systematic study and use of reinforced concrete back in the 1850s. But as far as I know, the reinforcing bars and wires that Monier used were smooth, round bars with no texture or bumpiness. Ransome used bars that were made of square stock that had been twisted, like this:

Twisted bar reinforcement

Image from Walcoom Corporation.

The twist helps prevent the bar from slipping relative to the concrete. This makes them act together, which is how reinforced concrete gets its strength. In the show, they say that the twist “gives it an adhesive quality to the concrete itself,” which isn’t quite right. What the twist does is provide what’s called mechanical interlock. For a twisted bar to slip, it has to both break the adhesive bond with the surrounding concrete and push it out of the way so it can slide past. This extra resistance to slip is what made twisted bars work better.

Nowadays, we almost always use what are called deformed bars, which have little ribs on the surface to provide the interlock.

Deformed bar reinforcement

Image from Walcoom Corporation.

But twisted rebar had a strong run in the early days of reinforced concrete. Many years ago, a friend and coworker of mine was directing some repair work at the Unity Temple in Oak Park, Illinois. This is a Unitarian Universalist church designed by Frank Lloyd Wright at the beginning of the 20th century. One day my friend called me into his office. “You have to see this,” he said, as he handed me a three-foot length of twisted rebar. “This is original Frank Lloyd Wright rebar. I found it in the trash. They were going to throw it away!” So I got to hold a piece of architectural and structural engineering history because of my friend’s dumpster diving.

Why do we need to use rebar? As they say in the podcast, concrete is strong in compression but relatively weak in tension. The usual rule of thumb is that its tensile strength is about one-tenth its compressive strength. This pretty much limits unreinforced concrete to structures that never undergo tensile stresses. But if we put rebar into concrete where tension will develop, that tension will be carried by the steel, and we can use concrete in almost any kind of structure.1

There is a problem, though. The highest tensile stresses tend to occur at the outer edges of structures, right along the surface. So to get the most benefit from rebar, we want to put it as close to the surface as possible. Unfortunately, concrete is porous, and water can work its way in, come in contact with the steel, and cause it to corrode. This has two effects:

  1. It reduces the amount of competent steel, weakening the structure.
  2. The iron oxide corrosion products take up more volume than the original steel. The expansion of the corroding rebar creates an outward pressure on the surrounding concrete, leading to cracking and spalling. This further exposes the rebar, leading to more corrosion in a vicious circle.

To protect the rebar from water infiltration, there are design rules about how far from the surface the rebar must be placed (this is called concrete cover). But as we said earlier, placing the rebar away from the surface makes it less effective in resisting tensile stresses. As in all types of design, there are trade-offs, and here the trade-off is between strength and longevity.

As you may know, salt water is more corrosive than fresh water because of the chloride ions from the dissolve NaCl. So reinforced concrete in coastal environments and northern climates, where salt is used on roadways, is especially susceptible to deterioration from rebar corrosion. If you look up while driving under any number of old reinforced concrete bridges in Chicago, you’ll see plenty of rusted and exposed rebar.

If you want to see Ransome’s work, and are leery of going to the Alvord Lake Bridge (the podcast says it’s in a sketchy section of the park), you can go to the Cantor Center for Visual Arts on the Stanford campus. The original building was built in 1891, just a couple of years after the bridge, and is made of reinforced concrete as designed by Ransome. The building was expanded in the early 1900s, using masonry construction, which was a bad idea. The new wings collapsed during the 1906 earthquake, leaving only Ransome’s work still standing. His part of the building also survived the Loma Prieta earthquake of 1989, the 35th anniversary of which was just a couple of days ago.

By the way, although Stanford’s page about the building says it used “a new method of cast-in-place concrete reinforced with twisted metal rods,” just like the Alvord Lake Bridge, James MacGregor’s well-regarded textbook, Reinforced Concrete: Mechanics & Design, says that while Ransome used twisted bars in other structures, he “used discarded cable car rope as beam reinforcement” in the museum. I don’t know which is correct, but the helically wound wires in cable car ropes would also provide good mechanical interlock.

It took me a while to write this post, mainly because I kept veering off on tangents about Frank Lloyd Wright’s home and office in Oak Park, strain compatibility, the weird belief among some people that Roman concrete was better than today’s, compression-only structures, rebar size nomenclature, and the tendency for concrete to use the waste products of other industries (like flyash and, perhaps, discarded cable car rope). I hope you appreciate how I managed to edit it down.


  1. Yes, I know that rebar is also used to resist compression and shear. I’m trying to keep things simple. 


Expanding

Regular readers1 know that I like to watch mathish YouTube videos from Numberphile, Stand-up Maths, and Mind Your Decisions. Unfortunately, this leads YouTube’s machine learning system to believe that I want to watch all math videos, no matter the level or quality. So it keeps pushing dull videos at me in which an overhead camera records the hands and pen of the presenter writing out line after line of elementary math. There are apparently an infinite supply of these things.

Occasionally, one of these videos has something in the thumbnail frame that catches my eye, and I click on it despite myself. Here’s a video I saw last night that pretends there’s a connection between a binomial expansion and Stephen Hawking. What it really does is solve what it says is a problem from a high school entrance exam. No Hawking at all.

The goal is to simplify this expression:

(21) 10

It could be argued that this is already in a pretty simple form—it’s certainly compact—but in this case, “simplify” means to get rid of the exponent.

If you can bear the tedium of the video, you’ll see that its solution goes through some algebra and various levels of substitution. It takes a very long time, and not just because the presenter insists on writing out every little step along the way.2 The entire approach to solving the problem is inefficient.

Because this is an exam problem, getting the solution quickly is important. To my mind, the fastest solution is to expand the binomial directly. I don’t have the binomial coefficients memorized up to the 10th degree, but Pascal’s Triangle can be built in a jiffy:

                              1                                
                           1     1                            
                        1     2     1                         
                     1     3     3     1                     
                  1     4     6     4     1                  
               1     5    10    10     5     1              
            1     6    15    20    15     6     1           
         1     7     21   35    35    21     7     1         
      1     8    28    56    70    56    28     8     1      
   1     9    36     84  126   126    84    36     9     1   
1    10    45   120   210   252   210   120    45    10     1 

You know how to put this together, right? Each new row starts and ends with a 1, and the other terms come from the sums of the terms in the previous row diagonally to the left and right, e.g.,

10=1+9 45=9+36 120=36+84 210=84+126

and so on. Because of the symmetry, you only have to calculate about half the terms.

So the expansion of (21) 10 is

2 5102 42+452 41202 32+2102 3 2522 22+2102 212023+452102+1

where I’ve used identities like

(2) 10=2 5

and

(2) 9=2 42

to simplify each term in the expansion.

Now we can do the various multiplications and collect the terms with and without the 2 terms to get

(32+720+1680+840+90+1) (160+960+1008+240+10)2

which gives us the correct answer:

336323782

While there may be some cleverness to the YouTuber’s method of substitution and resubstitution, this is certainly more straightforward and less likely to generate algebra mistakes—both of which are important when you’re under pressure during a test. The main thing to remember is that the odd powers of 2 have negative signs and the even powers don’t.

I suppose it’s unfair for me to insult someone who’s trying to help students, but his slow solution isn’t going to be very helpful. And he shouldn’t pretend that his video has anything to do with Stephen Hawking.


  1. Can you be a regular reader of a blog as irregularly published as this one? Discuss. 

  2. Yes, I realize that this is an instructional video meant for young people and there’s value in a methodical pace. But no one who can follow the math in this video needs to see every single addition and multiplication. 


Touch and run

Here’s one last bit of followup on my Finder/Terminal tool posts. In the first post on the topic, I mentioned that I had created a bunch of zero-length JPEG files using the touch command. And in both the first and second posts, I talked about how long the sel command took when there were hundreds of files to select. I thought it worth a post on how I made hundreds of zero-length files to test out the script.

The short answer is this:

run -f 'img-{:03d}.jpg' 500 | xargs touch

which created, in the current directory, 500 files named img-001.jpg through img-500.jpg. We’ll turn this into a long answer by going through each part.

You won’t find the run command on your computer unless you read this post from a few years ago, in which I gave its Python source code. But I used run because I have it and like it. We’ll get to using commands that are on your computer further down.

The usage message for run is this:

Usage:
run [options] <stop>
run [options] <start> <stop>
run [options] <start> <stop> <step>

Generate a run of integers or characters. Similar to jot and seq.

Options:
    -f FFF   formatting string for number
    -s SSS   separator string
    -c       characters instead of integers
    -r       reverse the run
    -h       show this help message

The run of numbers can be integers or reals, depending on the values of start, 
stop, and step. The defaults for both start and step are 1. If -c is used, 
then start and stop must both be given as characters and step (if given) is an 
integer.

As you can see, I used run with an -f option to put each number generated into a string. The formatting code used in the argument to -f follows the Python format string syntax. Most of the code is repeated verbatim; the part inside the curly braces tells run to format each number as three characters long with leading zeros, if necessary. The output of the run command is

img-001.jpg
img-002.jpg
img-003.jpg
.
.
.
img-499.jpg
img-500.jpg

What we want to do is run the touch command with each one of these filenames as its argument, e.g.,

touch img-001.jpg

The main purpose of touch is to update the modification timestamp on the given file. But if the file given as the argument to touch doesn’t exist, a zero-length file of that name is created. It’s this feature we’re going to exploit.

The problem with the way touch works is that it doesn’t take the filename from standard input—it needs it to be an argument. So we can’t just pipe the output of run into touch. Luckily, the xargs command is available. It constructs an argument list from standard input (the list of img-nnn.jpg file names) and executes the given utility (touch) with that argument list. Boom. On my M1 MacBook Air, it takes about a tenth of a second to create all the files.

But run isn’t necessary. There are two commands already on your Mac, jot and seq, that do much the same thing as run. I wrote run because I don’t like the syntax of either jot or seq, but they’re both fine for this simple case. We can create the same files as above with

jot -w 'img-%03d.jpg' 500 | xargs touch

or

seq -f 'img-%03.0f.jpg' 500 | xargs touch

Both jot and seq use printf-style formatting codes, which are the parts that start with percentage signs. Note that jot uses -w instead of -f as the option and that seq treats the numbers as floating point values instead of integers. This latter is why its number formatting bit has to be %03.0f instead of the simpler 03d. These minor annoyances are some of the reasons I wrote run.

Unsurprisingly, jot and seq are faster than run, but they’re all so fast I had to use the time command to learn the difference between them. My preference for the syntax of run far outweighs its tiny additional runtime.

Update 24 Sep 2024 4:37 PM
Are you surprised to see an update? You shouldn’t be; there’s always more than one way to do it. In this case, the additional way was suggested by Jonathan Buys on Mastodon and it uses brace expansion:

touch img-{001..500}.jpg

No need for piping. Although I’ve linked above to the bash manual, the brace expansion works in zsh, too.

For me, there are a few downsides to this:

  1. I find it hard to remember brace expansion (although I may find it easier after writing this).
  2. When I do remember brace expansion, I tend to use a hyphen between the numbers instead of a pair of periods, and that doesn’t work at all.
  3. I like to double-check the filenames before creating the files, and I can issue the run command by itself to make sure the names are what I want before adding the pipe to args touch.

Now, it’s true that the Jonathan’s brace expansion solution can be checked by running something like

echo img-{001..020}.jpg

to check the file names (in this case, there’s no need to generate all 500) before reissuing the command with touch instead of echo, so there’s more than one way to run a test, too. Thanks to Jonathan for the suggestion!