Thursday, October 15, 2015

river math problem

So a few days ago, my attention was brought to this article. It's a very straight-forward minimization problem. The crocodile/zebra spin is just fluff, the equation that needs minimized is given to the student. Find the derivative and evaluate it at zero. I can't imagine this being "too hard" for anyone who has taken Calculus 1.

I got to thinking about it, and decided to try to find a general solution. So I needed to set up the original equation. That wasn't too hard. Given width of the river, W; the distance along the bank to the target, L; and the relevant velocities (swimming, Vs; running, Vr; and flow speed, Vf (not considered in the given formula, but what the hell) the relationship is pretty easy to find. I decided to make it a function of the angle at which the crocodile swims (in my set up 0° is directly across the river, and the angle increases clockwise).

So: total time is the sum of swimming time and running time. Swimming time is easy. It is the distance across the river (at the given angle) divided by the swimming speed. Running time is a little trickier, but it works out to the absolute value of the horizontal distance to the target minus the horizontal distance covered by the swimming angle, plus drift distance. Drift distance is swimming time times flow speed. So, the total time as a function of angle is:

Taking the derivative and minimizing gives:

Check it out. It looks like the W and L don't matter. Just the ratio of velocities. But if the swimming speed is higher than the running speed plus flow speed, the result is meaningless. It turns out that even if it is close, the angle it gives is ridiculous. It isn't obvious from any of this but the function has a discontinuity cusp where
tan(θ) = L/W, and the minimization will only work to that point.

I was having a hard time showing this on paper, so I did what I always do in these situations: I wrote a program to do it for me. I used the browser-based Python interpreter at CodeSkulptor

Here it is. Use this one. Feel free to play with it.(The last I knew, CodeSkulptor doesn't work right in IE) You can change the distances and velocities. Note that negative W, Vr, and Vs are nonsense. The program can't handle negative L. Negative Vf is fine, though.

Here's a screen shot of the solution to the original problem:


Here's a screenshot of one of the weird results.


(FIX: there was a bug in my drift calculation when I took that. The minimum time will never have the crocodile landing upstream of the target. It is fixed in the final version)

Later,

UPDATE: I changed the color scheme for better contrast, and fiddled with the scale and offset to show that the discontinuity cusp is still there in the original problem




The discontinuity cusp is at θ = atan(L/W) if there is no drift. Adding drift changes the angle at which the crocodile would swim directly to the target. That is where the run time calculation (the absolute value term in the total time equation) is zero. I'm not sure I can simplify it to a relationship. I can see that Vr divides out, and the angle depends on W, L, Vs, and Vf. Maybe I'll work on it more this weekend.

Later,

UPDATE #2: Okay, I worked out the relationship that determines the angle where the discontinuity cusp is. It is a horribly messy quadratic:

The quadratic formula technically has '+ or -', but the '-' case can be discounted because that result is outside of the range the problem makes sense for, [0°,90°). If flow speed (Vf) is zero, this simplifies to cos(θ) = W/√W²+L², which equates to tan(θ) = (L/W).

Aside from the initial minimization that made me aware of the discontinuity cusp , this has all been algebra and trigonometry. Messy, sure, but not actually hard.

So here Use this one. is the final version of my program. Here is a screenshot of the original problem, showing the location of the discontinuity cusp:

Later,

UPDATE #(Again? WTF?): It occurred to me that I was using "discontinuity" wrong. The function is not discontinuous, it is continuous but not differentiable at the cusp. So, I have corrected the post.

Note to self: Do not use Blogger's "compose" mode. It adds a ton of weird, unnecessary html to the post.

Later,

UPDATE #(Seriously, what the hell is wrong with you?): I couldn't help thinking about the cases where the minimization doesn't apply. It really isn't that mysterious. As Vs approaches (or equals) Vr+Vf, the resulting θ would be beyond the cusp. which means that it doesn't apply. The total time function follows a different curve to the right and left of the cusp. There is actually a discontinuity in the curve. 1/cos(θ) → infinity as θ → 90°. Of course, the cusp usually comes before you get close to the discontinuity, so the minimum time will always be between 0° and the cusp. Well, if the flow speed is high, the cusp calculation can be outside the (-90°, 90°) range. but in that case the function is differentiable across that entire range, and the minimization applies. This got me wondering why my program didn't work for L < 0. It should have been obvious. I was only evaluating the equation for θ in [0°, 90°). If L < 0, θ < 0°. Once I fixed that to (-90°, 90°), I had to put an absolute value on the cos(θ) in the swim time evaluation (even if the angle at which the crocodile swims across the river is negative, the time it takes is still positive). I changed my graphics to reflect the change. So, once again, here is the actual, final version Use this one.
And the obligatory screenshot:

Later,

UPDATE #(Holy Shit! You're still on about this?): I couldn't stop fiddling with the program. I added Try-Except statements to the input handlers so that entering bad values wouldn't crash the program. Try it. I also made some corrections to the graph display. So here is the latest 'final' version.
And a screenshot:

Later,

Sunday, August 23, 2015

Cherry Marshamallows

This is the sort of thing that happens when I get bored:



The pink mess in the bowl is a previous batch that I left too close to a hot oven, and melted into a gooey pile.  Jac suggests making Rice Krispy treats with it.  I'll probably do that.

The plate on the right has the finished marshmallows from the second batch.  They are dusted in powdered sugar to keep them from sticking together.  They are actually purple, as can be seen on the exposed half-marshmallow.  These are awesome in hot chocolate or s'mores.

The plate on the left has the Frankensteinian remnants, and a few salvaged pieces from the first batch, dipped in chocolate.

The reason the first batch is pink, and the second purple is a change in the recipe. The original recipe called for cherry extract and a few drops of red food coloring.  I used the last of my cherry extract making it.  When I decided to make another batch, I went to the store to get more, but there wasn't any.  So I got cherry juice and substituted it for the water in the original.  I wasn't sure it would work, but I'm pretty happy with the results.  Next time I'll add some extract, too, though.  The cherry flavor is pretty subtle with just the juice.

So here's my (revised) recipe for Cherry Marshmallows.

Equipment:
   Electric mixer
   Whisk
   Rubber scraper
   Heavy-bottomed 2-qt saucepan
   Candy thermometer
   Medium mixing bowl (I prefer one with straight sides)
   Small microwave-safe bowl
   Baking dish (I used an 11"x7" brownie pan)
   Cling wrap
   Chef's knife

Ingredients:
   2 lg. egg whites, at room temperature
   1 c. cherry juice (I used black-cherry juice, I think that's why they're purple)
   3 envelopes unflavored gelatin
   2 c. granulated sugar
   1/2 c. light corn syrup
   1/4 t. salt
   1 t. cherry extract (or more for a stronger flavor)
   powdered sugar
   non-stick spray

Process:
   Line the baking dish with cling wrap and spray with non-stick spray.
   Mix gelatin and 1/2 c cherry juice in microwave safe bowl and set aside.
   Put egg whites in mixing bowl and set aside.
   Mix remaining 1/2 c juice, granulated sugar, corn syrup, and salt in saucepan.
   Heat on med-high.  Stir until sugar is dissolved.  Insert thermometer.
   Heat to 260°F.  Note - when it starts to boil, it will make a mess if the saucepan is
   too small.
   Watch the thermometer.  When the sugar mixture gets to about 240°F, start 
   beating the egg whites.
   They should form stiff peaks about the time the sugar gets to 260°.
   Microwave gelatin on high for 20 seconds.  Stir to fully liquefy.
   Whisk gelatin into sugar mixture.  Note - this will cause a lot of steam.  Be careful.
   Slowly pour sugar/gelatin mixture into egg whites while beating on low.  Not too
   slowly, if the sugar cools it will harden.
   Add cherry extract and gradually increase speed to med-high.
   Beat until very thick and glossy, 8 to 10 minutes.
   Pour into prepared dish.  Let sit at least 8 hours to allow gelatin to set.
   Dust (liberally) a cutting board with powdered sugar.  Flip marshmallow onto
   board and remove pan and cling wrap.  Dust marshmallow with powdered sugar.
   Cut into strips (then cubes) with chef's knife.  Dust knife with powdered sugar as it
   gets sticky. And it will.  
   Dust cubes with powdered sugar, then brush off excess.

Later, 

Saturday, May 16, 2015

Quote button

A long time ago, shortly after I added the "new quote" button on my random quote generator, Tom said it would be better to have the button above the quote so that it didn't move every time the quote length changed.  I didn't really care, so I never bothered.  Until just now.

Later,

Saturday, March 14, 2015

Happy Pi Day! Key Lime edition

Preheat oven to 350°F

Crust:
    1+1/4 c. graham cracker crumbs
    2 T. sugar
    1/3 c. melted butter (unsalted)

    mix together and press into pie plate.
    bake 10 minutes, let cool


Filling:
    3 egg yolks
    1+1/2 t. freshly grated key lime zest
    1 can (12oz) sweetened condensed milk
    2/3 c. freshly squeezed key lime juice

   Beat yolks and zest on high for 5 min
   Add condensed milk and continue to beat 4 more minutes
   Reduce speed to minimum.  Slowly pour in lime juice.  Stop when just combined

Pour filling into pie shell.  Bake 10 minutes.
 (if, like me, you made the shell too deep, and there isn't enough filling, make another batch, pour that into shell (pour any excess filling into a ramekin for a key-lime custard).  Bake another 10 minutes. )
Later,