Wednesday, February 16, 2011

MathJax

To get equations that just work on your site, go to http://www.mathjax.org/.

On those pages, there are several options, since it is a very flexible program, but the simplest is to copy the following text into your blog template or webpage:

<script type="text/javascript" src="http://www.mathjax.org/mathjax/MathJax.js">
    MathJax.Hub.Config({
        extensions: ["tex2jax.js"],
        jax: ["input/TeX", "output/HTML-CSS"],
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        },
        "HTML-CSS": { availableFonts: ["TeX"] }
    });
</script>

I found this after a much longer search than I thought at the configuration webpage.  Now what they actually have for the source is "path-to-MathJax/MathJax.js" and encourage you to get your own copy.  However, I don't have a place to put the downloaded mathjax stuff, so I just used theirs.  I don't think this is what they really want, but I didn't see a way around it.

There are lots of LaTeX commands you can use, here is a list

For example, this just works

$$\frac{1}{1+\frac{1}{1+\frac{1}{1+\frac{1}{1+\frac{1}{1+\frac{1}{1+\ldots}}}}}}$$

Wednesday, February 9, 2011

Barcodes!

For some crazy reason, I wanted to make barcodes.  I have no real reason why.  I guess I wanted to be able to read barcodes, or make a reader for barcodes.  So in order to do that I need to be able to make them.

Of course there are TONS of barcode systems.  Some are very tempting, but one of the simplest seems to be Code 128.  This is used fairly often, and it seemed pretty robust.

Barcodes are basically set up with a few bars in the front to tell you which part of 128 you are using (A, B, or C), then individual characters come along.  Each character is 6 stripes long starting with black and alternating.  Varying the thickness of the stripes will give you each character.  So, for example, "A" is 111323, that is, 'a black stripe that is 1 unit wide, a white stripe that is 1 unit wide, a black stripe that is 1 unit wide, a white stripe that is 3 units wide, a black stripe that is 2 units wide, a white stripe that is 3 units wide.  Look at the animation below



The end of the bar code is a check number and a stop character that is 7 bars long and unique.  Here is the chart that I used from Wikipedia



There are multiple ways to encode a message, so I just used code B throughout.  There are Shift keys so that the following character is in the shifted language.  Also, you can encode '12' as a '1' followed by a '2' or as a single character '12'.  I wonder if I could make something so that given a message it would give me the simplest barcode... hmm...

Anyway, I encoded messages using just column B using Mathematica.

I first changed a string like "Hello World" to its list of black and white bar notation using









So now I can try "Hello World"

 


encode uses another function to find the check number


This is not too important, and I won't go into it.  Basically, it calculates another character based on all of the characters in your text.  This is a standard way of know when you got the right answer when you are reading it.  Just do a quick check.

Then I plot it (with a bunch of rectangles)

I only plot black rectangles because that is all I need to plot.

Here are some examples


Note that whatever the message is, it always starts with 211214 and always ends with 2331112.

Next, on to the barcode reader!

Wednesday, February 2, 2011

More envelopes than a post office!


I wish I had more time to explore this. I found a passing comment in S. V. Meleshko's Methods for Constructing Exact Solutions of Partial Differential Equations about envelopes of curves. I will get to his statement at the end. Basically suppose we have a family of curves like the blue ones below




This is an example of a one parameter family of curves. Specifically, I made this by finding lines where the intercepts always add to 10,
 $$y(x,a)= \frac{a - 10}{a} x + 10 - a.$$
This begs the question of finding the envelope of the family of curves shown in black in the figure. What is the equation that describes the envelope? How do we find it?


I have always been fascinated by these types of curves ever since I did a string art project in middle school. I wondered, as I do now, what types of curves you could produce.


This has been addressed before in various places, and I gather that the topic used to be quite common in Calculus courses.  However, in case you don’t see it right away, here is a way to obtain the envelope. 

Let’s fix $x$. Now for this $x$, say $x_0$, we have a relation between $y$ and $a$ through the equation for a curve in the family.  We can think of the envelope as an extremum of the relationship between $y$ and $a$. Illustrating this graphically, if we consider the intersection of the vertical line $x_0$ and our family of curves $y(x_0,a)$, then as we scan through $a$, we will see that there are special points where the intersection point reaches a maximum.  This can be seen in the animation below (right click on the image below and select 'open in a new tab').




As we scan through $a$, we can see the intersection point (in red).  When it slows down and changes direction, this is a point of the envelope.


This is exactly $g(x_0)$. We can then describe the envelope equation as
$$g(x) = y ( x, a_{*} (x) ) $$
where $a_{*}(x)$ is found by finding the extremum of $y(x,a)$ with respect to $a$. Thus $a_{*}(x)$ is the solution to
$$\frac{\partial y(x,a)}{\partial a}=0.$$
For our case explicitly, the solution is
$$g(x)=10 \pm 2 \sqrt{10} \sqrt{x} + x.$$
A graph of these two curves can be seen in black in the first figure.

Note also from the way we have written the envelope that every point of $g(x)$ necessarily lies on a specific curve in the family of curves. This means that at each point a particular curve from our family is tangent to the envelope. Likewise, we also know that the envelope is tangent to one of our curves in the family. This is a good thing to recognize because from this we may construct a differential equation. We know that
$$g(x) = \frac{a_{*}-10}{a_{*}} x + 10 - a_{*},$$
$$g'(x)=\frac{a_{*} - 10}{a_{*}}.$$

From this, we may eliminate $a_*$ to obtain a differential equation for $g(x)$. In this case we get
$$g'(x)(10-x+x g'(x))=g(x)(g'(x)-1).$$
This is a nonlinear, first-order differential equation.  The solution for this differential equation is then the envelope function. If we plug the envelope in, we find that it is, in fact, a solution.

The interesting point is that the two branches of the envelope are not the only solutions! All of the curves in the family are also solutions to this nonlinear differential equation. Since it is not a linear differential equation, there is not a uniqueness or existence theorem. We can see that the one parameter family of curves satisfies this equation since they satisfy the equations that the differential equation was derived from.  I was just shocked that a family of lines could satisfy such an unruly differential equation.

Oftentimes we are confronted with a nonlinear differential equation where we are excited when we are able to get one solution to the problem. This is an example of an infinite number of solutions to a complicated looking differential equation. But most (an infinite number) of the solutions are just straight lines!

The converse of the above is actually easier to prove and we take it out of Meleshko (not the proof -- although it is not hard).  He says it in passing, and for two dimensional surfaces.  Basically, he says (on page 9) that if you have a family of curves that is a solution of a differential equation, then the envelope is also a solution!  Amazing!


Anyway, I know this is too long for anyone to read, but I will end with some interesting families of curves and their envelopes.


Here is a family of lines with sin waves on top
 $$y_{1}(x,a)= \frac{a - 10}{a} x + 10 - a + \sin(10 x).$$





Here is a family of exponentials
$$y_{2}(x,a) = a e^{\frac{x}{a}}$$




Here is a family of gaussians
$$y_{4}(x,a) = a e^{-(x-a)^2}$$



Here is another family of gaussians
$$y_{5}(x,a) = \frac{1}{\sqrt{\pi} a} e^{-\left(\frac{x}{a}\right)^2}$$



Here is one where I was playing with lines again
$$y_{5}(x,a) = \frac{a^2 - 1}{a} \left( \frac{x}{a}+ \frac{1}{2} \right) - a$$



Two and three dimensions should be fairly straightforward to do (although I have not).  What I would like to do is to be able to go backwards.  I have played enough to make a

Conjecture: any reasonable curve can be an envelope to a family of lines.

I want to make this explicit, but that is a topic for another day.
Powered by MathJax