online advertising

Saturday, November 21, 2015

Scientific Computing - Quiz 1 - Question 5

Problem:

Using Eqs.~(1.1.10)-(1.1.12) found in the lecture notes found under the course resource link, derive the three important schemes: Heun's method (A = 1/2), modified Euler-Cauchy (A = 0) and Euler (A = 1).

Solution:

Here is the link to the full lecture notes:

The derivation based on 1.1.12 is pretty easy, for Heun's method:

$ A + B = 1 \implies B = \frac{1}{2} $.
$ PB = \frac{1}{2} \implies P = 1 $.
$ BQ = \frac{1}{2} \implies Q = 1 $.

For Euler-Cauchy method:

$ A + B = 1 \implies B = 1 $.
$ PB = \frac{1}{2} \implies P = \frac{1}{2} $.
$ BQ = \frac{1}{2} \implies Q = \frac{1}{2} $.

For Euler method:

$ A + B = 1 \implies B = 0 $.
P and Q are meaningless in the context of the iteration, we do not want any correction at all in this case.

Scientific Computing - Quiz 1 - Question 4

Problem:

Using a Taylor expansion for $ y(t+\Delta t) = y_{n+1} $ and $ y(t-\Delta t)=y_{n-1} $, derive an Euler-type formula for the future state of the system $ y' = f(y,t) $.

Solution:

The Taylor expansions are:

$ y_{n+1} = y(t + \Delta t) = y(t) + \Delta t \frac{dy}{dt}(t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_1) = y(t) + \Delta t f(y, t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_1) $
$ y_{n-1} = y(t - \Delta t) = y(t) - \Delta t \frac{dy}{dt}(t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_2) = y(t) - \Delta t f(y, t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_2) $

Now subtract the equations to get our answer
$ \begin{eqnarray*} y_{n+1} - y_{n-1} &=& (y(t) + \Delta t f(y, t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_1)) - (y(t) - \Delta t f(y, t) + \frac{\Delta t^2}{2} \frac{d^2y}{dt^2}(c_2)) \\ &=& 2\Delta t f(y, t) + \frac{\Delta t^2}{2}(\frac{d^2y}{dt^2}(c_1) -\frac{d^2y}{dt^2}(c_2)) \\ y_{n+1} &=& y_{n-1} + 2\Delta t f(y, t) + \frac{\Delta t^2}{2}(\frac{d^2y}{dt^2}(c_1) -\frac{d^2y}{dt^2}(c_2)) \\ & \approx & y_{n-1} + 2\Delta t f(y, t) \end{eqnarray*} $

Scientific Computing - Quiz 1 - Question 3

Problem:

L'Hopital's rule is a method for determining the value of indeterminate forms. Determine the value of the following limits
$ \lim\limits_{x \to 0} \frac{e^x - 1 - x}{x^2} $

Solution:

$ \begin{eqnarray*} & & \lim\limits_{x \to 0} \frac{e^x - 1 - x}{x^2} \\ &=& \lim\limits_{x \to 0} \frac{e^x - 1}{2x} \\ &=& \lim\limits_{x \to 0} \frac{e^x}{2} \\ &=& \frac{1}{2} \\ \end{eqnarray*} $

Again, this is easy to validate numerically:

x
f(x)
$ e^2 $
0.1
0.517091808
0.000292
0.01
0.501670842
2.79E-06
0.001
0.500166708
2.78E-08
0.0001
0.500016671
2.78E-10
0.00001
0.500000696
4.85E-13

Scientific Computing - Quiz 1 - Question 2

Problem:

L'Hopital's rule is a method for determining the value of indeterminate forms. Determine the value of the following limits:

$ \lim\limits_{x \to 0} \frac{2\sin x - \sin 2x}{x-\sin x} $

Solution:

$ \begin{eqnarray*} & & \lim\limits_{x \to 0} \frac{2\sin x - \sin 2x}{x - \sin x} \\ &=& \lim\limits_{x \to 0} \frac{2\cos x - 2 \cos 2x}{1 -\cos x} \\ &=& \lim\limits_{x \to 0} \frac{-2\sin x + 4 \sin 2x}{\sin x} \\ &=& \lim\limits_{x \to 0} \frac{-2\sin x + 8 \sin x \cos x}{\sin x} \\ &=& \lim\limits_{x \to 0} -2 + 8 \cos x \\ &=& 6 \end{eqnarray*} $

One can easily verify this numerically:

x
f(x)
$e^2$
0.1
5.988008283
0.000144
0.01
5.999880001
1.44E-08
0.001
5.9999988
1.44E-12
0.0001
6.000000163
2.64E-14
0.00001
5.999979671
4.13E-10

Friday, November 20, 2015

Scientific Computing - Quiz 1 - Question 1

Problem:

L'Hopital's rule is a method for determining the value of indeterminate forms. Determine the value of the following limits

$ \lim\limits_{x \to 0}{\frac{\sin x}{x}} = 1 $

Solution:

It is really funny that the question gives out the answer - the problem already give us the limit!

Alright, let's derive it anyway, if we assume the derivative of sine is cosine, then that is easy. We have:

$ \begin{eqnarray*} & & \lim\limits_{x \to 0}{\frac{\sin x}{x}} \\ &=& \lim\limits_{x \to 0}{\frac{\cos x}{1}} \\ &=& 1 \end{eqnarray*} $

Note that we carefully said that we assume the derivative of sine is cosine above. It is a fact! Why do we bother to carefully assume it? The problem with the above is that it is a logically wrong proof! We actually need this limit to prove the derivative of sine and cosine, it is a circular reasoning.

The correct way of proving the identity should be found in any high school text book. This is based on the inequality $ \theta \ge \sin\theta \ge \theta\cos\theta $. The inequality can be argued geometrically, and then we divide through $ \theta $ and apply squeezing principle to get to the limit we wanted.

Algorithms: Design and Analysis, Part 1 - Programming Question 6 - Part 2

Problem:

Keep track of the median when data arrives 1 by 1, compute the sum of all medians modulo 10000 for each element.

Solution:

I loved this idea in the lecture, we maintain two heaps. The low heap contains the smaller half of the integers and we enable extract max there. The high heap contains the bigger half of the integers and we enable extract min there. We can use constant number of heap operation to maintain the invariant and therefore the per data item cost is $ O(\log n) $.


Algorithms: Design and Analysis, Part 1 - Programming Question 6 - Part 1

Problem:

Find the number of integers in [-10,000, 10,000] such that it is representable as a sum of two distinct integers in a list of a million integers.

Solution:

The original solution is to use hash table to look up, but that is too slow for this problem, people reported that will take hours. The distribution of the number has a very wide range and therefore sorting helped a lot, in particular, we will keep two pointers, left and right

If data[left] + data[right] > 10,000, there is no way there are any solution starting with data[right], so we decrement right by 1.

Similarly, if data[left] + data[right] < -10,000, we can increment left by 1.

Otherwise we found a match, but then what - we cannot discard left/right.

Now just move on a try if data[start] can match anything on the right by scanning right inward without moving the actual right pointer yet. Similarly we do the same for data[end].

With the above exercise, we should be exhausted everything that can start with start or end, so we are done with them, we decrement right by 1 and increment left by 1.

Here is the code of that!