Edit: I noticed a small mistake in the formulas below. I’ll try to get it fixed this week.
I visited a railroad museum today, and I saw a display showing how the piston is linked to the train wheel. For fun and learning I wanted to model the basic mathematics of how the linkage moves with the wheel and the piston, without looking up the answer on the Internet. That part seemed very simple:
Since l and p are fixed length, it was a matter of simple trigonometry, as seen above. Then I threw the math into a simple Racket program to simulate the movement. That part not hard, but it took an hour or two to add enough lines and circles to make the graphic look half-way decent. Here is a video recording of it running (about 10 seconds):
One interesting part of the math is the connection point of l and p (see the diagram above). Until you get very long lengths of l, you get something close to the cosine function but not quite the same.
In Emacs Calculator, frequently I come up with results that that are algebraically supposed to be a whole number, but instead because of precision error they end up as some annoying value that is very close. For example, I’m expecting a 0 but get “3.69600000004e-11” which is an extremely small value close to zero. For matrices, I defined this shortcut which maps over the elements, rounding them if they are “close enough” to the whole number.
After defining the macro I inserted it into ~/.emacs.d/calc.el:
The second video covers two subjects: (1) mapping functions over vectors, and (2) using emacs to display algebraic formulas as math LaTeX (to paste into a WordPress post, for example). Please forgive the improper pronunciation of “LaTeX”, which I remembered afterwards.
In my geometry studies, I learned that one can get the angle between two vectors with this formula:
I.e., the cosine of the angle equals the dot product of the two vectors over the product of their magnitudes.
Here we get about 1.05 radians or about 60.26 degrees. A cool thing about this formula is it works for vectors of any (matching) dimension, i.e., 3-D coordinates, 4-D coordinates, etc.
This is definitely doable in Emacs Calc, since we have a dot product function, called inner-product (press ‘x inner product’), But doing the angle formula involves a lot of steps, with either stack rotation or storing the vectors in variables. So I wanted to get the angle formula stored as a calc formula. Unfortunately, inner-product itself is only an interactive function, so this was problematic. However, inner-product actual calls another function, inner. So, this formula is possible:
How do you store this formula in Emacs? I could walk you through the steps described in section 18.4 of the Emacs Calc info manual, but the end result is that this code is stored in your ~/.emacs.d/calc.el:
This fractal idea did not originate with me, but I wrote some racket code to do the midpoint calculation using barycentric coordinates. This fractal draws a circle at the midpoint of a triangle, then subdivides the triangle and repeats:
Here is the same fractal to four iterations:
To get the midpoints, I could simple pass in the coordinates of the last triangle ABC, and then use “0.5” barycentric coordinates:
[P1 (barycentric->complex 0.5 0.5 0 A B C)]
[P2 (barycentric->complex 0.5 0.0 0.5 A B C)]
[P3 (barycentric->complex 0 0.5 0.5 A B C)]