Instructor: George Labahn

Week 1. May 10

prereq:

mt (june 24), final (aug 13), 5 assns: 25%, 35%, 40%

Topic 1: floating point number systems

pitfalls of computation

eg. compute e5.5e^{-5.5} using 5-digit arithmetic

recall:

ex=1+x1!+x22!+x33!+...e^x=1+\frac{x}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+...

so

e5.5=1+5.51!+(5.5)22!+(5.5)33!+...15.5+15.12527.730+...=0.0026363\begin{aligned} e^{-5.5} &= 1+\frac{-5.5}{1!}+\frac{(-5.5)^2}{2!}+\frac{(-5.5)^3}{3!}+... \\ &\approx 1-5.5+15.125-27.730+...=0.0026363 \end{aligned}

e5.5=1e5.5=(1+5.51!+5.522!+5.533!+...)1(1+5.5+15.125+27.730)1=0.0040865\begin{aligned} e^{-5.5} &= \frac{1}{e^{5.5}}=(1+\frac{5.5}{1!}+\frac{5.5^2}{2!}+\frac{5.5^3}{3!}+...)^{-1} \\ &\approx (1+5.5+15.125+27.730)^{-1}=0.0040865 \end{aligned}

why?

eg. let In=01xnx+αdxI_n=\int_0^1\frac{x^n}{x+\alpha}dx

note

In=01xn+xn1αxn1αx+αdx=01xn1(x+α)x+αdx01αxn1x+αdx=01xn1dxα01xn1x+αdx=1nαIn1\begin{aligned} I_n&=\int_0^1\frac{x^n+x^{n-1}\alpha-x^{n-1}\alpha}{x+\alpha}dx \\ &=\int_0^1\frac{x^{n-1}(x+\alpha)}{x+\alpha}dx-\int_0^1\frac{\alpha x^{n-1}}{x+\alpha}dx \\ &=\int_0^1x^{n-1}dx-\alpha\int_0^1\frac{x^{n-1}}{x+\alpha}dx \\ &=\frac{1}{n}-\alpha I_{n-1} \end{aligned}

where I0=log(1+αα)I_0=\log(\frac{1+\alpha}{\alpha}).

import numpy as np def In(alpha, n): I = np.log((1 + alpha) / alpha) for n in np.arange(1, n+1): I = 1./n - alpha*I return I In(0.5, 100) # 0.006644371123395131 In(2, 100) # 6058010443603.891 weird

Strange since In=01xnx+αdx01xn1dx=1n+1I_n=\int_0^1\frac{x^n}{x+\alpha}dx\le\int_0^1\frac{x^n}{1}dx=\frac{1}{n+1} if 0x10\le x\le 1 and α>1\alpha>1. and we should have I1001101I_{100}\le\frac{1}{101}.

if InExact=InApprox+ErrnI_n^{\mathrm{Exact}}=I_n^\mathrm{Approx}+\mathrm{Err}_n then,

Err=InEInA=(1nαIn1E)(1nαIn1A)=α(In1EIn1A)=αErrn1Errn=αnErr0\begin{aligned} \mathrm{Err}&=I_n^E-I_n^A\\ &=\left(\frac{1}{n}-\alpha I_{n-1}^E\right)-\left(\frac{1}{n}-\alpha I_{n-1}^A\right) \\ &=-\alpha(I_{n-1}^E-I_{n-1}^A) \\ &=-\alpha\mathrm{Err}_{n-1} \\ |\mathrm{Err}_n|&=|\alpha|^n|\mathrm{Err}_0| \end{aligned}

float point numbers

see. CS251

t: significant digits, L: lower bound, U: upper bound

single precision F(b=2, t=24, L=-126, U=127):

double precision F(b=2, t=53, L=-1022, U=1023):

note implicit leading 1 in precision is used.

special number:

defn. if x=±0.x1...xtxt+1...βpx=\plusmn0.x_1...x_tx_{t+1}...*\beta^p, then fl(x)=±0.x1...xtβp\mathrm{fl}(x)=\plusmn0.x_1...x_t*\beta^p, ie we use truncation. relative error is

δx=fl(x)xx\delta_x=\frac{\mathrm{fl}(x)-x}{x} \\

note fl(x)=(1+δ)x\mathrm{fl}(x)=(1+\delta)x.

we have

δx=0.0...0xt+1...x1.x2...xtxt+1...βδx0.xt+1...βt1ββ1t=E\begin{aligned} \delta_x&=\frac{-0.0...0x_{t+1}...}{x_1.x_2...x_tx_{t+1}...}\beta\\ |\delta_x|&\le\frac{0.x_{t+1}...*\beta^{-t}}{1}\beta\\ &\le\beta^{1-t} = E \end{aligned}

this quantity is called machine epsilon E.

it also often defined as the smallest positive number E such that fl(1.0+E)>1.0\mathrm{fl}(1.0+E)>1.0, and also often called unit round-off error.

if we use rounding we could get E=12β1tE=\frac{1}{2}\beta^{1-t}.

arithmetics

to do arithmetics, we do

eg. suppose x and y are real numbers, then error of addition:

xy=fl(fl(x)+fl(y))=(fl(x)+fl(y))(1+δz)\begin{aligned} x\oplus y&=\mathrm{fl}(\mathrm{fl}(x)+\mathrm{fl}(y)) \\ &=(\mathrm{fl}(x)+\mathrm{fl}(y))(1+\delta_z) \end{aligned}

x+yxyx+yx+y(x(1+δx)+y(1+δy))(1+δz)x+y=xδx+yδy+xδz+yδz+xδxδz+yδyδzx+yxδx+yδy+xδz+yδz+xδxδz+yδyδzx+y=(x+y)x+y(2E+E2)\begin{aligned} \left|\frac{x+y-x\oplus y}{x+y}\right|&\le \frac{\left|x+y-(x(1+\delta_x)+y(1+\delta_y))(1+\delta_z)\right|}{|x+y|} \\ &=\frac{|x\delta_x+y\delta_y+x\delta_z+y\delta_z+x\delta_x\delta_z+y\delta_y\delta_z|}{|x+y|}\\ &\le \frac{|x||\delta_x|+|y||\delta_y|+|x||\delta_z|+|y||\delta_z|+|x||\delta_x||\delta_z|+|y||\delta_y||\delta_z|}{|x+y|}\\ &=\frac{(|x|+|y|)}{|x+y|}(2E+E^2) \end{aligned}

Week 2. May 17

Polynomial interpolation

interpolation: given n points (x's distinct), find a nice function that go through the points.

vandermonde system

with n points (x1,y1),...,(xn,yn)(x_1,y_1),...,(x_n,y_n) and distinct xix_i's and a polynomial p(x) of degree at most n-1 satisfying p(x1)=y1,...,p(xn)=ynp(x_1)=y_1,...,p(x_n)=y_n results in n equations with n unknowns. In matrix form this is

[1x1x12...x1n11x2x22...x2n1......1xnxn2...xnn1][c1c2...cn]=[y1y2...yn]\begin{bmatrix} 1& x_1& x_1^2& ...& x_1^{n-1}\\ 1& x_2& x_2^2& ...& x_2^{n-1}\\ ...& & & & ...\\ 1& x_n& x_n^2& ...& x_n^{n-1} \end{bmatrix} \begin{bmatrix} c_1\\c_2\\...\\c_n \end{bmatrix}= \begin{bmatrix} y_1\\y_2\\...\\y_n \end{bmatrix}

the matrix of coefficients is called vandermande matrix V.

we have V(x1,...,xn)=i<j(xixj)|V(x_1,...,x_n)|=\prod_{i<j}(x_i-x_j). when x's are distinct then V is nonsigular. the system has a unique solution that always exists.

eg. have four points (1, 1), (-1, 7), (2, 1), (3, -5), find p(x)=c1+c2x+c3x2+c4x3p(x)=c_1+c_2x+c_3x^2+c_4x^3.
substitute the points note we have

p(1)=c1+c2+c3+c4=1p(1)=c1c2+c3c4=7p(2)=c1+2c2+4c3+8c4=1p(3)=c1+3c2+9c3+27c4=5\begin{aligned} p(1)=\quad&c_1&+c_2&+c_3&+c_4&=1\\ p(-1)=\quad&c_1&-c_2&+c_3&-c_4&=7\\ p(2)=\quad&c_1&+2c_2&+4c_3&+8c_4&=1\\ p(3)=\quad&c_1&+3c_2&+9c_3&+27c_4&=-5 \end{aligned}

use gaussian elimination to get p(x)=12x+3x2x3p(x)=1-2x+3x^2-x^3.

lagrange interpolation

lagrange form: given distinct points (x1,y1),...,(xn,yn)(x_1,y_1),...,(x_n,y_n) and distinct xix_i's define

Li(x)=(xx1)...(xxi1)(xxi+1)...(xxn)(xix1)...(xixi1)(xixi+1)...(xixn)=j[n]{i}xxjxixjL_i(x)=\frac{(x-x_1)...(x-x_{i-1})(x-x_{i+1})...(x-x_n)}{(x_i-x_1)...(x_i-x_{i-1})(x_i-x_{i+1})...(x_i-x_n)}=\prod_{j\in[n]-\{i\}}\frac{x-x_j}{x_i-x_j}

then the interpolating polynomial is p(x)=y1L1(x)+...+ynLn(x)p(x)=y_1L_1(x)+...+y_nL_n(x).

proof.

cubic hermite interpolation

given 2 points (x1,y1),(x2,y2)(x_1,y_1), (x_2,y_2) and two derivative values s1,s2s_1,s_2. find a polynomial S(x)S(x) of degree at most 3 with

S(x1)=y1,S(x2)=y2S(x1)=s1,S(x2)=s2S(x_1)=y_1,S(x_2)=y_2\\ S'(x_1)=s_1,S'(x_2)=s_2

then the polynomial is

S(x)=a+b(xx1)+c(xx1)2+d(xx1)3S(x)=a+b(x-x_1)+c(x-x_1)^2+d(x-x_1)^3

which is 4 eqs with 4 unknowns. the solution is

a=y1b=s1c=3y2s1s2Δxd=s1+s22yΔx2\begin{aligned} a&=y_1\\ b&=s_1\\ c&=\frac{3y'-2s_1-s_2}{\Delta x}\\ d&=\frac{s_1+s_2-2y'}{\Delta x^2} \end{aligned}

where Δx=x2x1,Δy=y2y1,y=ΔyΔx\Delta x=x_2-x_1,\Delta y=y_2-y_1,y'=\frac{\Delta y}{\Delta x}.

Week 3. May 25

issues with poly interpolation

eg.
img

Spline interpolation

piecewise polynomial interpolation: given n points (x1,y1),...,(xn,yn)(x_1,y_1),...,(x_n,y_n) with xix_i's distinct. we want to find a piecewise polynomial S(x) s.t.

S(x)={S1(x),x1xx2...Sn1(x),xn1xxnS(x)=\left\{\begin{matrix} S_1(x)&,x_1\le x\le x_2 \\ ...\\ S_{n-1}(x)&,x_{n-1}\le x \le x_n \end{matrix}\right.

with each Si(x)S_i(x) a polynomial (usually same degrees) and S(x)=yiS(x)=y_i.

linear piecewise polynomial: given n such points, each Si(x)S_i(x) is line joining (xi,yi)(x_i,y_i) to (xi+1,yy+1)(x_{i+1},y_{y+1}) (a poly with degree <= 1).

img

cubic spline interpolation

given n points (x1,y1),...,(xn,yn)(x_1,y_1),...,(x_n,y_n) with xix_i's distinct. we want to find a piecewise polynomial S(x) s.t.

S(x)={S1(x),x1xx2...Sn1(x),xn1xxnS(x)=\left\{\begin{matrix} S_1(x)&,x_1\le x\le x_2 \\ ...\\ S_{n-1}(x)&,x_{n-1}\le x \le x_n \end{matrix}\right.

with each Si(x)S_i(x) a polynomial of degree at most 3 satisfying

remark. cubic spline interpolation is not unique.

we can use boundary conditions to let the result be unique.

defn. (natural): S(x1)=S(xn)=0S''(x_1)=S''(x_n)=0.

defn. (clamped): S(x1)=s1S'(x_1)=s_1 and S(xn)=snS'(x_n)=s_n where s1,sns_1,s_n are given in advance.

defn. (periodic): S(x1)=S(xn)S'(x_1)=S'(x_n) and S(x1)=S(xn)S''(x_1)=S''(x_n) (assuming y1=yny_1=y_n).

defn. (not-a-knot): S1(x2)=S2(x2)S_1'''(x_2)=S_2'''(x_2) and Sn1(xn1)=Sn(xn1)S_{n-1}'''(x_{n-1})=S_n'''(x_{n-1}).

eg. by using cubic hermite for each interval [xi,xi+1][x_i,x_{i+1}], we have

Si(x)=ai+bi(xxi)+ci(xxi)2+di(xxi)3S_i(x)=a_i+b_i(x-x_i)+c_i(x-x_i)^2+d_i(x-x_i)^3

and Si(xi)=yi,Si(xi+1)=yi+1,Si(xi)=si,Si(xi+1)=si+1S_i(x_i)=y_i,S_i(x_{i+1})=y_{i+1},S_i'(x_i)=s_i,S_i'(x_{i+1})=s_{i+1}, where si,si+1s_i,s_{i+1} are unknowns. from here we know SS interpolates the points and the derivative is continuous. to let second derivative be continuous, we also have Si(xi+1)=Si+1(xi+1)S_i''(x_{i+1})=S_{i+1}''(x_{i+1}) for i=1,...,n2i=1,...,n-2. note

Si(x)=2ci+6di(xxi),Si+1(x)=2ci+1+6di+1(xxi+1)S_i''(x)=2c_i+6d_i(x-x_i),\quad S_{i+1}''(x)=2c_{i+1}+6d_{i+1}(x-x_{i+1})

plugging in x=xi+1x=x_{i+1} we get (delta_i is from i to i+1)

ci+3diΔxi=ci+13yi2sisi+1Δxi+3si+si+12yiΔxi2Δxi=3yi+12si+1si+2Δxi+1Δxi+1si+2(Δxi+Δxi+1)si+1+Δxisi+2=3(Δxi+1yi+Δxiyi+1)c_i+3d_i\Delta x_i=c_{i+1}\\ \frac{3 y_{i}^{\prime}-2 s_{i}-s_{i+1}}{\Delta x_{i}}+3 \frac{s_{i}+s_{i+1}-2 y_{i}'}{\Delta x_{i}^{2}}\Delta x_i=\frac{3 y_{i+1}-2 s_{i+1}-s_{i+2}}{\Delta x_{i+1}}\\ \Delta x_{i+1} s_{i}+2\left(\Delta x_{i}+\Delta x_{i+1}\right) s_{i+1}+\Delta x_{i} s_{i+2}=3\left(\Delta x_{i+1} y_{i}^{\prime}+\Delta x_{i} y_{i+1}^{\prime}\right)

which are n-2 equations for n unknowns.

suppose we want natural boundary conditions: S(x1)=S(xn)=0S''(x_1)=S''(x_n)=0.

this is the same as S1(x1)=Sn1(xn)=0S_1''(x_1)=S_{n-1}''(x_n)=0. combine the equations for {S1,c1,d1}\{S_1'',c_1,d_1\} and {Sn1,cn1,dn1}\{S_{n-1}'',c_{n-1},d_{n-1}\} we have additional two eqs.

3y1=2s1+s2,3yn1=2sn+sn13y_1'=2s_1+s_2,\quad 3y_{n-1}'=2s_n+s_{n-1}

note the coefficient matrix looks like diagonal and each row has 2 or 3 non-zero values, and it takes O(n) to determine s1,...,sns_1,...,s_n.

parametric curves

given n points (x1,y1),...,(xn,yn)(x_1,y_1),...,(x_n,y_n) with xix_i's not distinct. we look for a curve C(t)=(x(t),y(t)),t1ttnC(t)=(x(t), y(t)),t_1\le t\le t_n that goes through the points. steps:

  1. get parameter values t1,...,tnt_1,...,t_n
  2. separate x and y into two distinct interpolation problems:

how to find parameter values?

  1. use t1=0,t2=1,...,tn=n1t_1=0,t_2=1,...,t_n=n-1
  2. use arc length: t1=0,ti+1=ti+(xi+1xi)2+(yi+1yi)2t_1=0,t_{i+1}=t_i+\sqrt{(x_{i+1}-x_i)^2+(y_{i+1}-y_i)^2}

to plot the curve C(t):

  1. determine a set of parameter values τ0,...,τN,N>>n\tau_0,...,\tau_{N},N>>n
  2. plug τi\tau_i into x(t),y(t)x(t),y(t) to get χi,φi\chi_i,\varphi_i's, then plot the points (χi,φi)(\chi_i,\varphi_i).

eg.
img

Week 4. June 1

ODEs

eg. (population model) suppose p(t)=rp(t),p(t0)=p0p'(t)=rp(t),p(t_0)=p_0 where rr is constant, the solution is p(t)=p0er(tt0)p(t)=p_0e^{r(t-t_0)}.

general first order ODE

defn. if dydt=f(t,y)\frac{dy}{dt}=f(t,y) or y(t)=f(t,y(t))y'(t)=f(t,y(t)) where t0ttft_0\le t\le t_f, f is a function of two variables t and y, then ff is called system dynamics function.

initial value problem (IVP): let y=[y1,...,yN]\mathbf{y}=[y_1,...,y_N] and f(t,y)=[f1,...,fN]\mathbf{f}(t,y)=[f_1,...,f_N] (function of N+1 variables), t0ttft_0\le t\le t_f given

{dydt=f(t,y)y(t0)=a\left\{\begin{aligned} \frac{d\mathbf{y}}{dt}&=\mathbf{f}(t,\mathbf{y}) \\ \mathbf{y}(t_0)&=\mathbf{a} \end{aligned}\right.

what is y\mathbf{y}?

eg. (Predator-Prey) x(t)=x(t)(aαy(t)),y(t)=y(t)(b+βx(t))x'(t)=x(t)(a-\alpha y(t)),y'(t)=y(t)(-b+\beta x(t)) where a,α,b,βa,\alpha,b,\beta are constants (the populations interact; when there are many prey, predators increase in number ... until they eat too many).

img

second order ODE

for t0ttft_0\le t\le t_f, given

{d2ydt2=f(t,y,dydt)y(t0)=u0dydt(t0)=v0\left\{\begin{aligned} \frac{d^2y}{dt^2}&=f(t,y,\frac{dy}{dt})\\ y(t_0)&=u_0\\ \frac{dy}{dt}(t_0)&=v_0 \end{aligned}\right.

what is yy? let y1=y,y2=dydty_1=y,y_2=\frac{dy}{dt}, we can rewrite it to first order ODE (vector form).

eg. pendulum:

{θ¨+cmLθ˙+gLsinθ=0θ(0)=θ0θ˙(0)=v0\left\{\begin{aligned} &\ddot\theta+\frac{c}{m L} \dot\theta+\frac{g}{L} \sin \theta=0 \\ &\theta(0)=\theta_{0} \\ &\dot\theta(0)=v_{0} \end{aligned}\right.

let y1=θ,y2=θ˙y_1=\theta,y_2=\dot\theta, then this system is equivalent to:

{y˙1=y2y˙2=cmLy2gLsin(y1)y1(0)=θ0y2(0)=v0\left\{\begin{aligned} &\dot y_1=y_2\\ &\dot y_2=-\frac{c}{mL}y_2-\frac{g}{L}\sin(y_1) \\ &y_1(0)=\theta_{0} \\ &y_2(0)=v_{0} \end{aligned}\right.

forward euler method

consider first order ODE

{y(t)=f(t,y(t))y(t0)=α\left\{\begin{aligned} y'(t)&=f(t,y(t))\\ y(t_0)&=\alpha \end{aligned}\right.

suppose at t=tk1t=t_{k-1}, we have yk1y(tk1)y_{k-1}\approx y(t_{k-1}). at t=tkt=t_k, we use yk1y_{k-1} to compute yky_k.

using geometric

img

let hk1=tktk1h_{k-1}=t_k-t_{k-1} be the time step size. by ode, we have y(tk1)=f(tk1,y(tk1))y'(t_{k-1})=f(t_{k-1},y(t_{k-1})), we have approximation y(tk1)=slope of y at tk1ykyk1hk1y'(t_{k-1})=\text{slope of y at } t_{k-1}\approx\frac{y_k-y_{k-1}}{h_{k-1}}. we assume our approximation is correct so we replace y(tk1)y(t_{k-1}) by yk1y_{k-1} so the approximation is:

ykyk1hk1=f(tk1,yk1)yk=yk1+hk1f(tk1,yk1)\begin{aligned} \frac{y_k-y_{k-1}}{h_{k-1}}&=f(t_{k-1},y_{k-1})\\ y_k&=y_{k-1}+h_{k-1}f(t_{k-1},y_{k-1}) \end{aligned}

this requires one function evaluation for each time step.

using taylor expansion

local truncation error: assume timestep is constant h and we have a method computing yky_k. then yky(tk)=O(hp+1)y_k-y(t_k)=O(h^{p+1}). the bigger p the better.

by taylor, we have y(tk)=y(tk1+hk1)=y(tk1)+hk1y(tk1)+hk122!y(ϵk)y(t_k)=y(t_{k-1}+h_{k-1})=y({t_{k-1}})+h_{k-1}y'(t_{k-1})+\frac{h^2_{k-1}}{2!}y''(\epsilon_k) where tk1<ϵk<tkt_{k-1}<\epsilon_k<t_k. substitute the ode we have y(tk)=y(tk1)+hk1f(tk1,y(tk1))+O(h2)y\left(t_{k}\right)=y\left(t_{k-1}\right)+h_{k-1} f\left(t_{k-1}, y\left(t_{k-1}\right)\right)+O(h^{2}). use the approximation y(tk1)yk1,y(tk)yky(t_{k-1})\rightarrow y_{k-1},y(t_k)\rightarrow y_k we get the result. and y(tk)yk=O(h2)y(t_k)-y_k=O(h^2).

eg. compute

{y(t)=sintcosy(t)y(0)=1\left\{\begin{aligned} y'(t)&=\sin t-\cos y(t)\\ y(0)&=1 \end{aligned}\right.

we use even interval h=1Nh=\frac{1}{N} (ie t0=0,t1=h,...,tN=1t_0=0,t_1=h,...,t_N=1).

  1. let y0=1y_0=1
  2. for k=1,2,...,Nk=1,2,...,N, do yk=yk1+h(sintk1cosyk1)y_k=y_{k-1}+h\cdot(\sin t_{k-1}-\cos y_{k-1})

eg. compute

{x(t)=2x(t)3y(t)y(t)=tx(t)3y(t)x(0)=2y(0)=1\left\{\begin{aligned} x'(t)&=2x(t)-3y(t)\\ y'(t)&=tx(t)-3y(t)\\ x(0)&=2\\ y(0)&=1 \end{aligned}\right.

where 0<t<10<t<1. use interval h=1Nh=\frac{1}{N} so tk=kht_k=kh

  1. let [x0,y0]=[2,1][x_0,y_0]=[2,1]
  2. for k=1,...,Nk=1,...,N do [xk,yk]=[xk1,yk1]+h[2xk13yk1,tk1xk13yk1][x_k,y_k]=[x_{k-1},y_{k-1}]+h\cdot[2x_{k-1}-3y_{k-1},t_{k-1}x_{k-1}-3y_{k-1}]

Week 5. June 7

trapezoid method

algo.

y0=αyk=yk1+hk12(f(tk1,yk1)+f(tk,yk))\begin{aligned} y_0&=\alpha\\ y_k&=y_{k-1}+\frac{h_{k-1}}{2}(f(t_{k-1},y_{k-1})+f(t_k,y_k)) \end{aligned}

we have to solve for yky_k at each step.

using taylor

note by taylor

y(tk)=y(tk1)+hy(tk1)+h22!y(tk1)+h33!y(ϵk)y(tk)=y(tk1)+hy(tk1)+h22!y(α)y(t_k)=y(t_{k-1})+hy'(t_{k-1})+\frac{h^2}{2!}y''(t_{k-1})+\frac{h^3}{3!}y'''(\epsilon_k)\\ y'(t_k)=y'(t_{k-1})+hy''(t_{k-1})+\frac{h^2}{2!}y'''(\alpha)

isolate hyhy'' we have hy(tk1)=y(tk)y(tk1)h22!y(α)hy''(t_{k-1})=y'(t_k)-y''(t_{k-1})-\frac{h^2}{2!}y'''(\alpha), so

y(tk)=y(tk1)+hy(tk1)+h2!(y(tk)y(tk1)h22!y(α))+h33!y(ϵk)=y(tk1)+h2!(y(tk1)+y(tk))+O(h3)=y(tk1)+h2!(f(tk1,y(tk1))+f(tk,y(tk)))+O(h3)\begin{aligned} y(t_k)&=y(t_{k-1})+hy'(t_{k-1})+\frac{h}{2!}(y'(t_k)-y'(t_{k-1})-\frac{h^2}{2!}y'''(\alpha))+\frac{h^3}{3!}y'''(\epsilon_k)\\ &=y(t_{k-1})+\frac{h}{2!}(y'(t_{k-1})+y'(t_k))+O(h^3)\\ &=y(t_{k-1})+\frac{h}{2!}(f(t_{k-1},y(t_{k-1}))+f(t_k,y(t_k)))+O(h^3) \end{aligned}

use the approximation y(tk1)yk1,y(tk)yky(t_{k-1})\rightarrow y_{k-1},y(t_k)\rightarrow y_k we get the result.

modified euler method

consider first order ODE

{y(t)=f(t,y(t))y(t0)=α\left\{\begin{aligned} y'(t)&=f(t,y(t))\\ y(t_0)&=\alpha \end{aligned}\right.

algo. approximate derivative at any point by taking average slope at starting point and approximated endpoint:

y0=αyk=yk1+hk1f(tk1,yk1)yk=yk1+hk12(f(tk1,yk1)+f(tk,yk))\begin{aligned} y_0&=\alpha\\ y_k^*&=y_{k-1}+h_{k-1}f(t_{k-1},y_{k-1})\\ y_k&=y_{k-1}+\frac{h_{k-1}}{2}(f(t_{k-1},y_{k-1})+f(t_k,y_k^*)) \end{aligned}

using taylor

as before:

y(tk)=y(tk1)+h2!(f(tk1,y(tk1))+f(tk,y(tk)))+O(h3)=y(tk1)+h2(f(tk1,y(tk1)+f(tk,yk)))+h2(f(tk,yk)+f(tk,y(tk)))+O(h3)\begin{aligned} y(t_k)&=y(t_{k-1})+\frac{h}{2!}(f(t_{k-1},y(t_{k-1}))+f(t_k,y(t_k)))+O(h^3)\\ &=y(t_{k-1})+\frac{h}{2}(f(t_{k-1},y(t_{k-1})+f(t_k,y^*_k)))+\frac{h}{2}(-f(t_k,y^*_k)+f(t_k,y(t_k)))+O(h^3) \end{aligned}

since from forward euler we have y(tk)yk=O(h2)y(t_k)-y^*_k=O(h^2), so from taylor of f(tk,y(tk))f(t_k,y(t_k)) as function of y: f(tk,y(tk))=f(tk,yk)+O(y(tk)yk)=f(tk,yk)+O(h2)f(t_k,y(t_k))=f(t_k,y^*_k)+O(y(t_k)-y^*_k)=f(t_k,y^*_k)+O(h^2). hence

y(tk)=y(tk1)+h2(f(tk1,y(tk1))+f(tk,yk))+h2O(h2)+O(h3)=y(tk1)+h2(f(tk1,y(tk1))+f(tk,yk))+O(h3)\begin{aligned} y(t_k)&=y(t_{k-1})+\frac{h}{2}(f(t_{k-1},y(t_{k-1}))+f(t_k,y^*_k))+\frac{h}{2}O(h^2)+O(h^3)\\ &=y(t_{k-1})+\frac{h}{2}(f(t_{k-1},y(t_{k-1}))+f(t_k,y^*_k))+O(h^3) \end{aligned}

local truncation error

in general

given yn=y_n= formula in yn,yn1,...y_n,y_{n-1},..., to determine the LTE

  1. replace all yky_k by the actual values y(tk)y(t_k) (assume formula is correct)
  2. compare taylor of y(tn)y(t_n) with taylor at tnt_n and at tn1t_{n-1}

eg. what is error of yk=yk1+h4f(tk2,yk2)+3h4f(tk,yk)y_k=y_{k-1}+\frac{h}{4}f(t_{k-2},y_{k-2})+\frac{3h}{4}f(t_k,y_k).

  1. replace:

    y(tk)=y(tk1)+h4f(tk2,y(tk2))+3h4f(tk,y(tk))=y(tk1)+h4y(tk2)+3h4y(tk) \begin{aligned} y(t_k)&=y\left(t_{k-1}\right)+\frac{h}{4} f\left(t_{k-2}, y\left(t_{k-2}\right)\right)+\frac{3 h}{4} f\left(t_{k}, y\left(t_{k}\right)\right) \\ &=y\left(t_{k-1}\right)+\frac{h}{4} y^{\prime}\left(t_{k-2}\right)+\frac{3 h}{4} y^{\prime}\left(t_{k}\right) \end{aligned}

  2. expand centered around tk1t_{k-1}

    y(tk)=y(tk1)+hy(tk1)+h22!y(tk1)+...y(tk2)=y(tk1)hy(tk1)+h22!y(tk1)+...y'(t_k)=y'(t_{k-1})+hy''(t_{k-1})+\frac{h^2}{2!}y'''(t_{k-1})+...\\ y'(t_{k-2})=y'(t_{k-1})-hy''(t_{k-1})+\frac{h^2}{2!}y'''(t_{k-1})+...\\

  3. plug in and see where there is a difference between our result and expansion of formula

    y(tk)=y(tk1)+hy(tk1)+h22y(tk1)+h32y(tk1)+...y(tk)=y(tk1)+hy(tk1)+h22y(tk1)+h36y(tk1)+...(actual formula)y\left(t_{k}\right)=y\left(t_{k-1}\right)+hy^{\prime}\left(t_{k-1}\right)+\frac{h^{2}}{2}y^{\prime \prime}\left(t_{k-1}\right)+\frac{h^{3}}{2}y^{\prime \prime \prime}\left(t_{k-1}\right)+...\\ y\left(t_{k}\right)=y\left(t_{k-1}\right)+hy^{\prime}\left(t_{k-1}\right)+\frac{h^{2}}{2}y^{\prime \prime}\left(t_{k-1}\right)+\frac{h^{3}}{6}y^{\prime \prime \prime}\left(t_{k-1}\right)+...\text{(actual formula)}

it is O(h^3).

timestamp control

how to determine if numerical method likely gives a correct answer?

we can combine forward euler and modified euler:

ykFE=y(tk)+Ah2+O(h3)ykME=y(tk)+O(h3)y_k^\mathrm{FE}=y(t_k)+Ah^2+O(h^3)\\ y_k^\mathrm{ME}=y(t_k)+O(h^3)

so we have

local errorAh2=ykFEykME\text{local error}\approx|Ah^2|=|y_k^\mathrm{FE}-y_k^\mathrm{ME}|

in general, at every timestamp consider two rules, one for order p, one for order p+1, the local error is the difference between two methods: |local error|=Ahp=yk(1)yk(2)|Ah^p|=|y_k^{(1)}-y_k^{(2)}|.

if error is too big then reduce timestep h by 2 and repeat.

eg. a common method: runge kutta methods (RKF45)

k1=hf(tn,yn)k2=hf(tn+h4,yn+k14)k3=hf(tn+3h8,yn+3k132+9k232)k4=hf(tn+12h13,yn+1932k121977200k22197+7296k32197)k5=hf(tn+h,yn+439k12168k2+3680k3513845k44104)k6=hf(tn+h2,yn8k127+2k23544k32565+1859k4410411k540)yn+1=yn+25k1216+1408k32565+2197k44104k55 with error O(h4)yn+1=yn+16k1135+6656k312825+28561k4564309k550+2k655 with error O(h5)\begin{aligned} &k_{1}=h f\left(t_{n}, y_{n}\right) \\ &k_{2}=h f\left(t_{n}+\frac{h}{4}, y_{n}+\frac{k_{1}}{4}\right) \\ &k_{3}=h f\left(t_{n}+\frac{3 h}{8}, y_{n}+\frac{3 k_{1}}{32}+\frac{9 k_{2}}{32}\right) \\ &k_{4}=h f\left(t_{n}+\frac{12 h}{13}, y_{n}+\frac{1932 k_{1}}{2197}-\frac{7200 k_{2}}{2197}+\frac{7296 k_{3}}{2197}\right) \\ &k_{5}=h f\left(t_{n}+h, y_{n}+\frac{439 k_{1}}{216}-8 k_{2}+\frac{3680 k_{3}}{513}-\frac{845 k_{4}}{4104}\right) \\ &k_{6}=h f\left(t_{n}+\frac{h}{2}, y_{n}-\frac{8 k_{1}}{27}+2 k_{2}-\frac{3544 k_{3}}{2565}+\frac{1859 k_{4}}{4104}-\frac{11 k_{5}}{40}\right) \\ &y_{n+1}^{*}=y_{n}+\frac{25 k_{1}}{216}+\frac{1408 k_{3}}{2565}+\frac{2197 k_{4}}{4104}-\frac{k_{5}}{5} \text { with error } O\left(h^{4}\right) \\ &y_{n+1}=y_{n}+\frac{16 k_{1}}{135}+\frac{6656 k_{3}}{12825}+\frac{28561 k_{4}}{56430}-\frac{9 k_{5}}{50}+\frac{2 k_{6}}{55} \text { with error } O\left(h^{5}\right) \end{aligned}

timestamp control (2)

instead of dividing by 2, we can also do:

suppose we require Ahp=yk(1)yk(2)|Ah^p|=|y_k^{(1)}-y_k^{(2)}| < TOL, at next step we can change the timestep to some h2h_2:

yk+1(1)=y(tk+1)+Ah2p+O(h2p+1)yk+1(2)=y(tk+1)+O(h2p+1)y_{k+1}^{(1)}=y(t_{k+1})+Ah_2^p+O(h_2^{p+1})\\ y_{k+1}^{(2)}=y(t_{k+1})+O(h_2^{p+1})

now again we want local error = Ah2p=h2phpyk(1)yk(2)|Ah_2^p|=\frac{h_2^p}{h^p}|y_k^{(1)}-y_k^{(2)}| < TOL. this implies we can use h2<(TOLyk(1)yk(2)h)1ph_2<\left(\frac{\mathrm{TOL}}{|y_k^{(1)}-y_k^{(2)}|}h\right)^\frac{1}{p}.

stability

eg. the system dynamic function is y(t)=λy(t)y'(t)=-\lambda y(t) and y(t0)=y0y(t_0)=y_0. if we use forward euler to solve it, we have

yk=yk1+hf(tk1,yk1)=yk1+h(λyk1)=yk1(1hλ)=...=y0(1hλ)k\begin{aligned} y_k&=y_{k-1}+hf(t_{k-1},y_{k-1})\\ &=y_{k-1}+h\cdot(-\lambda y_{k-1})\\ &=y_{k-1}(1-h\lambda)\\ &=...\\ &=y_0(1-h\lambda)^k \end{aligned}

true solution is y(t)=y0eλty(t)=y_0e^{-\lambda t} (goes to 0 as t -> inf). so method is stable when 1hλ<1|1-h\lambda|<1, or h<2λh<\frac{2}{\lambda}.

event function

defn. event function is a function which changes sign.

eg. predator catches prey: Event(predator, prey) = |dist(predator, prey)| - ϵ.

eg. we shoot a golf ball with some initial speed. in the middle of the field there is a wall that goes up and down (h = Acos(wt+y)). whenever ball hits the wall, it stops; whenever it hits the group, it also stops. Event(ball, barrier) = |dist(top of barrier to ball)| - ϵ and = |height of ball| - ϵ. link

Week 6. June 15

fourier analysis

reviews of algebra:

defn. fourier series of f(t) is f(t)=a0+a1cos(qt)+b1sin(qt)+a2cos(2qt)+b2sin(2qt)+...=k0(akcos(kqt)+bksin(kqt))f(t)=a_0+a_1\cos(qt)+b_1\sin(qt)+a_2\cos(2qt)+b_2\sin(2qt)+...=\sum_{k\ge 0}(a_k\cos(kqt)+b_k\sin(kqt)) where q (=2πT\frac{2\pi}{T}) is a constant factor.

defn. we can write f(t)=k=ckejktf(t)=\sum_{k=-\infty}^\infty c_ke^{jkt}, where c0=a0,ck=ak2bk2j,ck=ak2+bk2jc_0=a_0,c_k=\frac{a_k}{2}-\frac{b_k}{2}j,c_{-k}=\frac{a_k}{2}+\frac{b_k}{2}j for k > 0.

defn. given N points f0,...,fN1f_0,...,f_{N-1}, discrete fourier transform is the N values F0,...,FN1F_0,...,F_{N-1} where Fk=1N(f0+Wkf1+W2kf2+...+W(N1)kfN1)=1Nn=0N1WnkfnF_k=\frac{1}{N}(f_0+W^{-k}f_1+W^{-2k}f_2+...+W^{-(N-1)k}f_{N-1})=\frac{1}{N}\sum_{n=0}^{N-1}W^{-nk}f_n, and W=e2πj/NW=e^{2\pi j/N}.

proof of inverse dft: the dft as matrix operation:

1N[11111W1W2W(N1)1W(N1)W2(N1)W(N1)(N1)][f0f1fN1]=[F0F1FN1]\frac{1}{N}\left[\begin{array}{ccccc} 1 & 1 & 1 & \cdots & 1 \\ 1 & W^{-1} & W^{-2} & \cdots & W^{-(N-1)} \\ \vdots & \vdots & \vdots & \cdots & \vdots \\ 1 & W^{-(N-1)} & W^{-2(N-1)} & \cdots & W^{-(N-1)(N-1)} \end{array}\right]\begin{bmatrix} f_{0} \\ f_{1} \\ \vdots \\ f_{N-1} \end{bmatrix}=\begin{bmatrix} F_{0} \\ F_{1} \\ \vdots \\ F_{N-1} \end{bmatrix}

the inverse dft as matrix operation:

[11111WW2W(N1)1WN1W2(N1)W(N1)(N1)][F0F1FN1]=[f0f1fN1]\left[\begin{array}{ccccc} 1 & 1 & 1 & \cdots & 1 \\ 1 & W & W^{2} & \cdots & W^{(N-1)} \\ \vdots & \vdots & \vdots & \cdots & \vdots \\ 1 & W^{N-1} & W^{2(N-1)} & \cdots & W^{(N-1)(N-1)} \end{array}\right]\begin{bmatrix} F_{0} \\ F_{1} \\ \vdots \\ F_{N-1} \end{bmatrix}=\begin{bmatrix} f_{0} \\ f_{1} \\ \vdots \\ f_{N-1} \end{bmatrix}

we want to show the two operations are inverses. their product should have 1 entries on diagonal and 0 otherwise, use the algebra review above. \square

eg. suppose all N points are 1, then:

eg. suppose fk=2kf_k=2^k, then Fk=1Nn=0N1(2Wk)n=1N(1(2Wk)N12Wk)=1N(12N12Wk)F_k=\frac{1}{N}\sum_{n=0}^{N-1}(2W^{-k})^n=\frac{1}{N}(\frac{1-(2W^{-k})^N}{1-2W^{-k}})=\frac{1}{N}(\frac{1-2^N}{1-2W^{-k}}) (note denominator cannot be 0 since Wk12W^{-k}\neq\frac{1}{2}).

Week 7. June 21

meaning of dft

given N equally spaced samples of a function f(t) over 0 to 2π2\pi, this implies we should keep only N coefficients from the fourier series of f(t):

cN/2+1,...c1,c0,c1,...,cN/2c_{-N/2+1},...c_{-1},c_0,c_1,...,c_{N/2}

where cn=cn=an2+bn22|c_{-n}|=|c_n|=\frac{\sqrt{a_n^2+b_n^2}}{2} is the information about f(t) of frequency k. note c0,cN/2|c_0|,|c_{N/2}| are special. we can approximate the fourier series (which is infinite sum) using these N numbers (we lose some info):

f(t)k=N2+1N2ckejktf(t)\approx\sum_{k=-\frac{N}{2}+1}^{\frac{N}{2}}c_ke^{jkt}

we have fk=f(k2πN),0k<Nf_k=f(k\frac{2\pi}{N}),0\le k<N and so for each sample fkf_k we have fk=f(tk)=n=N/2+1N/2cnejntkf_k=f(t_k)=\sum_{n=-N/2+1}^{N/2}c_ne^{jnt_k} where tk=2πNkt_k=\frac{2\pi}{N}k. let W=e2πj/NW=e^{2\pi j/N}, after some algebra

fk=n=0N2cnWnk+n=N2+11cnWnk=n=0N2cnWnk+n=N2+11cnW(N+n)k(unity)=n=0N2cnWnk+n=N2+1NcnWnk\begin{aligned} f_k&=\sum_{n=0}^{\frac{N}{2}}c_nW^{nk}+\sum_{n=-\frac{N}{2}+1}^{-1}c_nW^{nk}\\ &=\sum_{n=0}^{\frac{N}{2}}c_nW^{nk}+\sum_{n=-\frac{N}{2}+1}^{-1}c_nW^{(N+n)k}\quad\text{(unity)}\\ &=\sum_{n=0}^{\frac{N}{2}}c_nW^{nk}+\sum_{n=\frac{N}{2}+1}^Nc_{-n}W^{nk} \end{aligned}

we have

Fn=FNn=cn=cn|F_n|=|F_{N-n}|=|c_{-n}|=|c_n|

the fkf_k is then the inverse dft:

fk=n=0N1WnkFnFk=n=0N1Wnkfnf_k=\sum_{n=0}^{N-1}W^{nk}F_n\\F_k=\sum_{n=0}^{N-1}W^{-nk}f_n

note

Week 8. June 28

fast fourier transform

normally computing F0,...,FN1F_0,...,F_{N-1} we require O(n^2) operations. now assume N is even, and further more is a power of 2. we can compute them in O(NlogN) steps.

observation. if W is an Nth root of unity, then W2W^2 is an N2\frac{N}{2} root of unity.

observation. if W is an Nth root of unity, then WN/2=1W^{N/2}=-1. hence we also have WkN/2=(1)kW^{kN/2}=(-1)^k.

we separate the computation into k=even terms and k=odd terms.

NFk=n=0N1fnWnk=n=0N/21fnWnk+n=N/2N1fnWnk=n=0N/21fnWnk+(W(N/2)k)n=N/2N1fnW(nN/2)k=n=0N/21fnWnk+(1)kn=N/2N1fnW(nN/2)k\begin{aligned} NF_k&=\sum_{n=0}^{N-1}f_nW^{-nk}\\ &=\sum_{n=0}^{N/2-1}f_nW^{-nk}+\sum_{n=N/2}^{N-1}f_nW^{-nk}\\ &=\sum_{n=0}^{N/2-1}f_nW^{-nk}+(W^{-(N/2)k})\sum_{n=N/2}^{N-1}f_nW^{-(n-N/2)k}\\ &=\sum_{n=0}^{N/2-1}f_nW^{-nk}+(-1)^k\sum_{n=N/2}^{N-1}f_nW^{-(n-N/2)k}\\ \end{aligned}

when k=2k is even, (1)k=1(-1)^k=1, otherwise k=2k+1 (1)k=1(-1)^k=-1, let U=W2U=W^2, so

N2F2k=n=0N/21fnW2nk+n=N/2N1fnW(nN/2)2k=n=0N/21fnUnk+n=N/2N1fnU(nN/2)k=n=0N/21(fn+fN/2+n)U(nN/2)k\begin{aligned} \frac{N}{2}F_{2k}&=\sum_{n=0}^{N/2-1}f_nW^{-2nk}+\sum_{n=N/2}^{N-1}f_nW^{-(n-N/2)2k}\\ &=\sum_{n=0}^{N/2-1}f_nU^{-nk}+\sum_{n=N/2}^{N-1}f_nU^{-(n-N/2)k}\\ &=\sum_{n=0}^{N/2-1}(f_n+f_{N/2+n})U^{-(n-N/2)k} \end{aligned}

N2F2k+1=n=0N/21fnWn(2k+1)n=N/2N1fnW(nN/2)(2k+1)=n=0N/21fnWnUnkn=N/2N1fnWnU(nN/2)k=n=0N/21(fnfN/2+n)WnU(nN/2)k\begin{aligned} \frac{N}{2}F_{2k+1}&=\sum_{n=0}^{N/2-1}f_nW^{-n(2k+1)}-\sum_{n=N/2}^{N-1}f_nW^{-(n-N/2)(2k+1)}\\ &=\sum_{n=0}^{N/2-1}f_nW^{-n}U^{-nk}-\sum_{n=N/2}^{N-1}f_nW^{-n}U^{-(n-N/2)k}\\ &=\sum_{n=0}^{N/2-1}(f_n-f_{N/2+n})W^{-n}U^{-(n-N/2)k} \end{aligned}

the fourier transform can be divided into even and odd terms' fourier transforms.

algo.

define two sequences of length N/2:

gk=fk+fN/2+k2hk=fkfN/2+k2Wkg_k=\frac{f_k+f_{N/2+k}}{2}\\ h_k=\frac{f_k-f_{N/2+k}}{2}W^{-k}

  1. compute fourier transform of g0,...,gN/21g_0,...,g_{N/2-1} to get G0,...,GN/21G_0,...,G_{N/2-1}
  2. compute fourier transform of h0,...,hN/21h_0,...,h_{N/2-1} to get H0,...,HN/21H_0,...,H_{N/2-1}
  3. then F2k=Gk,F2k+1=HkF_{2k}=G_k,F_{2k+1}=H_k

cost: C(N) = 2C(N/2) + cN => O(NlogN)

butterfly algorithm (inplace fft):

f[n] -------> f[n] + f[n+N/2] \ ^ \ / \ / \ / \ / \ / v f[n+N/2] ----> f[n+N/2] - f[n+N/2]
var N = 2 ** m var W = e ** (2*j*pi / N) for k = 1, m: for j = 1, 2**(k-1): var v = (j - 1) * N for n = 0, N / 2 - 1: var wf = W ** -n var temp = wf * (f[n] - f[n + N/2]) f[n + v] = 1/2 * (f[n] + f[n + N/2]) f[n + v + N/2] = temp / 2 N /= 2 output in reverse binary form // (for 8 nums, F[1] => F[0b001] => F[0b100] => F[4])

signal filtering

basic idea:

eg. given signal 1: sound of both trains and bird sounds. we would like to decompose it into

correlating two signals

then we want to find M and L such that

defn. given two signals y0,...,yN1y_0,...,y_{N-1} and z0,...,zN1z_0,...,z_{N-1}, the correlation function is ϕk=1Nn=0Nyn+kzn,k=0,...,N1\phi_k=\frac{1}{N}\sum_{n=0}^Ny_{n+k}z_n,k=0,...,N-1.

img

wrap-around effects:

typically computing correlation takes O(n^2). however we can convert the computation into fourier transform:

algo.

  1. compute fourier transform of y0,...,yN1y_0,...,y_{N-1} to get Y0,...,YN1Y_0,...,Y_{N-1}
  2. compute fourier transform of z0,...,zN1z_0,...,z_{N-1} to get Z0,...,ZN1Z_0,...,Z_{N-1}
  3. multiply Φk=YkZk\Phi_k=Y_k\overline{Z_k}
  4. compute inverse ft of Φ0,...,ΦN1\Phi_0,...,\Phi_{N-1} to get ϕ0,...,ϕN1\phi_0,...,\phi_{N-1}

proof.

let Φk\Phi_k be the ft of ϕk\phi_k, we have Φk=1Nn=0N1ϕnWnk=1N2n=0N1u=0N1yu+nzuWnk\Phi_k=\frac{1}{N}\sum_{n=0}^{N-1}\phi_nW^{-nk}=\frac{1}{N^2}\sum_{n=0}^{N-1}\sum_{u=0}^{N-1}y_{u+n}z_uW^{-nk}.

let yu+n=s=0N1YsWs(u+n)y_{u+n}=\sum_{s=0}^{N-1}Y_sW^{s(u+n)}, zu=v=0N1ZvWvuz_u=\sum_{v=0}^{N-1}Z_vW^{vu}, then

Φk=1N2n=0N1u=0N1s=0N1YsW(u+n)sv=0N1ZvWvuWnk=1N2S=0N1Ysv=0N1Zvn=0N1u=0N1 Wu(s+v)Wn(sk)=1N2s=0N1Ysv=0N1Zvu=0N1 Wu(s+v)n=0N1 Wn(sk)=1Ns=0N1Ysv=0N1Zvu=0N1 Wu(s+v)δs,k=1NYkv=0N1Zvu=0N1 Wu(k+v)=Ykv=0N1Zvδv,Nk=YkZNk=YkZk\begin{aligned} &\Phi_{{k}}=\frac{1}{N^{2}} \sum_{n=0}^{N-1} \sum_{u=0}^{N-1} \sum_{s=0}^{N-1} Y_{{s}} {W}^{(u+{n}) {s}} \sum_{v=0}^{N-1} Z_{{v}} {W}^{{vu}} {W}^{-{nk}} \\ &=\frac{1}{N^{2}} \sum_{S=0}^{N-1} Y_{{s}} \sum_{v=0}^{N-1} Z_{{v}} \sum_{n=0}^{N-1} \sum_{u=0}^{N-1} {~W}^{{u}({s}+{v})} {W}^{{n}({s}-{k})} \\ &=\frac{1}{N^{2}} \sum_{s=0}^{N-1} Y_{{s}} \sum_{v=0}^{N-1} Z_{{v}} \sum_{u=0}^{N-1} {~W}^{{u}({s}+{v})} \sum_{n=0}^{N-1} {~W}^{{n}({s}-{k})} \\ &=\frac{1}{N} \sum_{s=0}^{N-1} Y_{{s}} \sum_{v=0}^{N-1} Z_{{v}} \sum_{u=0}^{N-1} {~W}^{{u}({s}+{v})} \delta_{{s}, {k}} \\ &=\frac{1}{N} {Y}_{{k}} \sum_{v=0}^{N-1} Z_{{v}} \sum_{u=0}^{N-1} {~W}^{{u}({k}+{v})} \\ &={Y}_{{k}} \sum_{v=0}^{N-1} Z_{{v}} \delta_{{v}, {N}-{k}} \\ &={Y}_{{k}} {Z}_{{N}-{k}} \\ &={Y}_{{k}} \overline{{Z}_{{k}}} \end{aligned}

where δs,k\delta_{s,k} = 1, when s = k, 0 otherwise. \square

signal aliasing

defn. fourier frequency is the number of cycles per second = kT=kFSN\frac{k}{T}=\frac{kF_S}{N}, where sampling rate FS=NTF_S=\frac{N}{T} = number of discrete samples per second.

if a real input signal contains high frequencies, but the spacing of discretely sampled data points is inadequate, then aliasing occur.

img

(wagon-wheel effect; stationary rotor of helicopter)

recall for fk=f(k2πN)f_k=f(k\frac{2\pi}{N}), we have Fk=ck+ck+N+ckN+ck+2N+ck2N+...F_k=c_k+c_{k+N}+c_{k-N}+c_{k+2N}+c_{k-2N}+..., sums of true continuous fourier series coefficients ckc_k of increasing frequency. so frequencies ckc_k for k>N2|k|>\frac{N}{2} get aliased to lower frequencies. (?)

to avoid aliasing:

data compression

img

consider 1D array of greyscale.

img

a dominant pattern appears 4 times, so F4F_4 is large. we can discard data with less frequencies.

1D compression strategy:

img

image processing in 2D

consider 2D image in grey scale with a 2D array X with MN pixels. assume data is scaled so that 0X(i,j)10\le X(i,j)\le 1.

we move from time/space domain to frequency domain using 2D dft:

Fj,k=1MNn=0N1m=0M1fn,mWNnkWMmj=1Nn=0N1WNnk(1Mm=0M1fn,mWMmj)(DFT per row)\begin{aligned} F_{j,k}&=\frac{1}{MN}\sum_{n=0}^{N-1}\sum_{m=0}^{M-1}f_{n,m}W_N^{-nk}W_M^{-mj}\\ &=\frac{1}{N}\sum_{n=0}^{N-1}W_N^{-nk}\left(\frac{1}{M}\sum_{m=0}^{M-1}f_{n,m}W_M^{-mj}\right)\quad\text{(DFT per row)} \end{aligned}

where WNW_N is Nth root of unity and WMW_M is Mth root of unity.

the 2D FFT can be computed efficiently using nested 1D FFTs:

  1. transform each row (separately) using 1D fft
  2. transform each column of the result using 1D fft

complexity: O(MN(logM+logN))O(MN(\log M+\log N)).

JPEG

  1. break down image into 8x8 blocks - image does not change very much in such small block
  2. transform each block from spatial to frequency domain using 2D discrete cosine transform
  3. reduce blocks in order to reduce size of higher frequencies
  4. entropy encoding: treat DC component separate since it is usually large. save first value then determine difference for each block afterwards
  5. convert 2D array into 1D array using a zigzag pattern - compress by skipping 0 continuous values

Week 11. July 19

linear algebra

solving system of linear equations

recall:

given nonsigular matrix A and vector b\mathbf{b}, solve Ax=bA\mathbf{x}=\mathbf{b} for x\mathbf{x}.

steps:

  1. LU decomposition: factor A into A=LUA = LU where L is lower triangular and U is upper triangular
  2. solve Ly=bL\mathbf{y}=\mathbf{b}
  3. solve Ux=yU\mathbf{x}=\mathbf{y}

eg. solve Ax=b:

[123456781][x1x2x3]=[444]\begin{bmatrix} 1&2&3 \\ 4&5&6 \\ 7&8&1 \end{bmatrix}\begin{bmatrix} x_1\\x_2\\x_3 \end{bmatrix}=\begin{bmatrix} 4\\4\\-4 \end{bmatrix}

first perform factorization A=LU:

[123456781]=[100410721][123036008]\begin{bmatrix} 1&2&3 \\ 4&5&6 \\ 7&8&1 \end{bmatrix}=\begin{bmatrix} 1&0&0 \\ 4&1&0 \\ 7&2&1 \end{bmatrix}\begin{bmatrix} 1&2&3 \\ 0&-3&-6 \\ 0&0&-8 \end{bmatrix}

then solve Ly=b:

[100410721][y1y2y3]=[444]\begin{bmatrix} 1&0&0 \\ 4&1&0 \\ 7&2&1 \end{bmatrix}\begin{bmatrix} y_1 \\ y_2 \\ y_3 \end{bmatrix}=\begin{bmatrix} 4\\4\\-4 \end{bmatrix}

note we can get y1=4y_1=4 directly, then use this to compute y2y_2, then use them to compute y3y_3. we have yT=[4,12,8]\mathbf{y}^T=[4,-12,-8].

finally solve Ux=y:

[123036008][x1x2x3]=[4128]\begin{bmatrix} 1&2&3 \\ 0&-3&-6 \\ 0&0&-8 \end{bmatrix}\begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}=\begin{bmatrix} 4\\ -12\\-8 \end{bmatrix}

note we get x3=1x_3=1 directly, then use this to compute x2x_2, then use them to compute x1x_1. we have xT=[3,2,1]\mathbf{x}^T=[-3,2,1].

cost:

PLU decomposition

LU decomposition does not work when pivot is 0 => we have 0 on diagonal and we need to swap rows to preceed.

[045123323069][233204513069]\left[\begin{array}{ccc|c} 0 & 4 & 5 & 1 \\ -2 & 3 & 3 & -2 \\ 3 & 0 & 6 & 9 \end{array}\right]\Rightarrow\left[\begin{array}{ccc|c} -2 & 3 & 3 & -2 \\ 0 & 4 & 5 & 1 \\ 3 & 0 & 6 & 9 \end{array}\right]

we need to permute rows => a permutation matrix P so that PA = LU.

steps:

  1. PLU decomposition: factor A into PA=LUPA = LU where L is lower triangular and U is upper triangular
  2. solve Ly=bL\mathbf{y}=\mathbf{b'} (b': permuted version of b)
  3. solve Ux=yU\mathbf{x}=\mathbf{y}

start with P set to nxn identity matrix I. whenever we swap a pair of rows during LU factorization, also swap corresponding rows of P, the final P is the permutation matrix (in implementation, we do not store P explicitly).

what happens when pivot is nearly 0? forming aikakk\frac{a_{ik}}{a_{kk}} gives number of large magnitude, causing large float point error during subtraction and magnify existing error.

partial pivoting strategy: find the row with entry having largest magnitude in current column beneath current row. swap rows if its entry has larger magnitude than diagonal entry.

eg. consider in 4-digit arithmetic.

{105x+y=1x+y=2\left\{\begin{matrix} 10^{-5}x+y=1\\ x+y=2 \end{matrix}\right.

pivot is 10510^{-5}. multiple first row by 10510^{5} we have (1105)y=2105(1051)y=105299999y=999989999y=9999(1-10^5)y=2-10^5\Rightarrow (10^5-1)y=10^5-2\Rightarrow 99999y=99998\Rightarrow 9999y=9999 (truncation) so y=1y=1 and x=0x=0.

if we switch rows, the pivot is 1

{x+y=2105x+y=1\left\{\begin{matrix} x+y=2\\ 10^{-5}x+y=1\\ \end{matrix}\right.

and (1105)y=12105y=1,x=1(1-10^{-5})y=1-2\cdot10^{-5}\Rightarrow y=1,x=1 which is better solution.

Week 12. July 26

conditioning of matrix

we want to recognize when matrix might be ill-formed for solving. this is what would happen if we get one answer for solving Ax=b but significantly different answer when we perturb b slightly. we want to measure the matrix A.

defn. ||\cdot|| is a matrix norm if

  1. A0||A||\geq 0 and A=0    A=0||A||=0\iff A=0
  2. cA=cA||cA||=|c|||A|| for any scalar c
  3. A+BA+B||A+B||\leq||A||+||B|| (triangle inequality)
  4. ABAB||AB||\leq ||A||\cdot||B|| (show by using 4.)
  5. AvAv||A\mathbf{v}||\leq||A||\cdot||\mathbf{v}|| (obvious from following defn)
  6. In=1||I_n||=1

defn. if ||\,|| is a vector form then we can define a matrix norm by A=maxx0Axx||A||=\max_{\mathbf{x}\neq \mathbf{0}}\frac{||A\mathbf{x}||}{||\mathbf{x}||} for any vector x\mathbf{x}.

prop.

when we work in float point environment we get Axnum=bnumA\mathbf{x}^\mathrm{num}=\mathbf{b}^\mathrm{num}. having accurate solution means we want small Δxx\frac{||\Delta \mathbf{x}||}{||\mathbf{x}||} where Δx=xexactxnum\Delta\mathbf{x}=\mathbf{x}^\mathrm{exact}-\mathbf{x}^\mathrm{num}, however we cannot measure this relative error because we do not know the exact solution. we can measure the residual Δbb\frac{||\Delta\mathbf{b}||}{||\mathbf{b}||} where Δb=bexactbnum\Delta\mathbf{b}=\mathbf{b}^\mathrm{exact}-\mathbf{b}^\mathrm{num}. we want to connect them.

to determine accuracy we want to estimate:

defn. the condition number of matrix A is denoted κ(A)=AA1\kappa(A)=||A||\cdot||A^{-1}||.

suppose κ(A)βq\kappa(A)\approx\beta^q, from Δxxκ(A)Δbb\frac{||\Delta\mathbf{x}||}{||\mathbf{x}||}\leq\kappa(A)\frac{||\Delta\mathbf{b}||}{||\mathbf{b}||} we estimate

improving solutions:

recall we wanted Ax=bA\mathbf{x}=\mathbf{b} but get Axnum=bnumA\mathbf{x}^\mathrm{num}=\mathrm{b}^\mathrm{num}, now x=xnum+Δx\mathbf{x}=\mathbf{x}^\mathrm{num}+\Delta\mathbf{x} and b=bnum+Δb\mathbf{b}=\mathbf{b}^\mathrm{num}+\Delta\mathbf{b} so AΔx=ΔbA\Delta\mathbf{x}=\Delta\mathbf{b}. we can solve this equation (cheap as we have done row reduction for A) to get estimate for Δx\Delta\mathbf{x}. then xnew=x+Δx\mathbf{x}^\mathrm{new}=\mathbf{x}+\Delta\mathbf{x} is an improved solution.

page rank

we represent web's structure as a directed graph. nodes represent pages, arcs represent links from one page to another. (out)degree of a node is defined as number of edges leaving that node. web is stored as an adjacency matrix (the degree of node is sum of its column).

local importance: we interpret links as votes: if page j links to page i, this is considered a vote by j that i is important. outgoing links of a page j have equal influence, so the importance that j gives to i is 1deg(j)\frac{1}{\mathrm{deg}(j)}.

2 -----------+ v +-------------------- 4 v | 5 ------------> 6 <---+ page 4 gives importance of 1/2 to page 5

so if page i has many incoming links, it is probably important.

global importance: what if page j only has one link to it but that page is important? => a node is important if important nodes link to it (this is a bit recursive)

ranking by random surfer model:

issues:

we can view actions of random surfer as transition matrix on the web

defn. if the surfer is at node i then all outnodes are equally likely, we define

Pij={1deg(j),link j to i exists0,elseP_{ij}=\left\{\begin{matrix} \frac{1}{\mathrm{deg}(j)},&\text{link }j\text{ to }i\text{ exists}\\ 0,&\text{else} \end{matrix}\right.

then probability that the random surfer is at node i after k steps is mi(k)=j=1RPijmj(k1)m_i^{(k)}=\sum_{j=1}^R P_{ij}m_j^{(k-1)}.

img

defn. let e=[1,...,1]\mathbf{e}=[1,...,1]^\intercal and d=[d1,...,dR]\mathbf{d}=[d_1,...,d_R]^\intercal where di=1d_i=1 if i is a dead end node, then 1Red\frac{1}{R}\mathbf{e}\mathbf{d}^\intercal is a matrix of probabilities which transitions from a dead end node to any other node with equal probability.

defn. let e=[1,...,1]\mathbf{e}=[1,...,1]^\intercal, then 1Ree\frac{1}{R}\mathbf{e}\mathbf{e}^\intercal is a matrix of probabilities which transitions to any other node with equal probability.

defn. we define google matrix as M=α(P+1Red)+(1α)1ReeM=\alpha(P+\frac{1}{R}\mathbf{e}\mathbf{d}^\intercal)+(1-\alpha)\frac{1}{R}\mathbf{e}\mathbf{e}^\intercal where 0<α<10<\alpha<1. so

img

property. (1) each element of M satisfies 0Mij10\leq M_{ij}\leq 1, and columns of M all add to 1. so M is still a markov matrix.

property. (2) if x=[x1,...,xR]\mathbf{x}=[x_1,...,x_R]^\intercal sums to 1, then y=Mx\mathbf{y}=M\mathbf{x} sums to 1.

property. (3) 1 is an eigenvalue of M, so Mx=1xM\mathbf{x}=1\mathbf{x}.
pf. note Me=eM^\intercal\mathbf{e}=\mathbf{e}. \square

property. (4) if λ1,...,λR\lambda_1,...,\lambda_R are eigenvalues of M then λ1,...,λR1|\lambda_1|,...,|\lambda_R|\leq 1.
proof. for each eigenvalue λ\lambda, we have Mv=λvM^\intercal\mathbf{v}=\lambda\mathbf{v} for all v\mathbf{v}. then for all i, m1iv1+...+mRivR=λvi    λvim1iv1+...+mRivR(m1i+...+m1R)max1kRvk=1max1kRvkm_{1i}v_1+...+m_{Ri}v_R=\lambda v_i\implies|\lambda v_i|\leq m_{1i}|v_1|+...+m_{Ri}|v_R|\leq (m_{1i}+...+m_{1R})\max_{1\leq k\leq R}|v_k|=1\cdot\max_{1\leq k\leq R}|v_k|, so λvimax1kRvk1|\lambda|\leq\frac{|v_i|}{\max_{1\leq k\leq R}|v_k|}\leq 1. \square

property. (5) M has only one eigenvalue of length 1. all others are less than 1.

property. (6) if M has eigenvalues 1=λ1λ2...λR1=\lambda_1\geq|\lambda_2|\geq...\geq|\lambda_R| and x(0)=[1R,...,1R]\mathbf{x}^{(0)}=[\frac{1}{R},...,\frac{1}{R}]^\intercal, then there are eigenvectors vi\mathbf{v}_i such that x(0)=i=1Rcivi\mathbf{x}^{(0)}=\sum_{i=1}^Rc_i\mathbf{v}_i.

page rank algorithm:

  1. let M=α(P+1Red)+(1α)1ReeM=\alpha(P+\frac{1}{R}\mathbf{e}\mathbf{d}^\intercal)+(1-\alpha)\frac{1}{R}\mathbf{e}\mathbf{e}^\intercal where 0<α<10<\alpha<1
  2. let x(0)=[1R,...,1R]\mathbf{x}^{(0)}=[\frac{1}{R},...,\frac{1}{R}]^\intercal (start with equal probs)
  3. for k = 1 to stopping criterion:
    1. x(k)=Mx(k1)\mathbf{x}^{(k)}=M\mathbf{x}^{(k-1)}
  4. ranking vector is then limkx(k)=:x()\lim_{k\rightarrow\infty}\mathbf{x}^{(k)}=:\mathbf{x}^{(\infty)}

P is sparse while M is dense, but M is special and we can do MxM\mathbf{x} without dense linear algebra,

  1. first do y=Px\mathbf{y}=P\mathbf{x} via sparse matrix multiplication, ie yi=pi1x1+...+pikxky_i=p_{i1}x_1+...+p_{ik}x_k
  2. similarly we include dead end pages directly via yiyi+x1+...+xky_i\leftarrow y_i+x_1+...+x_k
  3. so final y=Mx\mathbf{y}=M\mathbf{x} has components yi=α(pi1x1+...+pikxk+x1+...+xk)+1αRy_i=\alpha(p_{i1}x_1+...+p_{ik}x_k+x_1+...+x_k)+\frac{1-\alpha}{R}

convergence: by using property 6, we have x(k)=c1v1+i=2Rciλikvic1v1\mathbf{x}^{(k)}=c_1\mathbf{v}_1+ \sum_{i=2}^Rc_i\lambda_i^k\mathbf{v}_i\rightarrow c_1\mathrm{v}_1 as kk\rightarrow\infty. second eigenvalue tells us when to stop, and is λ2=α\lambda_2=\alpha. our stopping criterion is then λ2k1<ϵ|\lambda_2|^{k-1}<\epsilon. if we have precision 8 and α=.85\alpha=.85 we would stop at iteration 114.