Thursday, September 3, 2020

Mathpix Snip

Originally Posted on  by hsauro

I came across this amazing tool that can convert images of math equations into LaTeX format. The tool can be found at I’ve tried it on a number of texts including some not so clear and it does a fantastic job of converting to LaTeX. Here is a screen showing part of a page from Paul’s Online Notes:

The way it works is you select the screen icon on the mathpix tool, the entire screen goes black and white, then we draw a square around the section we want to convert and that’s it. In this case, it generates the following latex

We’ll start with finding the derivative of the sine function. To do this we will need to use the definition of the derivative. It’s been a while since we’ve had to use this, but sometimes there just isn’t anything we can do about it. Here is the definition of the derivative for the sine function.
\frac{d}{d x}(\sin (x))=\lim _{h \rightarrow 0} \frac{\sin (x+h)-\sin (x)}{h}
since we can’t just plug in $h=0$ to evaluate the limit we will need to use the following trig formula on the first sine in the numerator.
\sin (x+h)=\sin (x) \cos (h)+\cos (x) \sin (h)
Doing this gives us,
\frac{d}{d x}(\sin (x)) &=\lim _{h \rightarrow 0} \frac{\sin (x) \cos (h)+\cos (x) \sin (h)-\sin (x)}{h} \\
&=\lim _{h \rightarrow 0} \frac{\sin (x)(\cos (h)-1)+\cos (x) \sin (h)}{h} \\
&=\lim _{h \rightarrow 0} \sin (x) \frac{\cos (h)-1}{h}+\lim _{h \rightarrow 0} \cos (x) \frac{\sin (h)}{h}
As you can see upon using the trig formula we can combine the first and third term and then factor a sine out of that. We can then break up the fraction into two pieces, both of which can be dealt with separately.

Which when processed by LaTeX becomes:

This is rendered inside the mathpix tool but you’ll notice there isn’t a significant difference between the original and the converted image. I’ve converted some fairly rough images and it generally succeeds. It also gives you a confidence level on how well it thinks it’s done. It took under a second to generate the LaTeX.

As a harder test, I decided to attempt to translate a page from Jim Burns’ thesis. This is a thesis from the 1970s that was typed and the equations a combination of typed characters and hand drawn. The following image shows page 93 which is part of the proof for the connectivity theorem.

And here is the image analyzed by mathpix. Remarkably the conversion is almost perfect, the equations, in particular, are translated almost without error, even getting the subscripts on the subscripts correct. It got the delta F1 wrong at the start and it interpreted a mark on the paper as an apostrophe. I tried other pages that included derivatives and these converted without incident.

Wednesday, April 22, 2020

PID Control Demonstration

Those of you who are interested in feedback control may have come across the infamous PID controller. To many engineers, PID control is the answer to all their problems. What is it for? PID controllers are a sophisticated way to stabilize a system that is under continual threat of disturbance. The simplest example is the cruise control in a car. Most of us probably set the cruise control and forget about it. What’s happening behind the scenes is however much more interesting. As we go up a hill we notice the engine revving up to maintain the set cruise speed and when we go down a hill the engine slows down. The simplest feedback mechanism is where the controller compares the speed of the car with the set cruise speed and adjusts the engine accordingly. The secret to a good controller such as this is what it does with the error it finds between the actual speed of the car and the desired speed. The simplest solution is to adjust the speed of the engine directly in proportion to the difference, this is called proportional control and is most familiar to engineers. The problem with proportional control is twofold:

1. Too much proportional control and the response overshoots, perhaps so much so that it goes into uncontrolled oscillations, not a good thing in a car.

2. You usually have to set the cruise control slightly higher than the speed you actually want because a proportional controller finds it difficult to actually match the set speed unless the proportional control is strong enough (See problem 1!)

Both these problems can be solved by combining proportional control (The P in PID) with two others, integral (The I in PID) and derivative control (The D in PID). Derivative control is perhaps the easiest to understand. What derivative control does is measure how fast the error between desired and actual engine speed changes. If the difference is changing too fast then we want to slow down the response to avoid overshooting. Derivative control will therefore help get rid of the instability (Too much derivative control can however also result in instability).

The second problem is that the speed of the car doesn’t actually settle to the desired speed. This is fixed by using integral control. This is where the error is continually added up to form a net error and it is the net error than is then used to control the speed of the engine. So long as there is even a little bit of error (ie Desired speed 50 mph, actual speed 49 mph), the net error will increase and drive the car speed to the desired speed until the actual error is zero. Integral is particularly useful if there is a continual disturbance at work, for example, a headwind.

To demonstrate these capabilities, I have written a small PID control demonstration tool (for windows only I am afraid) that simulates each of the three elements in a PID controller. The screenshot below shows the application. A user can control the strength of the different controlling elements or remove them all together. To test the response a user can apply a step change to the desired car speed and see how the system responds. The simulation is continuous so it is easy to see the effects.

Partly supported by the NSF