Tuesday, July 12, 2011

MathJax in Blogger (II)

The new awesome way to include math in a website is MathJax. Until recently, one had to install MathJax in their own server in order to include math in their webpages. That changed a few weeks ago; you can now specify in your html code that the required script be loaded from MathJax's website.

To get MathJax to work in Blogger, just go to your Blogger account, click "Design" (top right of the page), and then "Edit HTML". After the first <head> you see, paste
<script src='http://cdn.mathjax.org/mathjax/latest/MathJax.js' type='text/javascript'>    
    MathJax.Hub.Config({
        HTML: ["input/TeX","output/HTML-CSS"],
        TeX:extensions: ["AMSmath.js","AMSsymbols.js"], 
               equationNumbers: { autoNumber: "AMS" } },
        extensions: ["tex2jax.js"],
        jax: ["input/TeX","output/HTML-CSS"],
        tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                   displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                   processEscapes: true },
        "HTML-CSS": { availableFonts: ["TeX"],
                      linebreaks: { automatic: true } }
    });
</script>

You're done! You can now use the usual $...$ or \(...\) for inline equations, and $$...$$ or
\[...\] for equations that are centered in their own line.

Time for an example:
\[
\cos^2\theta-\sin^2\theta=\cos 2\theta.
\] The above script loads AMSmath and AMSsymbols so that the macros defined in these popular packages are available for use in your blog.

UPDATE: Equations can now be numbered and referred to. For example, in Newtonian gravity we have
\begin{equation}\label{eq:gravt}
F=G\frac{mM}{r^2}.
\end{equation}Also,
\begin{equation}\label{eq:gravo}
F=mg,
\end{equation}and so equations (\ref{eq:gravt}) and (\ref{eq:gravo}) lead to\[
g=G\frac{M}{r^2},
\]assuming the equivalence of inertial and gravitational mass.

Tuesday, February 15, 2011

We're lucky $(1+x/n)^n$ converges!

If you're ever interested in borrowing money from a bank, here are some calculations that can help you make the right choice.

Suppose you want to borrow an amount of money $m$, at a yearly interest rate of $x\%$ for a $y$ number of years. The first thing you'd wonder is how much your monthly payment will be. Well, to calculate that, you have to know exactly how the bankers have done the math. As expected, they have tried to maximize the interest you pay to them.

Here's how it goes. The yearly interest rate is not actually applied every year, as any reasonable human being would expect, but is divided by 365 and applied every day. The reason for that will be clear below, but let's assume for now, in order to make the math easier, that the interest is applied every year.

At the beginning of the first year you borrow $m$. At the end of the first year you owe \[m-12p,\] where $p$ is the monthly payment. At this point the interest is applied and at the beginning of the second year you owe \[m_2=\left(1+\frac{x}{100}\right)(m-12p).\]At the end of the second year you owe $m_2-12p$, and at the beginning of the third year you owe \[m_3=\left(1+\frac{x}{100}\right)\left[\left(1+\frac{x}{100}\right)(m-12p)-12p\right],\] which simplifies to \[m_3=\left(1+\frac{x}{100}\right)^2 m-12\left[1+\left(1+\frac{x}{100}\right)\right]p.\] From this form of $m_3$ and after a moment's thought (which, by the way, might take more than a moment), it becomes clear that at the beginning of the $n$-th year you owe \[m_n=\left(1+\frac{x}{100}\right)^{n-1}m-12\sum_{i=1}^n \left(1+\frac{x}{100}\right)^{i-1}p.\] The sum can be recognized as the sum of the first $n$ terms of a geometric sequence with ratio $1+x/100$, and is equal to \[\sum_{i=1}^n \left(1+\frac{x}{100}\right)^{i-1}=n\left(1+\frac{x}{100}\right)^{n-1}.\] Since at the end of the loan term you can't owe anything, you set \[m_y=0,\] from which you can finally calculate the monthly payment, \[p=\frac{m}{12}\frac{\frac{x}{100}\left(1+\frac{x}{100}\right)^{y-2}}{\left(1+\frac{x}{100}\right)^y-1}.\] If you followed the math so far that's good. It should be obvious, though, that if a banker did the math that way, then they would be out of business very quickly, for that way they actually allow you to pay off capital without them having charged any interest for it. So it sort of makes sense that they use slightly more sophisticated math, but as I'll show they take it to the opposite extreme.

As I already mentioned the calculation of the monthly payment is actually done with the interest divided by the number of days in a year and applied every day. The equation from which the monthly payment follows is \[\left(1+\frac{x}{100d}\right)^{yd-1}m-\frac{12}{d}\sum_{i=1}^{yd} \left(1+\frac{x}{100d}\right)^{i-1}p=0,\] where $d$ is the number of days in a year, which, to account for leap years, you can take to be 365.2422. It is important to realize that the bank increases the frequency with which they apply the (divided by the frequency) interest, which brings them more money. This is just because of the behavior of the sequence \[a_n(z)=\left(1+\frac{z}{n}\right)^n;\]the higher the $n$ the bigger the $a_{n}(z)$. However, $a_n(z)$ converges to $e^z$, and so the bankers can't just take the frequency to infinity and have as pay as much interest as they want. The title of the post should now make sense; however, although we should be happy that $(1+z/n)^n$ converges, we can still be upset that it doesn't converge fast enough.

Note that the monthly payment we calculated in the previous paragraph was calculated as if they did allow you to pay every day, i.e. as if there was really an appropriately calculated daily payment that gave the monthly payment. That would be a fair system. However, the bankers are not fair; they actually apply the interest every day, but they only allow you to pay every month instead of every day. Needless to say, this produces a monthly payment higher than the one in the previous scenarios we considered. Think about this a little bit; they keep applying the (appropriately divided) interest every day, and they don't allow you to intervene and do something about it until the end of the month (some banks allow biweekly payments). By the way, the equation that gives $p$ with the current system is actually (I assume that a month has $365.2422/12=30.43685$ days) \[\left(1+\frac{x}{100d}\right)^{yd-1}m-\sum_{i=1}^{12y}\left(1+\frac{x}{100d}\right)^{30.43685(i-1)}p=0.\]

Tuesday, February 8, 2011

MathJax in Blogger

I recently came across MathJax, the amazing new open-source approach to displaying $\rm{\LaTeX}$ on the web. Finally we can use fonts for $\rm{\TeX}$ on the web, instead of these ridiculously ugly figures that substitute the $\rm{\TeX}$ code, and whose size and alignment induces vomiting.

One can install MathJax on their server and use it to include beautifully-typeset math in their webpages. To get it to work in Blogger, follow the steps below.
  1. ssh into your server and install MathJax as described here (preferably with the svn method so that you can easily update MathJax in the future).
  2. Since (most likely) Blogger is not hosted on your server, follow the instructions here. You basically need to create the file .htaccess in the server's mathjax/fonts/HTML-CSS/TeX/otf folder and paste the following text in it:
    <FilesMatch "\.(ttf|otf|eot)$">
    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    </IfModule>
    </FilesMatch>
  3. Finally, go to your Blogger account, click "Design", and then "Edit HTML". After the first <head> you see, paste
    <script type="text/javascript" src="your-server/mathjax/MathJax.js">
        mathjax.Hub.Config({
            extensions: ["tex2jax.js","TeX/AmsMath.js","TeX/AMSsymbols.js"],
            jax: ["input/TeX","output/HTML-CSS"],
            tex2jax: {
                inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                processEscapes: true,
            },
            "HTML-CSS": { availableFonts: ["TeX"] }
        });
    </script>
    where your-server is to be substituted with your server's http address.
You're done! You can now use the usual $...$ or \(...\) for inline equations, and $$...$$ or
\[...\] for equations that are centered in their own line.

Time for an example:\[\sin^2\theta+\cos^2\theta=1.\]Note that the code above loads AMSmath.js and AMSsymbols.js, so that the macros and environments they provide are defined for use on your blog.