online advertising

Wednesday, November 25, 2015

Algorithms: Design and Analysis, Part 1 - Exam Question 1

Problem:

Recall the Partition subroutine that we used in both QuickSort and RSelect. Suppose that the following array has just been partitioned around some pivot element: 3, 1, 2, 4, 5, 8, 7, 6, 9

Solution:

Any number that can be a pivot must have all left hand side smaller than it and right hand side larger than it, looking at the array, that will be these elements:

3, 1, 2, 4, 5, 8, 7, 6, 9

Therefore the answer is 4, 5, 9. I missed 9 during my own exam, just a careless mistake.

Monday, November 23, 2015

Focus and directix for a parabola

A parabola is defined as the locus of point where its distance to a line (called directrix) and a point (called focus) are equal.

As an canonical example, we define the directrix to be $ x = -a $, and the focus be $ (a, 0) $, then the parabola will be defined as point such that

$ (x + a)^2 = (x - a)^2 + y^2 $

Simplifying we will get $ y^2 = 4ax $.

The interesting thing is to find directix and focus from an arbitrary (let's say, still axis parallel) parabolas. It is best illustrated with an example

Suppose the parabola is $ -x^2 - 2y + 2x = 12 $

Now we have $ -x^2 $, so the parabola open downwards, it is best to deal with the positive coefficients, so we do:

$ -12 - 2y = x^2 - 2x $

Next we complete the square, this is trivial

$ -11 - 2y = (x - 1)^2 $

Last we just fit it into the form we want:

$ 4\frac{1}{2}(y + \frac{11}{2}) = -(x - 1)^2 $

So we know it is just  a shift of the standard form $ 4ay = x^2 $, the vertex is $ (1, \frac{-11}{2}) $, the focus is $ (1, -6) $, the directrix is $ y = -5 $. The focus and directix are found by moving $ a = \frac{1}{2} $ units from the vertex.

Here is a plot showing the elements



Last but not least, it is always nice to check answer:

$ (y + 5)^2 = (x - 1)^2 + (y + 6)^2 $
$ y^2 + 10y + 25 = x^2 - 2x + 1 + y^2 + 12y + 36 $
$  -x^2 - 2y + 2x = 12 $

So yes, we have got the right answer!

Saturday, November 21, 2015

Scientific Computing - Quiz 2 - Question 3-6

Problem:

Consider the differential equation
$ \frac{dy}{dt} = y $

with y(0)=1. The exact solution to this problem is given by $ y(t)=e^t $. Knowing the exact solution and using $\Delta t=0.1$ with the Euler scheme, compute the local error for two iterative steps ((a) $\epsilon_1$, (b) $\epsilon_2$) and compute the global error ((c) $E_1$, (d) $E_2$) at each of the three steps.

Solution:

The local error computation is exactly the same as the last problem.

$ \epsilon_1 = e^{0.1} - (1 + 0.1) \approx  0.0052 $
$ \epsilon_2 = e^{0.2} - (e^{0.1} + 0.1e^{0.1}) \approx 0.0057 $

For the 1st step, the global error is actually the same as the local error.

$ E_1 = \epsilon_1 = 0.0052 $

For the 2nd step, we need to actually iterate twice.

$ y_1 = 1 + 0.1 $
$ y_2 = y_1 + 0.1 y_1 = 1.21 $

Therefore $ E_2 = e^{0.2} - 1.21 \approx 0.011 $

Notice an important observation here - $ \epsilon_2 \neq E_2 - E_1 $. The error introduced in the last step also lead to wrong slope estimation, the local error of the second step assumed the correct slope will be there!

Scientific Computing - Quiz 2 - Question 1,2

Problem:

Question 1:

Consider the function $f(t)=\cos(t)$. What is the error bound associated with expanding the function $f(t+\Delta t)$ about $ t $using $\Delta t=0.1$. In particular, compute the error bound when truncated at the second-derivative (keep only the first two significant-digits).

Question 2:

Consider the function $f(t)=\cos(t)$. What is the error bound associated with expanding the function $f(t+\Delta t)$ about $t $ using $\Delta t=0.1$. In particular, compute the error bound when truncated at the third-derivative (keep only the first two significant-digits).

Solution:

Consider the Taylor expansions:

For question 1:

$ \cos(t + \Delta t) = \cos(t) - \Delta t \sin(t) - \frac{\Delta t^2}{2}\cos(c) $
So the error bound is the maximum value of the last term, which is $ \frac{0.1^2}{2} = 0.005 $

For question 2:

$ \cos(t + \Delta t) = \cos(t) - \Delta t \sin(t) - \frac{\Delta t^2}{2}\cos(t) + \frac{\Delta t^3}{6}\sin(c) $
So the error bound is the maximum value of the last term, which is $ \frac{0.1^3}{6} = 0.00017 $

Scientific Computing - Quiz 1 - Question 8

Problem:

Derive the Adams-Moulton scheme and Adams-Bashforth scheme that uses $y_{n+1}, y_{n}, f_{n-1}$ and $y_{n}, f_{n-1}, f_{n-2}$ respectively.

Solution:

To approximate a polynomial with three points, we use the Lagrange's polynomial.

$ \begin{eqnarray*} p(t) &=& f_n\frac{(t - (n-1)\Delta t)(t - (n-2)\Delta t)}{(n \Delta t - (n-1)\Delta t)(n \Delta t - (n-2)\Delta t)} + f_{n-1}\frac{(t - n\Delta t)(t - (n-2)\Delta t)}{((n - 1)\Delta t - n\Delta t)((n - 1)\Delta t - (n-2)\Delta t)} + f_{n-2}\frac{(t - n\Delta t)(t - (n-1)\Delta t)}{((n - 2)\Delta t - n\Delta t)((n - 2)\Delta t - (n-1)\Delta t)} \\ &=& f_n\frac{(t - (n-1)\Delta t)(t - (n-2)\Delta t)}{2 \Delta t^2} + f_{n-1}\frac{(t - n\Delta t)(t - (n-2)\Delta t)}{-\Delta t^2} + f_{n-2}\frac{(t - n\Delta t)(t - (n-1)\Delta t)}{2\Delta t^2} \\ &=& \frac{1}{2\Delta t^2} (f_n(t - (n-1)\Delta t)(t - (n-2)\Delta t) - 2f_{n-1}(t - n\Delta t)(t - (n-2)\Delta t) + f_{n-2}(t - n\Delta t)(t - (n-1)\Delta t)) \\ \end{eqnarray*} $

Note that while the expression is long, it is simply a quadratic polynomial with a lot of awful looking constants. To simplify things, let $ u = t - n\Delta t $

$ \begin{eqnarray*} p(u) &=& \frac{1}{2\Delta t^2} (f_n(u + \Delta t)(u + 2\Delta t) - 2f_{n-1}(u)(u + 2\Delta t) + f_{n-2}(u)(u + \Delta t)) \\ &=& \frac{1}{2\Delta t^2} (f_n(u^2 + 3u\Delta t + 2\Delta t^2) - 2f_{n-1}(u^2 + 2u\Delta t) + f_{n-2}(u^2 + u\Delta t)) \\ &=& \frac{1}{2\Delta t^2} (u^2(f_n - 2f_{n-1} + f_{n-2}) + u\Delta t(3f_n - 4f_{n-1} + f_{n-2}) + 2f_n\Delta t^2) \\ \end{eqnarray*} $

Ugly as is, we can integrate now.

$ \begin{eqnarray*} y_{n + 1} - y_{n} & = & \int\limits_{t_n}^{t_{n+1}}{f(t,y)dt} \\ y_{n + 1} & = & y_{n} + \int\limits_{t_n}^{t_{n+1}}{f(t,y)dt} \\ & \approx & y_{n} + \int\limits_{t_n}^{t_{n+1}}{p(t)dt} \\ & \approx & y_{n} + \int\limits_{0}^{\Delta t}{p(u)du} \\ & \approx & y_{n} + \int\limits_{0}^{\Delta t}{\frac{1}{2\Delta t^2} (u^2(f_n - 2f_{n-1} + f_{n-2}) + u\Delta t(3f_n - 4f_{n-1} + f_{n-2}) + 2f_n\Delta t^2)du} \\ & \approx & y_{n} + \frac{1}{2\Delta t^2} \int\limits_{0}^{\Delta t}{(u^2(f_n - 2f_{n-1} + f_{n-2}) + u\Delta t(3f_n - 4f_{n-1} + f_{n-2}) + 2f_n\Delta t^2)du} \\ & \approx & y_{n} + \frac{1}{2\Delta t^2} (\frac{u^3}{3}(f_n - 2f_{n-1} + f_{n-2}) + \frac{u^2}{2}\Delta t(3f_n - 4f_{n-1} + f_{n-2}) + 2uf_n\Delta t^2) |_{0}^{\Delta t} \\ & \approx & y_{n} + \frac{1}{2\Delta t^2} (\frac{\Delta t^3}{3}(f_n - 2f_{n-1} + f_{n-2}) + \frac{\Delta t^3}{2}(3f_n - 4f_{n-1} + f_{n-2}) + 2\Delta t^3f_n) \\ & \approx & y_{n} + \frac{\Delta t}{2} (\frac{1}{3}(f_n - 2f_{n-1} + f_{n-2}) + \frac{1}{2}(3f_n - 4f_{n-1} + f_{n-2}) + 2f_n) \\ & \approx & y_{n} + \frac{\Delta t}{12} (2(f_n - 2f_{n-1} + f_{n-2}) + 3(3f_n - 4f_{n-1} + f_{n-2}) + 12f_n) \\ & \approx & y_{n} + \frac{\Delta t}{12} (23f_n - 16f_{n-1} + 5f_{n-2}) \\ \end{eqnarray*} $

This is the Adam-Bashforth scheme - I will skip the Adam Moulton scheme, derivation like this is best leave to the computer...

We have the correct answer at https://en.wikipedia.org/wiki/Linear_multistep_method, and it is great to see the answer matches (to be honest to my future self, I did spend great time to fix the derivation to match the solution, the very detailed derivation does help me a lot to fix the bug without redoing everything)

Scientific Computing - Quiz 1 - Question 7

Problem:

Reconsider the fundamental theorem of calculus as expressed in (1.1.16 found in the lecture note packet). But now, integrate from $t-\Delta t$ to $t+\Delta t$. Using (1.1.19), what is the new version of (1.1.20):

Solution:

We start with $ y(t + \Delta t) - y(t - \Delta t) = \int\limits_{-t - \Delta t}^{t + \Delta t}{f(t, y)dt} $.

Next we approximate the integral by constant and put terms around, we get

$ y_{n+1} = y_{n-1} + 2\Delta t f(t, y_n) $.

Scientific Computing - Quiz 1 - Question 6

Problem:

Walk through one time-step of 4th-order Runge-Kutta with simple differential equation system $y'=y$, $y(0)=1$ and $\Delta t=2$.
Identify the correct values for f1; f2; f3; f4 and y(2).

Solution:

It is always good to know the solution to the differential equation first, in this case it is really easy.

$ \begin{eqnarray*} \frac{dy}{dt} &=& y \\ \frac{1}{y}\frac{dy}{dt} &=& 1 \\ \int{\frac{1}{y}\frac{dy}{dt}dt} &=& \int{1dt} \\ \log y &=& t + C_1 \\ y &=& e^{t + C_1} \\ &=& C_2e^t \\ \end{eqnarray*} $

Now the initial condition decides $ C_2 = 1 $, making the solution simply $ y = e^t $.

The analytic solution is not required, it is just that it is a perfect guide for us to evaluate whether our solution make sense. Now, looking up the Runge-Kutta equations, we have

$ f_1 = f(t_n, y_n) $
$ f_2 = f(t_n + \frac{\Delta t}{2}, y_n + \frac{\Delta t}{2} f_1) $
$ f_3 = f(t_n + \frac{\Delta t}{2}, y_n + \frac{\Delta t}{2} f_2) $
$ f_4 = f(t_n + \Delta t, y_n + \Delta t f_3) $

Next, we substitute $ f(t, y) = y $ in the expressions above, then we get:

$ f_1 = y_n $
$ f_2 = y_n + \frac{\Delta t}{2} f_1 $
$ f_3 = y_n + \frac{\Delta t}{2} f_2 $
$ f_4 = y_n + \Delta t f_3 $

Excel will just happy to crank those answers for us now.

The final answer is

$ f_1 = 1 $
$ f_2 = 2 $
$ f_3 = 3 $
$ f_4 = 7 $
$ y(2) = 7 $.

With excel, I fiddled around the parameter. In particular, I tried to tune $ \Delta t $ to 0.1 and iterate 20 times, the result has $ 1.1 \times 10^{-5} $ error only. It is in fact quite accurate.

As an aside to the analytic solution - the integral of $ \int{\frac{1}{y}dy} = \log{y} $ only make sense when $ y > 0 $. To make sense of cases when $ y < 0 $, we can take the sign out first before the integration and the expression become $ \log|y| = sign(y)t + C_1 \implies e^{|y|} = e^{|sign(y)|}e^{t + C_1} \implies e^{y} = e^{t + C_1} $ and the rest follows as is.