├── .gitignore ├── Manifest.toml ├── Project.toml ├── README.md ├── exams ├── computerexam.ipynb ├── computerexam2122.ipynb ├── computerexam2122s.ipynb ├── computerexam2223.ipynb ├── computerexam2223s.ipynb ├── computerexams.ipynb ├── mockexam.ipynb ├── mockexam.jl ├── mockexams.ipynb ├── mockfinal.pdf ├── mockfinals.pdf └── practice.ipynb ├── labs ├── lab1.ipynb ├── lab1.jl ├── lab1s.ipynb ├── lab2.ipynb ├── lab2.jl ├── lab2s.ipynb ├── lab3.ipynb ├── lab3.jl ├── lab3s.ipynb ├── lab4.ipynb ├── lab4.jl ├── lab4s.ipynb ├── lab5.ipynb ├── lab5.jl ├── lab5s.ipynb ├── lab6.ipynb ├── lab6.jl ├── lab6s.ipynb ├── lab7.ipynb ├── lab7.jl ├── lab7s.ipynb ├── lab8.ipynb ├── lab8.jl └── lab8s.ipynb ├── notes ├── A.Asymptotics.tex ├── A.Julia.ipynb ├── A.Permutations.tex ├── I.1.RectangularRule.tex ├── I.2.DividedDifferences.tex ├── I.3.DualNumbers.tex ├── I.4.NewtonMethod.tex ├── II.1.Integers.tex ├── II.2.Reals.tex ├── II.3.Arithmetic.tex ├── II.4.Intervals.tex ├── III.1.StructuredMatrices.tex ├── III.2.DifferentialEquations.tex ├── III.3.Cholesky.tex ├── III.4.Regression.tex ├── III.5.OrthogonalMatrices.tex ├── III.6.QR.tex ├── IV.1.Fourier.tex ├── IV.2.DFT.tex ├── IV.3.OrthogonalPolynomials.tex ├── IV.4.ClassicalOPs.tex ├── IV.5.GaussianQuadrature.tex ├── notes 2.synctex(busy) ├── notes 3.synctex(busy) ├── notes.pdf ├── notes.tex └── somacros.tex ├── sheets ├── figures │ └── sheet6s_5_1.pdf ├── sheet1.jmd ├── sheet1.pdf ├── sheet1.tex ├── sheet10.jmd ├── sheet10.pdf ├── sheet10.tex ├── sheet10s.pdf ├── sheet10s.tex ├── sheet11.jmd ├── sheet11.pdf ├── sheet11.tex ├── sheet11s.pdf ├── sheet11s.tex ├── sheet1s.pdf ├── sheet1s.tex ├── sheet2.jmd ├── sheet2.pdf ├── sheet2.tex ├── sheet2s.pdf ├── sheet2s.tex ├── sheet3.jmd ├── sheet3.pdf ├── sheet3.tex ├── sheet3s.pdf ├── sheet3s.tex ├── sheet4.jmd ├── sheet4.pdf ├── sheet4.tex ├── sheet4s.pdf ├── sheet4s.tex ├── sheet5.jmd ├── sheet5.pdf ├── sheet5.tex ├── sheet5s.pdf ├── sheet5s.tex ├── sheet6.jmd ├── sheet6.pdf ├── sheet6.tex ├── sheet6s.pdf ├── sheet6s.tex ├── sheet7.jmd ├── sheet7.pdf ├── sheet7.tex ├── sheet7s.pdf ├── sheet7s.tex ├── sheet8.jmd ├── sheet8.pdf ├── sheet8.tex ├── sheet8s.pdf ├── sheet8s.tex ├── sheet9.jmd ├── sheet9.pdf ├── sheet9.tex ├── sheet9s.pdf └── sheet9s.tex ├── slides ├── I.1.RectangularRule.key ├── I.1.RectangularRule.pdf ├── I.2.DividedDifferences.key ├── I.2.DividedDifferences.pdf ├── I.3.DualNumbers.key ├── I.3.DualNumbers.pdf ├── I.4.NewtonMethod.key ├── I.4.NewtonMethod.pdf ├── II.1.Integers.key ├── II.1.Integers.pdf ├── II.2.Reals.key ├── II.2.Reals.pdf ├── II.3.Arithmetic.key ├── II.3.Arithmetic.pdf ├── II.4.Intervals.key ├── II.4.Intervals.pdf ├── III.1.StructuredMatrices.key ├── III.1.StructuredMatrices.pdf ├── III.2.DifferentialEquations.key ├── III.2.DifferentialEquations.pdf ├── III.3.Cholesky.key ├── III.3.Cholesky.pdf ├── III.4.Regression.key ├── III.4.Regression.pdf ├── III.5.OrthogonalMatrices.ipynb ├── III.5.OrthogonalMatrices.key ├── III.5.OrthogonalMatrices.pdf ├── III.6.QR.key ├── III.6.QR.pdf ├── IV.1.Fourier.key ├── IV.1.Fourier.pdf ├── IV.2.DFT.key ├── IV.2.DFT.pdf ├── IV.3.OrthogonalPolynomials.key ├── IV.4.ClassicalOPs.key ├── IV.5.GaussianQuadrature.key ├── Revision.key ├── V.FutureDirections.key ├── commandlinetutorial.pdf └── figures │ ├── cheb_exp_101.pdf │ ├── cheb_exp_11.pdf │ ├── cheb_exp_5.pdf │ ├── chebyshevt.pdf │ ├── chebyshevu.pdf │ ├── gausserror.pdf │ ├── imagfft_analytic_10.pdf │ ├── imagfft_analytic_5.pdf │ ├── imagfft_m_runge_101.pdf │ ├── imagfft_m_runge_11.pdf │ ├── imagfft_m_runge_5.pdf │ ├── imagfft_runge_10.pdf │ ├── imagfft_runge_100.pdf │ ├── imagfft_runge_5.pdf │ ├── legendrep.pdf │ ├── realfft_analytic_10.pdf │ ├── realfft_analytic_5.pdf │ ├── realfft_m_exp_101.pdf │ ├── realfft_m_exp_11.pdf │ ├── realfft_m_exp_5.pdf │ ├── realfft_m_runge_101.pdf │ ├── realfft_m_runge_11.pdf │ ├── realfft_m_runge_5.pdf │ ├── realfft_runge_10.pdf │ ├── realfft_runge_100.pdf │ ├── realfft_runge_5.pdf │ ├── slides1_leftrect.pdf │ ├── slides1_rightrect.pdf │ ├── slides1_trap.pdf │ ├── slides2_rightdiff_h=0.01.pdf │ ├── slides2_rightdiff_h=0.1.pdf │ ├── slides2_rightdiff_h=0.3.pdf │ ├── slides2_rightdiff_h=1.0e-16.pdf │ ├── slides4_funct.pdf │ ├── slides_regress_l.pdf │ └── slides_regress_l1.pdf ├── somacros.tex └── src ├── MATH50003NumericalAnalysis.jl ├── compile.jl ├── exams └── mockexams.jl ├── labs ├── lab1s.jl ├── lab2s.jl ├── lab3s.jl ├── lab4s.jl ├── lab5s.jl ├── lab6s.jl ├── lab7s.jl └── lab8s.jl ├── notes ├── A.Asymptotics.jmd ├── A.Julia.jmd ├── A.Permutations.jmd ├── I.1.RectangularRule.jmd ├── I.2.DividedDifferences.jmd ├── I.3.DualNumbers.jmd ├── I.4.NewtonMethod.jmd ├── II.1.Integers.jmd ├── II.2.Reals.jmd ├── II.3.Arithmetic.jmd ├── II.4.Intervals.jmd ├── III.1.StructuredMatrices.jmd ├── III.2.DifferentialEquations.jmd ├── III.3.Cholesky.jmd ├── III.4.Regression.jmd ├── III.5.OrthogonalMatrices.jmd ├── III.6.QR.jmd ├── IV.1.Fourier.jmd ├── IV.2.DFT.jmd ├── IV.3.OrthogonalPolynomials.jmd ├── IV.4.ClassicalOPs.jmd ├── IV.5.GaussianQuadrature.jmd ├── template.tpl └── unused.jmd ├── sheets ├── sheet10s.jmd ├── sheet11s.jmd ├── sheet1s.jmd ├── sheet2s.jmd ├── sheet3s.jmd ├── sheet4s.jmd ├── sheet5s.jmd ├── sheet6s.jmd ├── sheet7s.jmd ├── sheet8s.jmd ├── sheet9s.jmd ├── template.tpl └── unused.jl └── slides ├── slides1.jl ├── slides2.jl ├── slides4.jl ├── slides_classicalops.jl ├── slides_fourier.jl ├── slides_gaussquad.jl ├── slides_ops.jl └── slides_regress.jl /.gitignore: -------------------------------------------------------------------------------- 1 | .CondaPkg 2 | default.profraw 3 | .DS_Store 4 | *.fdb_latexmk 5 | *.fls 6 | *.log 7 | *.gz 8 | *.out 9 | *.aux 10 | Manifest.toml 11 | *.toc 12 | *.ipynb_checkpoints 13 | notes/notes.synctex(busy) 14 | -------------------------------------------------------------------------------- /Project.toml: -------------------------------------------------------------------------------- 1 | name = "MATH50003NumericalAnalysis" 2 | uuid = "9f559201-c820-4d08-baf1-f1369a1016bd" 3 | authors = ["Sheehan Olver "] 4 | version = "0.1.0" 5 | 6 | [deps] 7 | BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 8 | ColorBitstring = "ce91de38-7578-4b98-aebb-6a5df05791d6" 9 | Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d" 10 | DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" 11 | FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" 12 | FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838" 13 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 14 | IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" 15 | Interact = "c601a237-2ae4-5e1e-952c-7a85b0c7eef1" 16 | IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" 17 | LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" 18 | Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" 19 | Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" 20 | Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781" 21 | PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8" 22 | Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" 23 | SetRounding = "3cc68bcd-71a2-5612-b932-767ffbe40ab0" 24 | SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" 25 | Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9" 26 | WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" 27 | -------------------------------------------------------------------------------- /exams/mockfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/exams/mockfinal.pdf -------------------------------------------------------------------------------- /exams/mockfinals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/exams/mockfinals.pdf -------------------------------------------------------------------------------- /labs/lab8.jl: -------------------------------------------------------------------------------- 1 | # # MATH50003 (2023–24) 2 | # # Revision Lab 3 | 4 | using LinearAlgebra, SetRounding, Test 5 | 6 | 7 | # **Problem 1(a)** Simpson's rule on a single panel is given by 8 | # $$ 9 | # ∫_a^b f(x) {\rm d}x ≈ {b-a \over 6} \left[f(a) + 4 f\!\left({a+b \over 2}\right) + f(b) \right]. 10 | # $$ 11 | # Complete the implementation of `simpsonsrule` by dividing $[0,1]$ into grid points $x_0, x_1, …, x_{2n}$ with $x_k = k/(2n)$ 12 | # and applying Simpson's rule on the intervals $[x_{2k-2},x_{2k}]$ for $k = 1,…,n$. 13 | 14 | function simpsonsrule(f, n) 15 | ## TODO: implement Simpsons rule 16 | 17 | end 18 | 19 | @test simpsonsrule(exp, 1000) ≈ exp(1)-1 20 | 21 | # **Problem 1(b)** By computing with various values of $n$, conjecture what the convergence rate is as $n → ∞$. Is it faster than Trapezium rules $O(n^{-2})$? 22 | 23 | ## TODO: vary n and deduce the converge rate, either by looking at errors or by plotting 24 | 25 | 26 | 27 | # **Problem 2** Consider a 3-term divided-difference approximation 28 | # $$ 29 | # f'(x) ≈ {5f(x+2h) - 3f(x) - 2f(x-h) \over 12h} 30 | # $$ 31 | # Implement this in the following function. 32 | 33 | function threeterm_divideddifference(f, x, h) 34 | ## TODO: Implement the above divided-difference formula 35 | 36 | end 37 | @test threeterm_divideddifference(exp, 0, 0.0000001) ≈ 1 atol=1E-5 38 | 39 | 40 | 41 | # **Problem 3** Using the following simplified `Dual` implementation, consider a double-dual number 42 | # like `Dual(Dual(a,b), Dual(c,d))`. By choosing `a`, `b`, `c` and `d`, construct the function `secondderivative(f, x)` 43 | # that computes the second derivative of a function. Hint: it might help to think of a double-dual number as 44 | # $(a + b*ε) + δ*(c + d*ε)$ where $ε^2 = δ^2 = 0$. 45 | 46 | struct Dual 47 | a 48 | b 49 | end 50 | 51 | import Base: +, *, exp 52 | function +(x::Dual, y::Dual) 53 | a,b = x.a, x.b # x == a+bϵ. This gets out a and b 54 | c,d = y.a, y.b # y == c+dϵ. This gets out c and d 55 | Dual(a+c, b+d) 56 | end 57 | function *(x::Dual, y::Dual) 58 | a,b = x.a, x.b # x == a+bϵ. This gets out a and b 59 | c,d = y.a, y.b # y == c+dϵ. This gets out c and d 60 | Dual(a*c, b*c + a*d) 61 | end 62 | exp(x::Dual) = Dual(exp(x.a), exp(x.a) * x.b) 63 | 64 | 65 | function secondderivative(f, x) 66 | ## TODO: compute the second derivative of f using a double-dual number. 67 | 68 | end 69 | 70 | f = x -> exp(x*exp(x)) 71 | @test secondderivative(f, 0) ≈ 3 72 | 73 | # **Problem 4** Implement the following function 74 | # `primedigits` that constructs a positive `Float64` of the form $2^q * (1.b_1…b_S)$ 75 | # where the exponent is specified by `q` and has significand 76 | # bits 77 | # $$ 78 | # b_k = \begin{cases} 79 | # 1 & k\hbox{ is prime} \\ 80 | # 0 & \hbox{otherwise} 81 | # \end{cases} 82 | # $$ 83 | # Hint: use the `gcd` function to determine if a number is prime. 84 | 85 | function primedigits(q) 86 | ## TODO: return a Float64 with the specified bits. 87 | 88 | end 89 | 90 | @test primedigits(3) == 11.317460078808892 91 | 92 | 93 | # **Problem 5** Implement the `sqrt` function with correctly rounded interval arithmetic. 94 | 95 | struct Interval # represents the set [a,b] 96 | a # left endpoint 97 | b # right endpoint 98 | end 99 | 100 | Interval(x) = Interval(x,x) # Support Interval(1) to represent [1,1] 101 | 102 | import Base: sqrt, in 103 | in(x, X::Interval) = X.a ≤ x ≤ X.b 104 | 105 | function sqrt(X::Interval) 106 | a,b = promote(X.a, X.b) # make sure all are the same type 107 | T = typeof(a) 108 | ## TODO: implement sqrt by correctly rounding the computation. 109 | 110 | end 111 | 112 | @test sqrt(big(2.0)) in sqrt(Interval(2.0)) 113 | 114 | 115 | # **Problem 6(a)** Consider the Schrödinger equation with quadratic oscillator: 116 | # $$ 117 | # u(-L) = u(L) = 0, -u'' + x^2 u = f(x) 118 | # $$ 119 | # Use row-eliminations to recast the tridiagonal finite-difference discretisation as a symmetric tridiagonal 120 | # system, solved via the `SymTridiagonal` type,. 121 | 122 | function schrodingersolve(n, L, f) 123 | x = range(-L,L;length=n+1) # discretisation grid 124 | ## TODO: Implement finite differences using a SymTridiagonal matrix, by using the knowledge of the solution at ±L. 125 | 126 | end 127 | 128 | f = x-> 2exp(-x^2) - 3exp(-x^2)*x^2 129 | n,L = 10000,10 130 | x = range(-L,L;length=n+1) 131 | @test schrodingersolve(n, L, f) ≈ exp.(-x.^2) atol=1E-4 132 | 133 | # **Problem 6(b)** The `eigvals` function computes eigenvalues of a matrix. Use this alongside the 134 | # symmetric diagonal discretisation to approximate $λ$ such that 135 | # $$ 136 | # u(-L) = u(L) = 0, -u'' + x^2 u = λ u 137 | # $$ 138 | # has a non-zero solution (i.e., an eigenvalue of the differential equation). 139 | # Can you conjecture their exact value if $L → ∞$? 140 | 141 | function shrodingereigvals(n, L) 142 | x = range(-L,L;length=n+1) # discretisation grid 143 | ## TODO: Use eigvals with a SymTridiagonal discretisation to approximate the eigenvalues of a Schrödinger operator 144 | 145 | end 146 | 147 | ## TODO: add experiments and a comment where you conjecture the true eigenvalues. 148 | 149 | 150 | # **Problem 7** Implement `reversecholesky(A)` that returns an upper-triangular matrix `U` such that `U*U' ≈ A`. 151 | # You may assume the input is symmetric positive definite and has `Float64` values. You must not use the inbuilt `cholesky` 152 | # function or in any other way reduce the problem to a standard Cholesky factorisation. 153 | 154 | 155 | function reversecholesky(A) 156 | n,m = size(A) 157 | if n ≠ m 158 | error("Matrix must be square") 159 | end 160 | if A ≠ A' 161 | error("Matrix must be symmetric") 162 | end 163 | U = UpperTriangular(zeros(n,n)) 164 | ## TODO: populate U so that U'U ≈ A 165 | 166 | U 167 | end 168 | 169 | A = [2 1 0; 1 2 1; 0 1 2] 170 | U = reversecholesky(A) 171 | @test U*U' ≈ A 172 | 173 | 174 | 175 | # **Problem 8** Complete the function `lagrangebasis(g, k, x)` where `g` is a vector of grid 176 | # points, that computes the Lagrange basis function at the point `x`. You may assume all numbers 177 | # are `Float64`. 178 | 179 | function lagrangebasis(g::AbstractVector, k, x) 180 | n = length(g) # number of points 181 | ## TODO: compute ℓ_k(x) corresponding to the grid g 182 | 183 | end 184 | 185 | g = 1:5 186 | @test lagrangebasis(g, 2, 2) == 1 187 | @test lagrangebasis(g, 2, 3) == lagrangebasis(g, 2, 4) == 0 188 | @test lagrangebasis(g, 3, 0.1) ≈ 8.169525 189 | 190 | # **Problem 9(a)** Construct a reverse Householder reflection, that gives an orthogonal matrix 191 | # $Q$ such that, for $𝐱 ∈ ℝ^n$, 192 | # $$ 193 | # 𝐱^⊤ Q = \|𝐱\|𝐞_1^⊤. 194 | # $$ 195 | 196 | function reversehouseholderreflection(x) 197 | ## TODO: implement a Householder reflection that acts on the left 198 | 199 | end 200 | 201 | x = randn(5) 202 | Q = reversehouseholderreflection(x) 203 | @test x'Q ≈ [norm(x) zeros(1,4)] 204 | 205 | # **Problem 9(b)** 206 | # Complete the function `lq(A)` that 207 | # returns a LQ factorisation, that is, `A = LQ` where `L` is lower triangular and `Q` is an orthogonal 208 | # matrix. You may assume that `A` is a square `Matrix{Float64}`. Do not manipulate the problem 209 | # to reduce it to a standard QR factorisation. 210 | function lq(A) 211 | m,n = size(A) 212 | m == n || error("not square") 213 | ## TODO Create Q and L such that A = L*Q, Q'Q == I and L is lower triangular 214 | 215 | end 216 | 217 | A = [1.0 2 3; 1 4 9; 1 1 1] 218 | L,Q = lq(A) 219 | @test Q'Q ≈ I 220 | @test L*Q ≈ A 221 | @test L ≈ tril(L) # it is acceptable to have small non-zero entries in L 222 | -------------------------------------------------------------------------------- /notes/A.Asymptotics.tex: -------------------------------------------------------------------------------- 1 | 2 | We introduce Big-O, little-o and asymptotic notation and see how they can be used to describe computational cost. 3 | 4 | \section{Asymptotics as $n \ensuremath{\rightarrow} \ensuremath{\infty}$} 5 | Big-O, little-o, and \ensuremath{\ldq}asymptotic to" are used to describe behaviour of functions at infinity. 6 | 7 | \begin{definition}[Big-O] 8 | \[ 9 | f(n) = O(\ensuremath{\phi}(n)) \qquad \hbox{(as $n \ensuremath{\rightarrow} \ensuremath{\infty}$)} 10 | \] 11 | means $\left|{f(n) \over \ensuremath{\phi}(n)}\right|$ is bounded for sufficiently large $n$. That is, there exist constants $C$ and $N_0$ such that, for all $n \geq N_0$, $|{f(n) \over \ensuremath{\phi}(n)}| \leq C$. \end{definition} 12 | 13 | \begin{definition}[little-O] 14 | \[ 15 | f(n) = o(\ensuremath{\phi}(n)) \qquad \hbox{(as $n \ensuremath{\rightarrow} \ensuremath{\infty}$)} 16 | \] 17 | means $\lim_{n \ensuremath{\rightarrow} \ensuremath{\infty}} {f(n) \over \ensuremath{\phi}(n)} = 0.$ \end{definition} 18 | 19 | \begin{definition}[asymptotic to] 20 | \[ 21 | f(n) \ensuremath{\sim} \ensuremath{\phi}(n) \qquad \hbox{(as $n \ensuremath{\rightarrow} \ensuremath{\infty}$)} 22 | \] 23 | means $\lim_{n \ensuremath{\rightarrow} \ensuremath{\infty}} {f(n) \over \ensuremath{\phi}(n)} = 1.$ \end{definition} 24 | 25 | \begin{example}[asymptotics with $n$] 26 | 27 | \begin{itemize} 28 | \item[1. ] \[ 29 | {\cos n \over n^2 -1} = O(n^{-2}) 30 | \] 31 | as 32 | 33 | \end{itemize} 34 | \[ 35 | \left|{{\cos n \over n^2 -1} \over n^{-2}} \right| \leq \left| n^2 \over n^2 -1 \right| \leq 2 36 | \] 37 | for $n \geq N_0 = 2$. 38 | 39 | \begin{itemize} 40 | \item[2. ] \[ 41 | \log n = o(n) 42 | \] 43 | as $\lim_{n \ensuremath{\rightarrow} \ensuremath{\infty}} {\log n \over n} = 0.$ 44 | 45 | 46 | \item[3. ] \[ 47 | n^2 + 1 \ensuremath{\sim} n^2 48 | \] 49 | as ${n^2 +1 \over n^2} \ensuremath{\rightarrow} 1.$ 50 | 51 | \end{itemize} 52 | \end{example} 53 | 54 | Note we sometimes write $f(O(\ensuremath{\phi}(n)))$ for a function of the form $f(g(n))$ such that $g(n) = O(\ensuremath{\phi}(n))$. 55 | 56 | We have some simple algebraic rules: 57 | 58 | \begin{proposition}[Big-O rules] 59 | \begin{align*} 60 | O(\ensuremath{\phi}(n))O(\ensuremath{\psi}(n)) = O(\ensuremath{\phi}(n)\ensuremath{\psi}(n)) \qquad \hbox{(as $n \ensuremath{\rightarrow} \ensuremath{\infty}$)} \\ 61 | O(\ensuremath{\phi}(n)) + O(\ensuremath{\psi}(n)) = O(|\ensuremath{\phi}(n)| + |\ensuremath{\psi}(n)|) \qquad \hbox{(as $n \ensuremath{\rightarrow} \ensuremath{\infty}$)}. 62 | \end{align*} 63 | \end{proposition} 64 | \textbf{Proof} See any standard book on asymptotics, eg \href{https://www.taylorfrancis.com/books/mono/10.1201/9781439864548/asymptotics-special-functions-frank-olver}{F.W.J. Olver, Asymptotics and Special Functions}. \ensuremath{\QED} 65 | 66 | \section{Asymptotics as $x \ensuremath{\rightarrow} x_0$} 67 | We also have Big-O, little-o and "asymptotic to" at a point: 68 | 69 | \begin{definition}[Big-O] 70 | \[ 71 | f(x) = O(\ensuremath{\phi}(x)) \qquad \hbox{(as $x \ensuremath{\rightarrow} x_0$)} 72 | \] 73 | means $|{f(x) \over \ensuremath{\phi}(x)}|$ is bounded in a neighbourhood of $x_0$. That is, there exist constants $C$ and $r$ such that, for all $0 \leq |x - x_0| \leq r$, $|{f(x) \over \ensuremath{\phi}(x)}| \leq C$. \end{definition} 74 | 75 | \begin{definition}[little-O] 76 | \[ 77 | f(x) = o(\ensuremath{\phi}(x)) \qquad \hbox{(as $x \ensuremath{\rightarrow} x_0$)} 78 | \] 79 | means $\lim_{x \ensuremath{\rightarrow} x_0} {f(x) \over \ensuremath{\phi}(x)} = 0.$ \end{definition} 80 | 81 | \begin{definition}[asymptotic to] 82 | \[ 83 | f(x) \ensuremath{\sim} \ensuremath{\phi}(x) \qquad \hbox{(as $x \ensuremath{\rightarrow} x_0$)} 84 | \] 85 | means $\lim_{x \ensuremath{\rightarrow} x_0} {f(x) \over \ensuremath{\phi}(x)} = 1.$ \end{definition} 86 | 87 | \begin{example}[asymptotics with $x$] 88 | \[ 89 | \exp x = 1 + x + O(x^2) \qquad \hbox{as $x \ensuremath{\rightarrow} 0$} 90 | \] 91 | since $\exp x = 1 + x + {\exp t \over 2} x^2$ for some $t \in [0,x]$ and 92 | \[ 93 | \left|{{\exp t \over 2} x^2 \over x^2}\right| \leq {3 \over 2} 94 | \] 95 | provided $x \leq 1$. \end{example} 96 | 97 | \section{Computational cost} 98 | We will use Big-O notation to describe the computational cost of algorithms. Consider the following simple sum 99 | \[ 100 | \sum_{k=1}^n x_k^2 101 | \] 102 | which we might implement as: 103 | 104 | 105 | \begin{lstlisting} 106 | (*@\HLJLk{function}@*) (*@\HLJLnf{sumsq}@*)(*@\HLJLp{(}@*)(*@\HLJLn{x}@*)(*@\HLJLp{)}@*) 107 | (*@\HLJLn{n}@*) (*@\HLJLoB{=}@*) (*@\HLJLnf{length}@*)(*@\HLJLp{(}@*)(*@\HLJLn{x}@*)(*@\HLJLp{)}@*) 108 | (*@\HLJLn{ret}@*) (*@\HLJLoB{=}@*) (*@\HLJLnfB{0.0}@*) 109 | (*@\HLJLk{for}@*) (*@\HLJLn{k}@*) (*@\HLJLoB{=}@*) (*@\HLJLni{1}@*)(*@\HLJLoB{:}@*)(*@\HLJLn{n}@*) 110 | (*@\HLJLn{ret}@*) (*@\HLJLoB{=}@*) (*@\HLJLn{ret}@*) (*@\HLJLoB{+}@*) (*@\HLJLn{x}@*)(*@\HLJLp{[}@*)(*@\HLJLn{k}@*)(*@\HLJLp{]}@*)(*@\HLJLoB{{\textasciicircum}}@*)(*@\HLJLni{2}@*) 111 | (*@\HLJLk{end}@*) 112 | (*@\HLJLn{ret}@*) 113 | (*@\HLJLk{end}@*) 114 | \end{lstlisting} 115 | 116 | \begin{lstlisting} 117 | sumsq (generic function with 1 method) 118 | \end{lstlisting} 119 | 120 | 121 | Each step of this algorithm consists of one memory look-up (\texttt{z = x[k]}), one multiplication (\texttt{w = z*z}) and one addition (\texttt{ret = ret + w}). We will ignore the memory look-up in the following discussion. The number of CPU operations per step is therefore 2 (the addition and multiplication). Thus the total number of CPU operations is $2n$. But the constant $2$ here is misleading: we didn't count the memory look-up, thus it is more sensible to just talk about the asymptotic complexity, that is, the \emph{computational cost} is $O(n)$. 122 | 123 | Now consider a double sum like: 124 | \[ 125 | \sum_{k=1}^n \sum_{j=1}^k x_j^2 126 | \] 127 | which we might implement as: 128 | 129 | 130 | \begin{lstlisting} 131 | (*@\HLJLk{function}@*) (*@\HLJLnf{sumsq2}@*)(*@\HLJLp{(}@*)(*@\HLJLn{x}@*)(*@\HLJLp{)}@*) 132 | (*@\HLJLn{n}@*) (*@\HLJLoB{=}@*) (*@\HLJLnf{length}@*)(*@\HLJLp{(}@*)(*@\HLJLn{x}@*)(*@\HLJLp{)}@*) 133 | (*@\HLJLn{ret}@*) (*@\HLJLoB{=}@*) (*@\HLJLnfB{0.0}@*) 134 | (*@\HLJLk{for}@*) (*@\HLJLn{k}@*) (*@\HLJLoB{=}@*) (*@\HLJLni{1}@*)(*@\HLJLoB{:}@*)(*@\HLJLn{n}@*) 135 | (*@\HLJLk{for}@*) (*@\HLJLn{j}@*) (*@\HLJLoB{=}@*) (*@\HLJLni{1}@*)(*@\HLJLoB{:}@*)(*@\HLJLn{k}@*) 136 | (*@\HLJLn{ret}@*) (*@\HLJLoB{=}@*) (*@\HLJLn{ret}@*) (*@\HLJLoB{+}@*) (*@\HLJLn{x}@*)(*@\HLJLp{[}@*)(*@\HLJLn{j}@*)(*@\HLJLp{]}@*)(*@\HLJLoB{{\textasciicircum}}@*)(*@\HLJLni{2}@*) 137 | (*@\HLJLk{end}@*) 138 | (*@\HLJLk{end}@*) 139 | (*@\HLJLn{ret}@*) 140 | (*@\HLJLk{end}@*) 141 | \end{lstlisting} 142 | 143 | \begin{lstlisting} 144 | sumsq2 (generic function with 1 method) 145 | \end{lstlisting} 146 | 147 | 148 | Now the inner loop is $O(1)$ operations (we don't try to count the precise number), which we do $k$ times for $O(k)$ operations as $k \ensuremath{\rightarrow} \ensuremath{\infty}$. The outer loop therefore takes 149 | \[ 150 | \ensuremath{\sum}_{k = 1}^n O(k) = O\left(\ensuremath{\sum}_{k = 1}^n k\right) = O\left( {n (n+1) \over 2} \right) = O(n^2) 151 | \] 152 | operations. 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /notes/A.Permutations.tex: -------------------------------------------------------------------------------- 1 | 2 | Permutation matrices are matrices that represent the action of permuting the entries of a vector, that is, matrix representations of the symmetric group $S_n$, acting on $\ensuremath{\bbR}^n$. Recall every $\ensuremath{\sigma} \ensuremath{\in} S_n$ is a bijection between $\{1,2,\ensuremath{\ldots},n\}$ and itself. We can write a permutation $\ensuremath{\sigma}$ in \emph{Cauchy notation}: 3 | \[ 4 | \begin{pmatrix} 5 | 1 & 2 & 3 & \ensuremath{\cdots} & n \cr 6 | \ensuremath{\sigma}_1 & \ensuremath{\sigma}_2 & \ensuremath{\sigma}_3 & \ensuremath{\cdots} & \ensuremath{\sigma}_n 7 | \end{pmatrix} 8 | \] 9 | where $\{\ensuremath{\sigma}_1,\ensuremath{\ldots},\ensuremath{\sigma}_n\} = \{1,2,\ensuremath{\ldots},n\}$ (that is, each integer appears precisely once). We denote the \emph{inverse permutation} by $\ensuremath{\sigma}^{-1}$, which can be constructed by swapping the rows of the Cauchy notation and reordering. 10 | 11 | We can encode a permutation in vector $\mathbf \ensuremath{\sigma} = [\ensuremath{\sigma}_1,\ensuremath{\ldots},\ensuremath{\sigma}_n]$. This induces an action on a vector (using indexing notation) 12 | \[ 13 | \ensuremath{\bm{\v}}[\mathbf \ensuremath{\sigma}] = \begin{bmatrix}v_{\ensuremath{\sigma}_1}\\ \vdots \\ v_{\ensuremath{\sigma}_n} \end{bmatrix} 14 | \] 15 | \begin{example}[permutation of a vector] Consider the permutation $\ensuremath{\sigma}$ given by 16 | \[ 17 | \begin{pmatrix} 18 | 1 & 2 & 3 & 4 & 5 \cr 19 | 1 & 4 & 2 & 5 & 3 20 | \end{pmatrix} 21 | \] 22 | We can apply it to a vector: 23 | 24 | 25 | \begin{lstlisting} 26 | (*@\HLJLk{using}@*) (*@\HLJLn{LinearAlgebra}@*) 27 | (*@\HLJLn{\ensuremath{\sigma}}@*) (*@\HLJLoB{=}@*) (*@\HLJLp{[}@*)(*@\HLJLni{1}@*)(*@\HLJLp{,}@*) (*@\HLJLni{4}@*)(*@\HLJLp{,}@*) (*@\HLJLni{2}@*)(*@\HLJLp{,}@*) (*@\HLJLni{5}@*)(*@\HLJLp{,}@*) (*@\HLJLni{3}@*)(*@\HLJLp{]}@*) 28 | (*@\HLJLn{v}@*) (*@\HLJLoB{=}@*) (*@\HLJLp{[}@*)(*@\HLJLni{6}@*)(*@\HLJLp{,}@*) (*@\HLJLni{7}@*)(*@\HLJLp{,}@*) (*@\HLJLni{8}@*)(*@\HLJLp{,}@*) (*@\HLJLni{9}@*)(*@\HLJLp{,}@*) (*@\HLJLni{10}@*)(*@\HLJLp{]}@*) 29 | (*@\HLJLn{v}@*)(*@\HLJLp{[}@*)(*@\HLJLn{\ensuremath{\sigma}}@*)(*@\HLJLp{]}@*) (*@\HLJLcs{{\#}}@*) (*@\HLJLcs{we}@*) (*@\HLJLcs{permutate}@*) (*@\HLJLcs{entries}@*) (*@\HLJLcs{of}@*) (*@\HLJLcs{v}@*) 30 | \end{lstlisting} 31 | 32 | \begin{lstlisting} 33 | 5-element Vector(*@{{\{}}@*)Int64(*@{{\}}}@*): 34 | 6 35 | 9 36 | 7 37 | 10 38 | 8 39 | \end{lstlisting} 40 | 41 | 42 | Its inverse permutation $\ensuremath{\sigma}^{-1}$ has Cauchy notation coming from swapping the rows of the Cauchy notation of $\ensuremath{\sigma}$ and sorting: 43 | \[ 44 | \begin{pmatrix} 45 | 1 & 4 & 2 & 5 & 3 \cr 46 | 1 & 2 & 3 & 4 & 5 47 | \end{pmatrix} \rightarrow \begin{pmatrix} 48 | 1 & 2 & 4 & 3 & 5 \cr 49 | 1 & 3 & 2 & 5 & 4 50 | \end{pmatrix} 51 | \] 52 | \end{example} 53 | 54 | Note that the operator 55 | \[ 56 | P_\ensuremath{\sigma}(\ensuremath{\bm{\v}}) = \ensuremath{\bm{\v}}[{\mathbf \ensuremath{\sigma}}] 57 | \] 58 | is linear in $\ensuremath{\bm{\v}}$, therefore, we can identify it with a matrix whose action is: 59 | \[ 60 | P_\ensuremath{\sigma} \begin{bmatrix} v_1\\ \vdots \\ v_n \end{bmatrix} = \begin{bmatrix}v_{\ensuremath{\sigma}_1} \\ \vdots \\ v_{\ensuremath{\sigma}_n} \end{bmatrix}. 61 | \] 62 | The entries of this matrix are 63 | \[ 64 | P_\ensuremath{\sigma}[k,j] = \ensuremath{\bm{\e}}_k^\ensuremath{\top} P_\ensuremath{\sigma} \ensuremath{\bm{\e}}_j = \ensuremath{\bm{\e}}_k^\ensuremath{\top} \ensuremath{\bm{\e}}_{\ensuremath{\sigma}^{-1}_j} = \ensuremath{\delta}_{k,\ensuremath{\sigma}^{-1}_j} = \ensuremath{\delta}_{\ensuremath{\sigma}_k,j} 65 | \] 66 | where $\ensuremath{\delta}_{k,j}$ is the \emph{Kronecker delta}: 67 | \[ 68 | \ensuremath{\delta}_{k,j} := \begin{cases} 1 & k = j \\ 69 | 0 & \hbox{otherwise} 70 | \end{cases}. 71 | \] 72 | This construction motivates the following definition: 73 | 74 | \begin{definition}[permutation matrix] $P \in \ensuremath{\bbR}^{n \ensuremath{\times} n}$ is a permutation matrix if it is equal to the identity matrix with its rows permuted. \end{definition} 75 | 76 | \begin{proposition}[permutation matrix inverse] Let $P_\ensuremath{\sigma}$ be a permutation matrix corresponding to the permutation $\ensuremath{\sigma}$. Then 77 | \[ 78 | P_\ensuremath{\sigma}^\ensuremath{\top} = P_{\ensuremath{\sigma}^{-1}} = P_\ensuremath{\sigma}^{-1} 79 | \] 80 | That is, $P_\ensuremath{\sigma}$ is \emph{orthogonal}: 81 | \[ 82 | P_\ensuremath{\sigma}^\ensuremath{\top} P_\ensuremath{\sigma} = P_\ensuremath{\sigma} P_\ensuremath{\sigma}^\ensuremath{\top} = I. 83 | \] 84 | \end{proposition} 85 | \textbf{Proof} 86 | 87 | We prove orthogonality via: 88 | \[ 89 | \ensuremath{\bm{\e}}_k^\ensuremath{\top} P_\ensuremath{\sigma}^\ensuremath{\top} P_\ensuremath{\sigma} \ensuremath{\bm{\e}}_j = (P_\ensuremath{\sigma} \ensuremath{\bm{\e}}_k)^\ensuremath{\top} P_\ensuremath{\sigma} \ensuremath{\bm{\e}}_j = \ensuremath{\bm{\e}}_{\ensuremath{\sigma}^{-1}_k}^\ensuremath{\top} \ensuremath{\bm{\e}}_{\ensuremath{\sigma}^{-1}_j} = \ensuremath{\delta}_{k,j} 90 | \] 91 | This shows $P_\ensuremath{\sigma}^\ensuremath{\top} P_\ensuremath{\sigma} = I$ and hence $P_\ensuremath{\sigma}^{-1} = P_\ensuremath{\sigma}^\ensuremath{\top}$. 92 | 93 | \ensuremath{\QED} 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /notes/I.1.RectangularRule.tex: -------------------------------------------------------------------------------- 1 | 2 | \section{Rectangular rule} 3 | One possible definition for an integral is the limit of a Riemann sum, for example: 4 | \[ 5 | \ensuremath{\int}_a^b f(x) {\rm d}x = \lim_{n \ensuremath{\rightarrow} \ensuremath{\infty}} h \ensuremath{\sum}_{j=1}^n f(x_j) 6 | \] 7 | where $x_j = a+jh$ are evenly spaced points dividing up the interval $[a,b]$, that is with the \emph{step size} $h = (b-a)/n$. This suggests an algorithm known as the \emph{(right-sided) rectangular rule} for approximating an integral: choose $n$ large so that 8 | \[ 9 | \ensuremath{\int}_a^b f(x) {\rm d}x \ensuremath{\approx} h \ensuremath{\sum}_{j=1}^n f(x_j). 10 | \] 11 | In the lab we explore practical implementation of this approximation, and observe that the error in approximation is bounded by $C/n$ for some constant $C$. This can be expressed using \ensuremath{\ldq}Big-O" notation: 12 | \[ 13 | \ensuremath{\int}_a^b f(x) {\rm d}x = h \ensuremath{\sum}_{j=1}^n f(x_j) + O(1/n). 14 | \] 15 | In these notes we consider the \ensuremath{\ldq}Analysis" part of \ensuremath{\ldq}Numerical Analysis": we want to \emph{prove} the convergence rate of the approximation, including finding an explicit expression for the constant $C$. 16 | 17 | To tackle this question we consider the error incurred on a single panel $(x_{j-1},x_j)$, then sum up the errors on rectangles. 18 | 19 | Now for a secret. There are only so many tools available in analysis (especially at this stage of your career), and one can make a safe bet that the right tool in any analysis proof is either (1) integration-by-parts, (2) geometric series or (3) Taylor series. In this case we use (1): 20 | 21 | \begin{lemma}[(Right-sided) Rectangular Rule error on one panel] Assuming $f$ is differentiable we have 22 | \[ 23 | \ensuremath{\int}_a^b f(x) {\rm d}x = (b-a) f(b) + \ensuremath{\delta} 24 | \] 25 | where $|\ensuremath{\delta}| \ensuremath{\leq} M (b-a)^2$ for $M = \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|f'(x)|$. 26 | 27 | \end{lemma} 28 | \textbf{Proof} We write 29 | \meeq{ 30 | \ensuremath{\int}_a^b f(x) {\rm d}x = \ensuremath{\int}_a^b (x-a)' f(x) {\rm d}x = [(x-a) f(x)]_a^b - \ensuremath{\int}_a^b (x-a) f'(x) {\rm d} x \ccr 31 | = (b-a) f(b) + \underbrace{\left(-\ensuremath{\int}_a^b (x-a) f'(x) {\rm d} x \right)}_\ensuremath{\delta}. 32 | } 33 | Recall that we can bound the absolute value of an integral by the supremum of the integrand times the width of the integration interval: 34 | \[ 35 | \abs{\ensuremath{\int}_a^b g(x) {\rm d} x} \ensuremath{\leq} (b-a) \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|g(x)|. 36 | \] 37 | The lemma thus follows since 38 | \begin{align*} 39 | \abs{\ensuremath{\int}_a^b (x-a) f'(x) {\rm d} x} &\ensuremath{\leq} (b-a) \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|(x-a) f'(x)| \\ 40 | &\ensuremath{\leq} (b-a) \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|x-a| \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|f'(x)|\\ 41 | &\ensuremath{\leq} M (b-a)^2. 42 | \end{align*} 43 | \ensuremath{\QED} 44 | 45 | Now summing up the errors in each panel gives us the error of using the Rectangular rule: 46 | 47 | \begin{theorem}[Rectangular Rule error] Assuming $f$ is differentiable we have 48 | \[ 49 | \ensuremath{\int}_a^b f(x) {\rm d}x = h \ensuremath{\sum}_{j=1}^n f(x_j) + \ensuremath{\delta} 50 | \] 51 | where $|\ensuremath{\delta}| \ensuremath{\leq} M (b-a) h$ for $M = \sup_{a \ensuremath{\leq} x \ensuremath{\leq} b}|f'(x)|$, $h = (b-a)/n$ and $x_j = a + jh$. 52 | 53 | \end{theorem} 54 | \textbf{Proof} We split the integral into a sum of smaller integrals: 55 | \[ 56 | \ensuremath{\int}_a^b f(x) {\rm d}x = \ensuremath{\sum}_{j=1}^n \ensuremath{\int}_{x_{j-1}}^{x_j} f(x) {\rm d}x = 57 | \ensuremath{\sum}_{j=1}^n \br[(x_j - x_{j-1}) f(x_j) + \ensuremath{\delta}_j] = h \ensuremath{\sum}_{j=1}^n f(x_j) + \underbrace{\ensuremath{\sum}_{j=1}^n \ensuremath{\delta}_j}_\ensuremath{\delta} 58 | \] 59 | where $\ensuremath{\delta}_j$, the error on each panel as in the preceding lemma, satisfies 60 | \[ 61 | |\ensuremath{\delta}_j| \ensuremath{\leq} (x_j-x_{j-1})^2 \sup_{x_{j-1} \ensuremath{\leq} x \ensuremath{\leq} x_j}|f'(x)| \ensuremath{\leq} M h^2. 62 | \] 63 | Thus using the triangular inequality we have 64 | \[ 65 | |\ensuremath{\delta}| = \abs{ \ensuremath{\sum}_{j=1}^n \ensuremath{\delta}_j} \ensuremath{\leq} \ensuremath{\sum}_{j=1}^n |\ensuremath{\delta}_j| \ensuremath{\leq} M n h^2 = M(b-a)h. 66 | \] 67 | \ensuremath{\QED} 68 | 69 | Note a consequence of this lemma is that the approximation converges as $n \ensuremath{\rightarrow} \ensuremath{\infty}$ (i.e. $h \ensuremath{\rightarrow} 0$). In the labs and problem sheets we will consider the left-sided rule: 70 | \[ 71 | \ensuremath{\int}_a^b f(x) {\rm d}x \ensuremath{\approx} h \ensuremath{\sum}_{j=0}^{n-1} f(x_j). 72 | \] 73 | We also consider the \emph{Trapezium rule}. Here we approximate an integral by an affine function: 74 | \[ 75 | \ensuremath{\int}_a^b f(x) {\rm d} x \ensuremath{\approx} \ensuremath{\int}_a^b {(b-x)f(a) + (x-a)f(b) \over b-a} \dx 76 | = {b-a \over 2} \br[f(a) + f(b)]. 77 | \] 78 | Subdividing an interval $a = x_0 < x_1 < \ensuremath{\ldots} < x_n = b$ and applying this approximation separately on each subinterval $[x_{j-1},x_j]$, where $h = (b-a)/n$ and $x_j = a + jh$, leads to the approximation 79 | \[ 80 | \ensuremath{\int}_a^b f(x) {\rm d}x \ensuremath{\approx} {h \over 2} f(a) + h \ensuremath{\sum}_{j=1}^{n-1} f(x_j) + {h \over 2} f(b) 81 | \] 82 | We shall see both experimentally and provably that this approximation converges faster than the rectangular rule. 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /notes/I.2.DividedDifferences.tex: -------------------------------------------------------------------------------- 1 | 2 | \section{Divided Differences} 3 | Given a function, how can we approximate its derivative at a point? We consider an intuitive approach to this problem using \emph{(Right-sided) Divided Differences}: 4 | \[ 5 | f'(x) \ensuremath{\approx} {f(x+h) - f(x) \over h} 6 | \] 7 | Note by the definition of the derivative we know that this approximation will converge to the true derivative as $h \ensuremath{\rightarrow} 0$. But in numerical approimxations we also need to consider the rate of convergence. 8 | 9 | Now in the previous section I mentioned there are three basic tools in analysis: (1) integration-by-parts, (2) geometric series or (3) Taylor series. In this case we use (3): 10 | 11 | \begin{proposition}[divided differences error] Suppose that $f$ is twice-differentiable on the interval $[x,x+h]$. The error in approximating the derivative using divided differences is 12 | \[ 13 | f'(x) = {f(x+h) - f(x) \over h} + \ensuremath{\delta} 14 | \] 15 | where $|\ensuremath{\delta}| \ensuremath{\leq} Mh/2$ for $M = \sup_{x \ensuremath{\leq} t \ensuremath{\leq} x+h} |f''(t)|$. 16 | 17 | \end{proposition} 18 | \textbf{Proof} Follows immediately from Taylor's theorem: 19 | \[ 20 | f(x+h) = f(x) + f'(x) h + \underbrace{{f''(t) \over 2} h^2}_{h \ensuremath{\delta}} 21 | \] 22 | for some $x \ensuremath{\leq} t \ensuremath{\leq} x+h$, by bounding: 23 | \[ 24 | |\ensuremath{\delta}| \ensuremath{\leq} \abs{{f''(t) \over 2} h} \ensuremath{\leq} {M h \over 2}. 25 | \] 26 | \ensuremath{\QED} 27 | 28 | Unlike the rectangular rule, the computational cost of computing the divided difference is independent of $h$! We only need to evaluate a function $f$ twice and do a single division. Here we are assuming that the computational cost of evaluating $f$ is independent of the point of evaluation. Later we will investigate the details of how computers work with numbers via floating point, and confirm that this is a sensible assumption. 29 | 30 | So why not just set $h$ ridiculously small? In the lab we explore this question and observe that there are significant errors introduced in the numerical realisation of this algorithm. We will return to the question of understanding these errors after learning floating point numbers. 31 | 32 | There are alternative versions of divided differences. Left-side divided differences evaluates to the left of the point where we wish to know the derivative: 33 | \[ 34 | f'(x) \ensuremath{\approx} {f(x) - f(x-h) \over h} 35 | \] 36 | and central differences: 37 | \[ 38 | f'(x) \ensuremath{\approx} {f(x + h) - f(x - h) \over 2h} 39 | \] 40 | We can further arrive at an approximation to the second derivative by composing a left- and right-sided finite difference: 41 | \[ 42 | f''(x) \ensuremath{\approx} {f'(x+h) - f'(x) \over h} \ensuremath{\approx} {{f(x+h) - f(x) \over h} - {f(x) - f(x-h) \over h} \over h} 43 | = {f(x+h) - 2f(x) + f(x-h) \over h^2} 44 | \] 45 | In the lab we investigate the convergence rate of these approximations (in particular, that central differences is more accurate than standard divided differences) and observe that they too suffer from unexplained (for now) loss of accuracy as $h \ensuremath{\rightarrow} 0$. In the problem sheet we prove the theoretical convergence rate, which is never realised because of these errors. 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /notes/I.3.DualNumbers.tex: -------------------------------------------------------------------------------- 1 | 2 | \section{Dual Numbers} 3 | In this section we introduce a mathematically beautiful alternative to divided differences for computing derivatives: \emph{dual numbers}. These are a commutative ring that \emph{exactly} compute derivatives, which when implemented on a computer gives very high-accuracy approximations to derivatives. They underpin forward-mode \href{https://en.wikipedia.org/wiki/Automatic_differentiation}{automatic differentation}. Automatic differentiation is a basic tool in Machine Learning for computing gradients necessary for training neural networks. 4 | 5 | \begin{definition}[Dual numbers] Dual numbers $\ensuremath{\bbD}$ are a commutative ring (over $\ensuremath{\bbR}$) generated by $1$ and $\ensuremath{\epsilon}$ such that $\ensuremath{\epsilon}^2 = 0$. Dual numbers are typically written as $a + b \ensuremath{\epsilon}$ where $a$ and $b$ are real. \end{definition} 6 | 7 | This is very much analoguous to complex numbers, which are a field generated by $1$ and $\I$ such that $\I^2 = -1$. Compare multiplication of each number type: 8 | \meeq{ 9 | (a + b \I) (c + d \I) = ac + (bc + ad) \I + bd \I^2 = ac -bd + (bc + ad) \I \ccr 10 | (a + b \ensuremath{\epsilon}) (c + d \ensuremath{\epsilon}) = ac + (bc + ad) \ensuremath{\epsilon} + bd \ensuremath{\epsilon}^2 = ac + (bc + ad) \ensuremath{\epsilon} 11 | } 12 | And just as we view $\ensuremath{\bbR} \ensuremath{\subset} \ensuremath{\bbC}$ by equating $a \ensuremath{\in} \ensuremath{\bbR}$ with $a + 0\I \ensuremath{\in} \ensuremath{\bbC}$, we can view $\ensuremath{\bbR} \ensuremath{\subset} \ensuremath{\bbD}$ by equating $a \ensuremath{\in} \ensuremath{\bbR}$ with $a + 0{\rm \ensuremath{\epsilon}} \ensuremath{\in} \ensuremath{\bbD}$. 13 | 14 | \subsection{Differentiating polynomials} 15 | Polynomials evaluated on dual numbers are well-defined as they depend only on the operations $+$ and $*$. From the formula for multiplication of dual numbers we deduce that evaluating a polynomial at a dual number $a + b \ensuremath{\epsilon}$ tells us the derivative of the polynomial at $a$: 16 | 17 | \begin{theorem}[polynomials on dual numbers] Suppose $p$ is a polynomial. Then 18 | \[ 19 | p(a + b \ensuremath{\epsilon}) = p(a) + b p'(a) \ensuremath{\epsilon} 20 | \] 21 | \end{theorem} 22 | \textbf{Proof} 23 | 24 | First consider $p(x) = x^n$ for $n \ensuremath{\geq} 0$. The cases $n = 0$ and $n = 1$ are immediate. For $n > 1$ we have by induction: 25 | \[ 26 | (a + b \ensuremath{\epsilon})^n = (a + b \ensuremath{\epsilon}) (a + b \ensuremath{\epsilon})^{n-1} = (a + b \ensuremath{\epsilon}) (a^{n-1} + (n-1) b a^{n-2} \ensuremath{\epsilon}) = a^n + b n a^{n-1} \ensuremath{\epsilon}. 27 | \] 28 | For a more general polynomial 29 | \[ 30 | p(x) = \ensuremath{\sum}_{k=0}^n c_k x^k 31 | \] 32 | the result follows from linearity: 33 | \[ 34 | p(a + b \ensuremath{\varepsilon}) = \ensuremath{\sum}_{k=0}^n c_k (a+b\ensuremath{\epsilon})^k = c_0 + \ensuremath{\sum}_{k=1}^n c_k (a^k +k b a^{k-1}\ensuremath{\epsilon}) 35 | = \ensuremath{\sum}_{k=0}^n c_k a^k + b \ensuremath{\sum}_{k=1}^n c_k k a^{k-1}\ensuremath{\epsilon} = p(a) + b p'(a) \ensuremath{\epsilon}. 36 | \] 37 | \ensuremath{\QED} 38 | 39 | \begin{example}[differentiating polynomial] Consider computing $p'(2)$ where 40 | \[ 41 | p(x) = (x-1)(x-2) + x^2. 42 | \] 43 | We can use dual numbers to differentiate, avoiding expanding in monomials or applying rules of differentiating: 44 | \[ 45 | p(2+\ensuremath{\epsilon}) = (1+\ensuremath{\epsilon})\ensuremath{\epsilon} + (2+\ensuremath{\epsilon})^2 = \ensuremath{\epsilon} + 4 + 4\ensuremath{\epsilon} = 4 + \underbrace{5}_{p'(2)}\ensuremath{\epsilon} 46 | \] 47 | \end{example} 48 | 49 | \subsection{Differentiating other functions} 50 | We can extend real-valued differentiable functions to dual numbers in a similar manner. First, consider a standard function with a Taylor series (e.g. ${\rm cos}$, ${\rm sin}$, ${\rm exp}$, etc.) 51 | \[ 52 | f(x) = \ensuremath{\sum}_{k=0}^\ensuremath{\infty} f_k x^k 53 | \] 54 | so that $a$ is inside the radius of convergence. This leads naturally to a definition on dual numbers: 55 | \meeq{ 56 | f(a + b \ensuremath{\epsilon}) = \ensuremath{\sum}_{k=0}^\ensuremath{\infty} f_k (a + b \ensuremath{\epsilon})^k = f_0 + \ensuremath{\sum}_{k=1}^\ensuremath{\infty} f_k (a^k + k a^{k-1} b \ensuremath{\epsilon}) = \ensuremath{\sum}_{k=0}^\ensuremath{\infty} f_k a^k + \ensuremath{\sum}_{k=1}^\ensuremath{\infty} f_k k a^{k-1} b \ensuremath{\epsilon} \ccr 57 | = f(a) + b f'(a) \ensuremath{\epsilon} 58 | } 59 | More generally, given a differentiable function we can extend it to dual numbers: 60 | 61 | \begin{definition}[dual extension] Suppose a real-valued function $f$ is differentiable at $a$. If 62 | \[ 63 | f(a + b \ensuremath{\epsilon}) = f(a) + b f'(a) \ensuremath{\epsilon} 64 | \] 65 | then we say that it is a \emph{dual extension at} $a$. 66 | 67 | Thus, for basic functions we have natural extensions: 68 | \begin{align*} 69 | \exp(a + b \ensuremath{\epsilon}) &:= \exp(a) + b \exp(a) \ensuremath{\epsilon} \\ 70 | \sin(a + b \ensuremath{\epsilon}) &:= \sin(a) + b \cos(a) \ensuremath{\epsilon} \\ 71 | \cos(a + b \ensuremath{\epsilon}) &:= \cos(a) - b \sin(a) \ensuremath{\epsilon} \\ 72 | \log(a + b \ensuremath{\epsilon}) &:= \log(a) + {b \over a} \ensuremath{\epsilon} \\ 73 | \sqrt{a+b \ensuremath{\epsilon}} &:= \sqrt{a} + {b \over 2 \sqrt{a}} \ensuremath{\epsilon} \\ 74 | |a + b \ensuremath{\epsilon}| &:= |a| + b\, {\rm sign} a\, \ensuremath{\epsilon} 75 | \end{align*} 76 | provided the function is differentiable at $a$. Note the last example does not have a convergent Taylor series (at 0) but we can still extend it where it is differentiable. 77 | 78 | Going further, we can add, multiply, and compose such functions: 79 | 80 | \begin{lemma}[product and chain rule] If $f$ is a dual extension at $g(a)$ and $g$ is a dual extension at $a$, then $q(x) := f(g(x))$ is a dual extension at $a$. If $f$ and $g$ are dual extensions at $a$ then $r(x) := f(x) g(x)$ is also dual extensions at $a$. In other words: 81 | \meeq{ 82 | q(a+b \ensuremath{\epsilon}) = q(a) + b q'(a) \ensuremath{\epsilon} \ccr 83 | r(a+b \ensuremath{\epsilon}) = r(a) + b r'(a) \ensuremath{\epsilon} 84 | } 85 | \end{lemma} 86 | \textbf{Proof} For $q$ it follows immediately: 87 | \meeq{ 88 | q(a + b \ensuremath{\epsilon}) = f(g(a + b \ensuremath{\epsilon})) = f(g(a) + b g'(a) \ensuremath{\epsilon}) \ccr 89 | = f(g(a)) + b g'(a) f'(g(a))\ensuremath{\epsilon} = q(a) + b q'(a) \ensuremath{\epsilon}. 90 | } 91 | For $r$ we have 92 | \meeq{ 93 | r(a + b \ensuremath{\epsilon}) = f(a+b \ensuremath{\epsilon} )g(a+b \ensuremath{\epsilon} )= (f(a) + b f'(a) \ensuremath{\epsilon})(g(a) + b g'(a) \ensuremath{\epsilon}) \ccr 94 | = f(a)g(a) + b (f'(a)g(a) + f(a)g'(a)) \ensuremath{\epsilon} = r(a) +b r'(a) \ensuremath{\epsilon}. 95 | } 96 | \end{definition} 97 | 98 | A simple corollary is that any function defined in terms of addition, multiplication, composition, etc. of functions that are dual with differentiation will be differentiable via dual numbers. 99 | 100 | \begin{example}[differentiating non-polynomial] 101 | 102 | Consider differentiating $f(x) = \exp(x^2 + \E^x)$ at the point $a = 1$ by evaluating on the duals: 103 | \[ 104 | f(1 + \ensuremath{\epsilon}) = \exp(1 + 2\ensuremath{\epsilon} + \E + \E \ensuremath{\epsilon}) = \exp(1 + \E) + \exp(1 + \E) (2 + \E) \ensuremath{\epsilon}. 105 | \] 106 | Therefore we deduce that 107 | \[ 108 | f'(1) = \exp(1 + \E) (2 + \E). 109 | \] 110 | \end{example} 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /notes/I.4.NewtonMethod.tex: -------------------------------------------------------------------------------- 1 | 2 | \section{Newton's method} 3 | In school you may recall learning Newton's method: a way of approximating zeros/roots to a function by using a local approximation by an affine function. That is, approximate a function $f(x)$ locally around an initial guess $x_0$ by its first order Taylor series: 4 | \[ 5 | f(x) \ensuremath{\approx} f(x_0) + f'(x_0) (x-x_0) 6 | \] 7 | and then find the root of the right-hand side which is 8 | \[ 9 | f(x_0) + f'(x_0) (x-x_0) = 0 \ensuremath{\Leftrightarrow} x = x_0 - {f(x_0) \over f'(x_0)}. 10 | \] 11 | We can then repeat using this root as the new initial guess. In other words we have a sequence of \emph{hopefully} more accurate approximations: 12 | \[ 13 | x_{k+1} = x_k - {f(x_k) \over f'(x_k)}. 14 | \] 15 | The convergence theory of Newton's method is rich and beautiful but outside the scope of this module. But provided $f$ is smooth, if $x_0$ is sufficiently close to a root this iteration will converge. 16 | 17 | Thus \emph{if} we can compute derivatives, we can (sometimes) compute roots. The lab will explore using dual numbers to accomplish this task. This is in some sense a baby version of how Machine Learning algorithms train neural networks. 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /notes/II.4.Intervals.tex: -------------------------------------------------------------------------------- 1 | 2 | \section{Interval Arithmetic} 3 | It is possible to use rounding modes (up/down) to do rigorous computation to compute bounds on the error in, for example, the digits of $\E$. To do this we will use set/interval arithmetic. For sets $X,Y \ensuremath{\subseteq} \ensuremath{\bbR}$, the set arithmetic operations are defined as 4 | \begin{align*} 5 | X + Y &:= \{x + y : x \ensuremath{\in} X, y \ensuremath{\in} Y\}, \\ 6 | XY &:= \{xy : x \ensuremath{\in} X, y \ensuremath{\in} Y\}, \\ 7 | X/Y &:= \{x/y : x \ensuremath{\in} X, y \ensuremath{\in} Y\} 8 | \end{align*} 9 | We will use floating point arithmetic to construct approximate set operations $\ensuremath{\oplus}$, $\ensuremath{\otimes}$ so that 10 | \begin{align*} 11 | X + Y &\ensuremath{\subseteq} X \ensuremath{\oplus} Y, \\ 12 | XY &\ensuremath{\subseteq} X \ensuremath{\otimes} Y,\\ 13 | X/Y &\ensuremath{\subseteq} X \ensuremath{\oslash} Y 14 | \end{align*} 15 | thereby a complicated algorithm can be run on sets and the true result is guaranteed to be a subset of the output. 16 | 17 | When our sets are intervals we can deduce simple formulas for basic arithmetic operations. For simplicity we only consider the case where all values are positive. 18 | 19 | \begin{proposition}[interval bounds] For intervals $X = [a,b]$ and $Y = [c,d]$ satisfying $0 < a \ensuremath{\leq} b$ and $0 < c \ensuremath{\leq} d$, and $n > 0$, we have: 20 | \meeq{ 21 | X + Y = [a+c, b+d] \ccr 22 | X/n = [a/n,b/n] \ccr 23 | XY = [ac, bd] 24 | } 25 | \end{proposition} 26 | \textbf{Proof} We first show $X+Y \ensuremath{\subseteq} [a+c,b+d]$. If $z \ensuremath{\in} X + Y$ then $z = x+y$ such that $a \ensuremath{\leq} x \ensuremath{\leq} b$ and $c \ensuremath{\leq} y \ensuremath{\leq} d$ and therefore $a + c \ensuremath{\leq} z \ensuremath{\leq} c + d$ and $z \ensuremath{\in} [a+c,b+d]$. Equality follows from convexity. First note that $a+c, b+d \ensuremath{\in} X+Y$. Any point $z \ensuremath{\in} [a+b,c+d]$ can be written as a convex combination of the two endpoints: there exists $0 \ensuremath{\leq} t \ensuremath{\leq} 1$ such that 27 | \[ 28 | z = (1-t) (a+c) + t (b+d) = \underbrace{(1-t) a + t b}_x + \underbrace{(1-t) c + t d}_y 29 | \] 30 | Because intervals are convex we have $x \ensuremath{\in} X$ and $y \ensuremath{\in} Y$ and hence $z \ensuremath{\in} X+Y$. 31 | 32 | The remaining two proofs are left for the problem sheet. 33 | 34 | \ensuremath{\QED} 35 | 36 | We want to implement floating point variants of these operations that are guaranteed to contain the true set arithmetic operations. We do so as follows: 37 | 38 | \begin{definition}[floating point interval arithmetic] For intervals $A = [a,b]$ and $B = [c,d]$ satisfying $0 < a \ensuremath{\leq} b$ and $0 < c \ensuremath{\leq} d$, and $n > 0$, define: 39 | \begin{align*} 40 | [a,b] \ensuremath{\oplus} [c,d] &:= [\fldown(a+c), \flup(b+d)] \\ 41 | [a,b] \ensuremath{\ominus} [c,d] &:= [\fldown(a-d), \flup(b-c)] \\ 42 | [a,b] \ensuremath{\oslash} n &:= [\fldown(a/n), \flup(b/n)] \\ 43 | [a,b] \ensuremath{\otimes} [c,d] &:= [\fldown(ac), \flup(bd)] 44 | \end{align*} 45 | \end{definition} 46 | 47 | \begin{example}[small sum] consider evaluating the first few terms in the Taylor series of the exponential at $x = 1$ using interval arithmetic with half-precision $F_{16}$ arithmetic. The first three terms are exact since all numbers involved are exactly floats, in particular if we evaluate $1 + x + x^2/2$ with $x = 1$ we get 48 | \[ 49 | 1 + 1 + 1/2 \ensuremath{\in} 1 \ensuremath{\oplus} [1,1] \ensuremath{\oplus} ([1,1] \ensuremath{\otimes} [1,1]) \ensuremath{\oslash} 2 = [5/2, 5/2] 50 | \] 51 | Noting that 52 | \[ 53 | 1/6 = (1/3)/2 = 2^{-3} (1.01010101\ensuremath{\ldots})_2 54 | \] 55 | we can extend the computation to another term: 56 | \begin{align*} 57 | 1 + 1 + 1/2 + 1/6 &\ensuremath{\in} [5/2,5/2] \ensuremath{\oplus} ([1,1] \ensuremath{\oslash} 6) \ccr 58 | = [2 (1.01)_2, 2 (1.01)_2] \ensuremath{\oplus} 2^{-3}[(1.0101010101)_2, (1.0101010110)_2] \ccr 59 | = [\fldown(2 (1.0101010101\red{0101})_2), \flup(2 (1.0101010101\red{011})_2)] \ccr 60 | = [2(1.0101010101)_2, 2(1.0101010110)_2] \ccr 61 | = [2.666015625, 2.66796875] 62 | \end{align*} 63 | \end{example} 64 | 65 | \begin{example}[exponential with intervals] Consider computing $\exp(x)$ for $0 \ensuremath{\leq} x \ensuremath{\leq} 1$ from the Taylor series approximation: 66 | \[ 67 | \exp(x) = \sum_{k=0}^n {x^k \over k!} + \underbrace{\exp(t){x^{n+1} \over (n+1)!}}_{\ensuremath{\delta}_{x,n}} 68 | \] 69 | where we can bound the error by (using the fact that $\ensuremath{\euler} = 2.718\ensuremath{\ldots} \ensuremath{\leq} 3$) 70 | \[ 71 | |\ensuremath{\delta}_{x,n}| \ensuremath{\leq} {\exp(1) \over (n+1)!} \ensuremath{\leq} {3 \over (n+1)!}. 72 | \] 73 | Put another way: $\ensuremath{\delta}_{x,n} \ensuremath{\in} \left[-{3 \over (n+1)!}, {3 \over (n+1)!}\right]$. We can use this to adjust the bounds derived from interval arithmetic for the interval arithmetic expression: 74 | \[ 75 | \exp(X) \ensuremath{\subseteq} \left(\ensuremath{\bigoplus}_{k=0}^n {X \ocaret k \ensuremath{\oslash} k!}\right) \ensuremath{\oplus} \left[\fldown\fpr(-{3 \over (n+1)!}), \flup\fpr({3 \over (n+1)!})\right] 76 | \] 77 | For example, with $n = 3$ we have $|\ensuremath{\delta}_{1,2}| \ensuremath{\leq} 3/4! = 1/2^3$. Thus we can prove that: 78 | \meeq{ 79 | \ensuremath{\euler} = 1 + 1 + 1/2 + 1/6 + \ensuremath{\delta}_x \ensuremath{\in} [2(1.0101010101)_2, 2(1.0101010110)_2] \ensuremath{\oplus} [-1/2^3, 1/2^3] \ccr 80 | = [2(1.0100010101)_2, 2(1.0110010110)_2] = [2.541015625,2.79296875] 81 | } 82 | In the lab we get many more digits by using a computer to compute the bounds. \end{example} 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /notes/notes 2.synctex(busy): -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/notes/notes 2.synctex(busy) -------------------------------------------------------------------------------- /notes/notes 3.synctex(busy): -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/notes/notes 3.synctex(busy) -------------------------------------------------------------------------------- /notes/notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/notes/notes.pdf -------------------------------------------------------------------------------- /notes/somacros.tex: -------------------------------------------------------------------------------- 1 | 2 | \def\addtab#1={#1\;&=} 3 | 4 | \def\meeq#1{\def\ccr{\\\addtab} 5 | %\tabskip=\@centering 6 | \begin{align*} 7 | \addtab#1 8 | \end{align*} 9 | } 10 | 11 | \def\leqaddtab#1\leq{#1\;&\leq} 12 | \def\mleeq#1{\def\ccr{\\\addtab} 13 | %\tabskip=\@centering 14 | \begin{align*} 15 | \leqaddtab#1 16 | \end{align*} 17 | } 18 | 19 | 20 | \def\vc#1{\mbox{\boldmath$#1$\unboldmath}} 21 | 22 | \def\vcsmall#1{\mbox{\boldmath$\scriptstyle #1$\unboldmath}} 23 | 24 | \def\vczero{{\mathbf 0}} 25 | 26 | 27 | %\def\beginlist{\begin{itemize}} 28 | % 29 | %\def\endlist{\end{itemize}} 30 | 31 | 32 | \def\pr(#1){\left({#1}\right)} 33 | \def\br[#1]{\left[{#1}\right]} 34 | \def\fbr[#1]{\!\left[{#1}\right]} 35 | \def\set#1{\left\{{#1}\right\}} 36 | \def\ip<#1>{\left\langle{#1}\right\rangle} 37 | \def\iip<#1>{\left\langle\!\langle{#1}\right\rangle\!\rangle} 38 | 39 | \def\norm#1{\left\| #1 \right\|} 40 | 41 | \def\abs#1{\left|{#1}\right|} 42 | \def\fpr(#1){\!\pr({#1})} 43 | 44 | \def\Re{{\rm Re}\,} 45 | \def\Im{{\rm Im}\,} 46 | 47 | \def\floor#1{\left\lfloor#1\right\rfloor} 48 | \def\ceil#1{\left\lceil#1\right\rceil} 49 | 50 | 51 | \def\mapengine#1,#2.{\mapfunction{#1}\ifx\void#2\else\mapengine #2.\fi } 52 | 53 | \def\map[#1]{\mapengine #1,\void.} 54 | 55 | \def\mapenginesep_#1#2,#3.{\mapfunction{#2}\ifx\void#3\else#1\mapengine #3.\fi } 56 | 57 | \def\mapsep_#1[#2]{\mapenginesep_{#1}#2,\void.} 58 | 59 | 60 | \def\vcbr{\br} 61 | 62 | 63 | \def\bvect[#1,#2]{ 64 | { 65 | \def\dots{\cdots} 66 | \def\mapfunction##1{\ | \ ##1} 67 | \begin{pmatrix} 68 | \,#1\map[#2]\, 69 | \end{pmatrix} 70 | } 71 | } 72 | 73 | \def\vect[#1]{ 74 | {\def\dots{\ldots} 75 | \vcbr[{#1}] 76 | }} 77 | 78 | \def\vectt[#1]{ 79 | {\def\dots{\ldots} 80 | \vect[{#1}]^{\top} 81 | }} 82 | 83 | \def\Vectt[#1]{ 84 | { 85 | \def\mapfunction##1{##1 \cr} 86 | \def\dots{\vdots} 87 | \begin{bmatrix} 88 | \map[#1] 89 | \end{bmatrix} 90 | }} 91 | 92 | 93 | 94 | \def\thetaB{\mbox{\boldmath$\theta$}} 95 | \def\zetaB{\mbox{\boldmath$\zeta$}} 96 | 97 | 98 | \def\newterm#1{{\it #1}\index{#1}} 99 | 100 | 101 | \def\TT{{\mathbb T}} 102 | \def\C{{\mathbb C}} 103 | \def\R{{\mathbb R}} 104 | \def\II{{\mathbb I}} 105 | \def\F{{\mathcal F}} 106 | \def\E{{\rm e}} 107 | \def\I{{\rm i}} 108 | \def\D{{\rm d}} 109 | \def\dx{\D x} 110 | \def\ds{\D s} 111 | \def\dt{\D t} 112 | \def\CC{{\cal C}} 113 | \def\DD{{\cal D}} 114 | \def\U{{\mathbb U}} 115 | \def\A{{\cal A}} 116 | \def\K{{\cal K}} 117 | \def\DTU{{\cal D}_{{\rm T} \rightarrow {\rm U}}} 118 | \def\LL{{\cal L}} 119 | \def\B{{\cal B}} 120 | \def\T{{\cal T}} 121 | \def\W{{\cal W}} 122 | 123 | 124 | \def\tF_#1{{\tt F}_{#1}} 125 | \def\Fm{\tF_m} 126 | \def\Fab{\tF_{\alpha,\beta}} 127 | \def\FC{\T} 128 | \def\FCpmz{\FC^{\pm {\rm z}}} 129 | \def\FCz{\FC^{\rm z}} 130 | 131 | \def\tFC_#1{{\tt T}_{#1}} 132 | \def\FCn{\tFC_n} 133 | 134 | \def\rmz{{\rm z}} 135 | 136 | \def\chapref#1{Chapter~\ref{Chapter:#1}} 137 | \def\secref#1{Section~\ref{Section:#1}} 138 | \def\exref#1{Exercise~\ref{Exercise:#1}} 139 | \def\lmref#1{Lemma~\ref{Lemma:#1}} 140 | \def\propref#1{Proposition~\ref{Proposition:#1}} 141 | \def\warnref#1{Warning~\ref{Warning:#1}} 142 | \def\thref#1{Theorem~\ref{Theorem:#1}} 143 | \def\defref#1{Definition~\ref{Definition:#1}} 144 | \def\probref#1{Problem~\ref{Problem:#1}} 145 | \def\corref#1{Corollary~\ref{Corollary:#1}} 146 | 147 | \def\sgn{{\rm sgn}\,} 148 | \def\Ai{{\rm Ai}\,} 149 | \def\Bi{{\rm Bi}\,} 150 | \def\wind{{\rm wind}\,} 151 | \def\erf{{\rm erf}\,} 152 | \def\erfc{{\rm erfc}\,} 153 | \def\qqquad{\qquad\quad} 154 | \def\qqqquad{\qquad\qquad} 155 | 156 | 157 | \def\spand{\hbox{ and }} 158 | \def\spodd{\hbox{ odd}} 159 | \def\speven{\hbox{ even}} 160 | \def\qand{\quad\hbox{and}\quad} 161 | \def\qqand{\qquad\hbox{and}\qquad} 162 | \def\qfor{\quad\hbox{for}\quad} 163 | \def\qqfor{\qquad\hbox{for}\qquad} 164 | \def\qas{\quad\hbox{as}\quad} 165 | \def\qqas{\qquad\hbox{as}\qquad} 166 | \def\qor{\quad\hbox{or}\quad} 167 | \def\qqor{\qquad\hbox{or}\qquad} 168 | \def\qqwhere{\qquad\hbox{where}\qquad} 169 | 170 | 171 | 172 | %%% Words 173 | 174 | \def\naive{na\"\i ve\xspace} 175 | \def\Jmap{Joukowsky map\xspace} 176 | \def\Mobius{M\"obius\xspace} 177 | \def\Holder{H\"older\xspace} 178 | \def\Mathematica{{\sc Mathematica}\xspace} 179 | \def\apriori{apriori\xspace} 180 | \def\WHf{Weiner--Hopf factorization\xspace} 181 | \def\WHfs{Weiner--Hopf factorizations\xspace} 182 | 183 | \def\Jup{J_\uparrow^{-1}} 184 | \def\Jdown{J_\downarrow^{-1}} 185 | \def\Jin{J_+^{-1}} 186 | \def\Jout{J_-^{-1}} 187 | 188 | 189 | 190 | \def\bD{\D\!\!\!^-} 191 | 192 | 193 | 194 | 195 | \def\questionequals{= \!\!\!\!\!\!{\scriptstyle ? \atop }\,\,\,} 196 | 197 | \def\elll#1{\ell^{\lambda,#1}} 198 | \def\elllp{\ell^{\lambda,p}} 199 | \def\elllRp{\ell^{(\lambda,R),p}} 200 | 201 | 202 | \def\elllRpz_#1{\ell_{#1{\rm z}}^{(\lambda,R),p}} 203 | 204 | 205 | \def\sopmatrix#1{\begin{pmatrix}#1\end{pmatrix}} 206 | 207 | 208 | \def\bbR{{\mathbb R}} 209 | \def\bbC{{\mathbb C}} 210 | -------------------------------------------------------------------------------- /sheets/figures/sheet6s_5_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/figures/sheet6s_5_1.pdf -------------------------------------------------------------------------------- /sheets/sheet1.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 1** 2 | 3 | 4 | 5 | ------ 6 | 7 | **Problem 1** Assuming $f$ is differentiable, prove the left-point Rectangular rule error formula 8 | $$ 9 | ∫_a^b f(x) {\rm d}x = h ∑_{j=0}^{n-1} f(x_j) + δ 10 | $$ 11 | where $|δ| ≤ M (b-a) h$ for $M = \sup_{a ≤ x ≤ b}|f'(x)|$, $h = (b-a)/n$ and $x_j = a + jh$. 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | **Problem 2(a)** Assuming $f$ is twice-differentiable, prove a one-panel Trapezium rule error bound: 20 | $$ 21 | ∫_a^b f(x) {\rm d}x = (b-a) {f(a) + f(b) \over 2} + δ 22 | $$ 23 | where $|δ| ≤ M (b-a)^3$ for $M = \sup_{a ≤ x ≤ b}|f''(x)|$. 24 | 25 | _Hint_: Recall from the notes 26 | $$ 27 | ∫_a^b {(b-x) f(a) + (x-a) f(b) \over b-a} \dx = (b-a) {f(a) + f(b) \over 2} 28 | $$ 29 | and you may need to use Taylor's theorem. Note that the bound is not sharp and so you may arrive at something sharper like $|δ| ≤ 3(b-a)^3 M/4$. 30 | The sharpest bound is $|δ| ≤ (b-a)^3 M/12$ but that would be a significantly harder challenge to show! 31 | 32 | 33 | 34 | 35 | **Problem 2(b)** Assuming $f$ is twice-differentiable, prove a bound for the Trapezium rule error: 36 | $$ 37 | ∫_a^b f(x) {\rm d}x = h \br[{f(a) \over 2} + ∑_{j=1}^{n-1} f(x_j) + {f(b) \over 2}] + δ 38 | $$ 39 | where $|δ| ≤ M (b-a) h^2$ for $M = \sup_{a ≤ x ≤ b}|f''(x)|$. 40 | 41 | 42 | 43 | 44 | ----- 45 | 46 | **Problem 3** Assuming $f$ is twice-differentiable, 47 | for the left difference approximation 48 | $$ 49 | f'(x) = {f(x) - f(x - h) \over h} + δ, 50 | $$ 51 | show that $|δ| ≤ Mh/2$ for $M = \sup_{x-h ≤ t ≤ x}\abs{f''(t)}$. 52 | 53 | 54 | 55 | 56 | 57 | 58 | **Problem 4** Assuming $f$ is thrice-differentiable, 59 | for the central differences approximation 60 | $$ 61 | f'(x) = {f(x + h) - f(x - h) \over 2h} + δ, 62 | $$ 63 | show that $|δ| ≤ Mh^2/6$ for $M = \sup_{x-h ≤ t ≤ x+h}\abs{f'''(t)}$. 64 | 65 | 66 | 67 | 68 | 69 | 70 | **Problem 5** Assuming $f$ is thrice-differentiable, 71 | for the second-order derivative approximation 72 | $$ 73 | {f(x+h) - 2f(x) + f(x-h) \over h^2} = f''(x) + δ 74 | $$ 75 | show that $|δ| ≤ Mh/3$ for $M = \sup_{x-h ≤ t ≤ x+h}\abs{f'''(t)}$. 76 | 77 | -------------------------------------------------------------------------------- /sheets/sheet1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet1.pdf -------------------------------------------------------------------------------- /sheets/sheet10.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 10** 2 | 3 | 4 | 5 | **Problem 1** What are the upper $3 × 3$ sub-block of the multiplication matrix $X$ / Jacobi matrix $J$ for the 6 | monic and orthonormal polynomials with respect to the following weights on 7 | $[-1,1]$: 8 | $$ 9 | 1-x, \sqrt{1-x^2}, 1-x^2 10 | $$ 11 | 12 | 13 | 14 | 15 | **Problem 2** Compute the roots of the Legendre polynomial $P_3(x)$, orthogonal with respect 16 | to $w(x) = 1$ on $[-1,1]$, by computing the eigenvalues of a $3 × 3$ truncation 17 | of the Jacobi matrix. 18 | 19 | 20 | 21 | 22 | 23 | **Problem 3** Compute the interpolatory quadrature rule for 24 | $w(x) = \sqrt{1-x^2}$ with the points $[-1,1/2,1]$. 25 | 26 | 27 | 28 | **Problem 4** Compute the 2-point 29 | interpolatory quadrature rule associated with roots of orthogonal polynomials for the weights $\sqrt{1-x^2}$, $1$, 30 | and $1-x$ on $[-1,1]$ by integrating the Lagrange bases. 31 | 32 | 33 | 34 | 35 | 36 | **Problem 5(a)** For the matrix 37 | $$ 38 | J_n = \begin{bmatrix} 0 & 1/\sqrt{2} \\ 39 | 1/\sqrt{2} & 0 & 1/2 \\ 40 | & 1/2 & 0 & ⋱ \\ 41 | && ⋱ & ⋱ & 1/2 \\ 42 | &&& 1/2 & 0 43 | \end{bmatrix} ∈ ℝ^{n × n} 44 | $$ 45 | use the relationship with the Jacobi matrix associated with $T_n(x)$ to prove that, for $x_j = \cos θ_j$, and $θ_j = (n-j+1/2)π/n$, 46 | $$ 47 | J_n = Q_n \begin{bmatrix} x_1 \\ & ⋱ \\ && x_n \end{bmatrix} Q_n^⊤ 48 | $$ 49 | where 50 | $$ 51 | \meeq{ 52 | 𝐞_1^⊤ Q_n 𝐞_j = {1 \over \sqrt{n}}, \qquad 53 | 𝐞_k^⊤ Q_n 𝐞_j = \sqrt{2 \over n} \cos (k-1) θ_j. 54 | } 55 | $$ 56 | You may use without proof the sums-of-squares formula 57 | $$ 58 | 1 + 2 ∑_{k=1}^{n-1} \cos^2 k θ_j = n. 59 | $$ 60 | 61 | 62 | 63 | 64 | **Problem 5(b)** Show for $w(x) = 1/\sqrt{1-x^2}$ that the Gaussian quadrature rule is 65 | $$ 66 | Q_n^w[f] = {π \over n} \sum_{j=1}^n f(x_j) 67 | $$ 68 | where $x_j = \cos θ_j$ for $θ_j = (j-1/2)π/n$. 69 | 70 | 71 | 72 | **Problem 5(c)** Give an explicit formula for the polynomial that interpolates $\exp x$ at the points $x_1,…,x_n$ as defined above, in terms of Chebyshev polynomials 73 | with the coefficients defined in terms of a sum involving only exponentials, cosines and $θ_j = (n-j+1/2)π/n$. 74 | 75 | -------------------------------------------------------------------------------- /sheets/sheet10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet10.pdf -------------------------------------------------------------------------------- /sheets/sheet10s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet10s.pdf -------------------------------------------------------------------------------- /sheets/sheet11.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Revision Sheet** 2 | 3 | 4 | **Problem 1(a)** State which real number is represented by an IEEE 16-bit floating point number (with $σ = 15, Q = 5$, and $S = 10$) with bits 5 | $$ 6 | {\tt 1\ 01000\ 0000000001} 7 | $$ 8 | 9 | 10 | 11 | **Problem 1(b)** How are the following real numbers rounded to the nearest $F_{16}$? 12 | $$ 13 | 1/2, 1/2 + 2^{-12}, 3 + 2^{-9} + 2^{-10}, 3 + 2^{-10} + 2^{-11}. 14 | $$ 15 | 16 | 17 | 18 | **Problem 2(a)** Consider a Lower triangular matrix with floating point entries: 19 | $$ 20 | L = \begin{bmatrix} 21 | ℓ_{11} \\ 22 | ℓ_{21} & ℓ_{22} \\ 23 | ⋮ & ⋱ & ⋱ \\ 24 | ℓ_{n1} & ⋯ & ℓ_{n,n-1} & ℓ_{nn} 25 | \end{bmatrix} ∈ F_{σ,Q,S}^{n × n} 26 | $$ 27 | and a vector $𝐱 \in F_{σ,Q,S}^{n}$, where $F_{σ,Q,S}$ is a set of floating-point numbers. 28 | Denoting matrix-vector multiplication implemented using floating point arithmetic as 29 | $$ 30 | 𝐛 := {\tt lowermul}(L,𝐱) 31 | $$ 32 | express the entries $b_k := {\bf e}_k^⊤ 𝐛$ in terms of $ℓ_{kj}$ and $x_k := {\bf e}_k^⊤ 𝐱$, 33 | using rounded floating-point operations $⊕$ and $⊗$. 34 | 35 | 36 | 37 | **Problem 2(b)** Assuming all operations involve normal floating numbers, show that your approximation has the form 38 | $$ 39 | L 𝐱 = {\tt lowermul}(L, 𝐱) + 𝛜 40 | $$ 41 | where, for $ϵ_{\rm m}$ denoting machine epsilon and $E_{n,ϵ}:= {n ϵ \over 1-nϵ}$ and assuming $n ϵ_{\rm m} < 2$, 42 | $$ 43 | \| 𝛜 \|_1 ≤ 2E_{n,ϵ_{\rm m}/2} \|L\|_1 \| 𝐱 \|_1. 44 | $$ 45 | Here we use the matrix norm $\| A \|_1 := \max_j ∑_{k=1}^n |a_{kj}|$ 46 | and the vector norm $\| 𝐱 \|_1 := ∑_{k=1}^n |x_k|$. You may use the fact that 47 | $$ 48 | x_1 ⊕ ⋯ ⊕ x_n = x_1 + ⋯ + x_n + σ_n 49 | $$ 50 | where 51 | $$ 52 | |σ_n| ≤ \| 𝐱 \|_1 E_{n-1,ϵ_{\rm m}/2}. 53 | $$ 54 | 55 | 56 | 57 | 58 | 59 | 60 | **Problem 3** What is the dual extension of square-roots? I.e. what should $\sqrt{a + b ϵ}$ equal assuming $a > 0$? 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | **Problem 4** Use the Cholesky factorisation to determine 69 | whether the following matrix is symmetric positive definite: 70 | $$ 71 | \begin{bmatrix} 2 & 2 & 1 \\ 72 | 2 & 3 & 2\\ 73 | 1 & 2 & 2 74 | \end{bmatrix} 75 | $$ 76 | 77 | 78 | 79 | **Problem 5** Use reflections to determine the entries of an orthogonal matrix $Q$ such that 80 | $$ 81 | Q \begin{bmatrix} 2 \\ 1 \\ 2 \end{bmatrix} = \begin{bmatrix} -3 \\ 0 \\ 0 \end{bmatrix}. 82 | $$ 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | **Problem 6** For the function $f(θ) = \sin 3 θ$, state explicit formulae for its Fourier coefficients 91 | $$ 92 | \hat f_k := {1 \over 2π} \int_0^{2π} f(θ) {\rm e}^{-{\rm i} k θ} {\rm d}θ 93 | $$ 94 | and their discrete approximation: 95 | $$ 96 | \hat f_k^n := {1 \over n} \sum_{j=0}^{n-1} f(θ_j) {\rm e}^{-{\rm i} k θ_j}. 97 | $$ 98 | for _all_ integers $k$, $n = 1,2,…$, where $θ_j = 2π j/n$. 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | **Problem 7** Consider orthogonal polynomials 108 | $$ 109 | H_n(x) = 2^n x^n + O (x^{n-1}) 110 | $$ 111 | as $x → ∞$ and $n = 0, 1, 2, …$, orthogonal with respect to the inner product 112 | $$ 113 | \langle f, g \rangle = \int_{-∞}^∞ f(x) g(x) w(x) {\rm d}x, \qquad w(x) = \exp(-x^2) 114 | $$ 115 | Construct $H_0(x)$, $H_1(x)$, $H_2(x)$ and hence show that $H_3(x) = 8x^3-12x$. You may use without proof the formulae 116 | $$ 117 | \int_{-∞}^∞ w(x) {\rm d}x = \sqrt{π}, \int_{-∞}^∞ x^2 w(x) {\rm d}x = \sqrt{π}/2, 118 | \int_{-∞}^∞ x^4 w(x) {\rm d}x = 3\sqrt{π}/4. 119 | $$ 120 | 121 | 122 | 123 | 124 | 125 | **Problem 8(a)** Derive the 3-point Gauss quadrature formula 126 | $$ 127 | \int_{-∞}^∞ f(x) \exp(-x^2) {\rm d}x ≈ w_1 f(x_1) + w_2 f(x_2) + w_3 f(x_3) 128 | $$ 129 | with analytic expressions for $x_j$ and $w_j$. 130 | 131 | 132 | 133 | 134 | 135 | **Problem 8(b)** Compute the 2-point and 3-point Gaussian quadrature rules associated with $w(x) = 1$ on $[-1,1]$. 136 | 137 | 138 | 139 | 140 | 141 | **Problem 9** Solve Problem 4(b) from PS8 using **Lemma 12 (discrete orthogonality)** with 142 | $w(x) = 1/\sqrt{1-x^2}$ on $[-1,1]$. That is, use the connection of $T_n(x)$ with $\cos n θ$ to 143 | show that the Discrete Cosine Transform 144 | $$ 145 | C_n := \begin{bmatrix} 146 | \sqrt{1/n} \\ 147 | & \sqrt{2/n} \\ 148 | && ⋱ \\ 149 | &&& \sqrt{2/n} 150 | \end{bmatrix} 151 | \begin{bmatrix} 152 | 1 & ⋯ & 1\\ 153 | \cos θ_1 & ⋯ & \cos θ_n \\ 154 | ⋮ & ⋱ & ⋮ \\ 155 | \cos (n-1)θ_1 & ⋯ & \cos (n-1)θ_n 156 | \end{bmatrix} 157 | $$ 158 | for $θ_j = π(j-1/2)/n$ is an orthogonal matrix. 159 | 160 | 161 | -------------------------------------------------------------------------------- /sheets/sheet11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet11.pdf -------------------------------------------------------------------------------- /sheets/sheet11s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet11s.pdf -------------------------------------------------------------------------------- /sheets/sheet1s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet1s.pdf -------------------------------------------------------------------------------- /sheets/sheet2.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 2** 2 | 3 | **Problem 1** Using dual number arithmetic, compute the following polynomials evaluated at the 4 | dual number $2+ϵ$ and use this to deduce their derivative at $2$: 5 | $$ 6 | 2x^2 + 3x + 4, (x+1)(x+2)(x+3), (2x+1)x^3 7 | $$ 8 | 9 | 10 | 11 | 12 | 13 | **Problem 2** What should the following functions applied to dual numbers return for $x = a+b ϵ$: 14 | $$ 15 | f(x) = x^{100} + 1, g(x) = 1/x, h(x) = \tan x 16 | $$ 17 | State the domain where these definitions are valid. 18 | 19 | 20 | 21 | 22 | **Problem 3(a)** What is the correct definition of division on dual numbers, i.e., 23 | $$ 24 | (a + b ϵ )/(c + d ϵ ) = s + t ϵ 25 | $$ 26 | for what choice of $s$ and $t$? 27 | 28 | 29 | 30 | **Problem 3(b)** A _field_ is a commutative ring such that $0 ≠ 1$ and all nonzero elements have a multiplicative inverse, i.e., 31 | there exists $a^{-1}$ such that $a a^{-1} = 1$. Can we use the previous part to define $a^{-1} := 1/a$ to make $𝔻$ a field? Why or why not? 32 | 33 | 34 | 35 | 36 | 37 | **Problem 4** Use dual numbers to compute the derivative of the following functions at $x = 0.1$: 38 | $$ 39 | \exp(\exp x \cos x + \sin x), \prod_{k=1}^3 \left({x \over k}-1\right),\hbox{ and } f^{\rm s}_2(x) = {1 + {x - 1 \over 2 + {x-1 \over 2}}} 40 | $$ 41 | 42 | 43 | -------------------------------------------------------------------------------- /sheets/sheet2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet2.pdf -------------------------------------------------------------------------------- /sheets/sheet2.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,a4paper]{article} 2 | 3 | \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry} 4 | \usepackage{lmodern} 5 | \usepackage{amssymb,amsmath} 6 | \usepackage{bm} 7 | \usepackage{graphicx} 8 | \usepackage{microtype} 9 | \usepackage{hyperref} 10 | \usepackage[usenames,dvipsnames]{xcolor} 11 | \setlength{\parindent}{0pt} 12 | \setlength{\parskip}{1.2ex} 13 | 14 | 15 | 16 | 17 | \hypersetup 18 | { pdfauthor = { }, 19 | pdftitle={ }, 20 | colorlinks=TRUE, 21 | linkcolor=black, 22 | citecolor=blue, 23 | urlcolor=blue 24 | } 25 | 26 | 27 | 28 | 29 | \usepackage{upquote} 30 | \usepackage{listings} 31 | \usepackage{xcolor} 32 | \lstset{ 33 | basicstyle=\ttfamily\footnotesize, 34 | upquote=true, 35 | breaklines=true, 36 | breakindent=0pt, 37 | keepspaces=true, 38 | showspaces=false, 39 | columns=fullflexible, 40 | showtabs=false, 41 | showstringspaces=false, 42 | escapeinside={(*@}{@*)}, 43 | extendedchars=true, 44 | } 45 | \newcommand{\HLJLt}[1]{#1} 46 | \newcommand{\HLJLw}[1]{#1} 47 | \newcommand{\HLJLe}[1]{#1} 48 | \newcommand{\HLJLeB}[1]{#1} 49 | \newcommand{\HLJLo}[1]{#1} 50 | \newcommand{\HLJLk}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 51 | \newcommand{\HLJLkc}[1]{\textcolor[RGB]{59,151,46}{\textit{#1}}} 52 | \newcommand{\HLJLkd}[1]{\textcolor[RGB]{214,102,97}{\textit{#1}}} 53 | \newcommand{\HLJLkn}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 54 | \newcommand{\HLJLkp}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 55 | \newcommand{\HLJLkr}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 56 | \newcommand{\HLJLkt}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 57 | \newcommand{\HLJLn}[1]{#1} 58 | \newcommand{\HLJLna}[1]{#1} 59 | \newcommand{\HLJLnb}[1]{#1} 60 | \newcommand{\HLJLnbp}[1]{#1} 61 | \newcommand{\HLJLnc}[1]{#1} 62 | \newcommand{\HLJLncB}[1]{#1} 63 | \newcommand{\HLJLnd}[1]{\textcolor[RGB]{214,102,97}{#1}} 64 | \newcommand{\HLJLne}[1]{#1} 65 | \newcommand{\HLJLneB}[1]{#1} 66 | \newcommand{\HLJLnf}[1]{\textcolor[RGB]{66,102,213}{#1}} 67 | \newcommand{\HLJLnfm}[1]{\textcolor[RGB]{66,102,213}{#1}} 68 | \newcommand{\HLJLnp}[1]{#1} 69 | \newcommand{\HLJLnl}[1]{#1} 70 | \newcommand{\HLJLnn}[1]{#1} 71 | \newcommand{\HLJLno}[1]{#1} 72 | \newcommand{\HLJLnt}[1]{#1} 73 | \newcommand{\HLJLnv}[1]{#1} 74 | \newcommand{\HLJLnvc}[1]{#1} 75 | \newcommand{\HLJLnvg}[1]{#1} 76 | \newcommand{\HLJLnvi}[1]{#1} 77 | \newcommand{\HLJLnvm}[1]{#1} 78 | \newcommand{\HLJLl}[1]{#1} 79 | \newcommand{\HLJLld}[1]{\textcolor[RGB]{148,91,176}{\textit{#1}}} 80 | \newcommand{\HLJLs}[1]{\textcolor[RGB]{201,61,57}{#1}} 81 | \newcommand{\HLJLsa}[1]{\textcolor[RGB]{201,61,57}{#1}} 82 | \newcommand{\HLJLsb}[1]{\textcolor[RGB]{201,61,57}{#1}} 83 | \newcommand{\HLJLsc}[1]{\textcolor[RGB]{201,61,57}{#1}} 84 | \newcommand{\HLJLsd}[1]{\textcolor[RGB]{201,61,57}{#1}} 85 | \newcommand{\HLJLsdB}[1]{\textcolor[RGB]{201,61,57}{#1}} 86 | \newcommand{\HLJLsdC}[1]{\textcolor[RGB]{201,61,57}{#1}} 87 | \newcommand{\HLJLse}[1]{\textcolor[RGB]{59,151,46}{#1}} 88 | \newcommand{\HLJLsh}[1]{\textcolor[RGB]{201,61,57}{#1}} 89 | \newcommand{\HLJLsi}[1]{#1} 90 | \newcommand{\HLJLso}[1]{\textcolor[RGB]{201,61,57}{#1}} 91 | \newcommand{\HLJLsr}[1]{\textcolor[RGB]{201,61,57}{#1}} 92 | \newcommand{\HLJLss}[1]{\textcolor[RGB]{201,61,57}{#1}} 93 | \newcommand{\HLJLssB}[1]{\textcolor[RGB]{201,61,57}{#1}} 94 | \newcommand{\HLJLnB}[1]{\textcolor[RGB]{59,151,46}{#1}} 95 | \newcommand{\HLJLnbB}[1]{\textcolor[RGB]{59,151,46}{#1}} 96 | \newcommand{\HLJLnfB}[1]{\textcolor[RGB]{59,151,46}{#1}} 97 | \newcommand{\HLJLnh}[1]{\textcolor[RGB]{59,151,46}{#1}} 98 | \newcommand{\HLJLni}[1]{\textcolor[RGB]{59,151,46}{#1}} 99 | \newcommand{\HLJLnil}[1]{\textcolor[RGB]{59,151,46}{#1}} 100 | \newcommand{\HLJLnoB}[1]{\textcolor[RGB]{59,151,46}{#1}} 101 | \newcommand{\HLJLoB}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 102 | \newcommand{\HLJLow}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 103 | \newcommand{\HLJLp}[1]{#1} 104 | \newcommand{\HLJLc}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 105 | \newcommand{\HLJLch}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 106 | \newcommand{\HLJLcm}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 107 | \newcommand{\HLJLcp}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 108 | \newcommand{\HLJLcpB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 109 | \newcommand{\HLJLcs}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 110 | \newcommand{\HLJLcsB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 111 | \newcommand{\HLJLg}[1]{#1} 112 | \newcommand{\HLJLgd}[1]{#1} 113 | \newcommand{\HLJLge}[1]{#1} 114 | \newcommand{\HLJLgeB}[1]{#1} 115 | \newcommand{\HLJLgh}[1]{#1} 116 | \newcommand{\HLJLgi}[1]{#1} 117 | \newcommand{\HLJLgo}[1]{#1} 118 | \newcommand{\HLJLgp}[1]{#1} 119 | \newcommand{\HLJLgs}[1]{#1} 120 | \newcommand{\HLJLgsB}[1]{#1} 121 | \newcommand{\HLJLgt}[1]{#1} 122 | 123 | 124 | \def\endash{–} 125 | \def\bbD{ {\mathbb D} } 126 | \def\bbZ{ {\mathbb Z} } 127 | \def\bbR{ {\mathbb R} } 128 | \def\bbC{ {\mathbb C} } 129 | 130 | \def\x{ {\vc x} } 131 | \def\a{ {\vc a} } 132 | \def\b{ {\vc b} } 133 | \def\e{ {\vc e} } 134 | \def\f{ {\vc f} } 135 | \def\u{ {\vc u} } 136 | \def\v{ {\vc v} } 137 | \def\y{ {\vc y} } 138 | \def\z{ {\vc z} } 139 | 140 | \def\red#1{ {\color{red} #1} } 141 | \def\blue#1{ {\color{blue} #1} } 142 | \def\green#1{ {\color{ForestGreen} #1} } 143 | \def\magenta#1{ {\color{magenta} #1} } 144 | 145 | \input{somacros} 146 | 147 | \begin{document} 148 | 149 | 150 | 151 | \textbf{Numerical Analysis MATH50003 (2023\ensuremath{\endash}24) Problem Sheet 2} 152 | 153 | \textbf{Problem 1} Using dual number arithmetic, compute the following polynomials evaluated at the dual number $2+\ensuremath{\epsilon}$ and use this to deduce their derivative at $2$: 154 | \[ 155 | 2x^2 + 3x + 4, (x+1)(x+2)(x+3), (2x+1)x^3 156 | \] 157 | \textbf{Problem 2} What should the following functions applied to dual numbers return for $x = a+b \ensuremath{\epsilon}$: 158 | \[ 159 | f(x) = x^{100} + 1, g(x) = 1/x, h(x) = \tan x 160 | \] 161 | State the domain where these definitions are valid. 162 | 163 | \textbf{Problem 3(a)} What is the correct definition of division on dual numbers, i.e., 164 | \[ 165 | (a + b \ensuremath{\epsilon} )/(c + d \ensuremath{\epsilon} ) = s + t \ensuremath{\epsilon} 166 | \] 167 | for what choice of $s$ and $t$? 168 | 169 | \textbf{Problem 3(b)} A \emph{field} is a commutative ring such that $0 \ensuremath{\neq} 1$ and all nonzero elements have a multiplicative inverse, i.e., there exists $a^{-1}$ such that $a a^{-1} = 1$. Can we use the previous part to define $a^{-1} := 1/a$ to make $\ensuremath{\bbD}$ a field? Why or why not? 170 | 171 | \textbf{Problem 4} Use dual numbers to compute the derivative of the following functions at $x = 0.1$: 172 | \[ 173 | \exp(\exp x \cos x + \sin x), \prod_{k=1}^3 \left({x \over k}-1\right),\hbox{ and } f^{\rm s}_2(x) = {1 + {x - 1 \over 2 + {x-1 \over 2}}} 174 | \] 175 | 176 | 177 | \end{document} -------------------------------------------------------------------------------- /sheets/sheet2s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet2s.pdf -------------------------------------------------------------------------------- /sheets/sheet3.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 3** 2 | 3 | **Problem 1** With 8-bit unsigned integers, what is the result for the following computations: 4 | $$ 5 | 127 ⊕_{256} 200,\quad 2 ⊗_{256} 127,\quad 2 ⊗_{256} 128, \quad 0 ⊖_{256} 1 6 | $$ 7 | 8 | 9 | 10 | **Problem 2(a)** With 8-bit signed integers, what are the bits for the following: $10, 120, -10$. 11 | 12 | 13 | 14 | **Problem 2(b)** With 8-bit signed integers, what is the result for the following computations: 15 | $$ 16 | 127 ⊕_{256}^{\rm s} 200,\quad 2 ⊗_{256}^{\rm s} 127, \quad 2 ⊗_{256}^{\rm s} 128, \quad 0 ⊖_{256}^{\rm s} 1 17 | $$ 18 | 19 | 20 | 21 | 22 | **Problem 3** What is $π$ to 5 binary places? Hint: recall that $π ≈ 3.14$. 23 | 24 | 25 | 26 | **Problem 4** What are the single precision $F_{32} = F_{127,8,23}$ floating point representations for the following: 27 | $$ 28 | 2, \quad 31, \quad 32, \quad 23/4, \quad (23/4)\times 2^{100} 29 | $$ 30 | 31 | 32 | 33 | 34 | **Problem 5** Let $m(y) = \min\{x \in F_{32} : x > y \}$ be the smallest single precision number 35 | greater than $y$. What is $m(2) - 2$ and $m(1024) - 1024$? 36 | 37 | 38 | -------------------------------------------------------------------------------- /sheets/sheet3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet3.pdf -------------------------------------------------------------------------------- /sheets/sheet3.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,a4paper]{article} 2 | 3 | \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry} 4 | \usepackage{lmodern} 5 | \usepackage{amssymb,amsmath} 6 | \usepackage{bm} 7 | \usepackage{graphicx} 8 | \usepackage{microtype} 9 | \usepackage{hyperref} 10 | \usepackage[usenames,dvipsnames]{xcolor} 11 | \setlength{\parindent}{0pt} 12 | \setlength{\parskip}{1.2ex} 13 | 14 | 15 | 16 | 17 | \hypersetup 18 | { pdfauthor = { }, 19 | pdftitle={ }, 20 | colorlinks=TRUE, 21 | linkcolor=black, 22 | citecolor=blue, 23 | urlcolor=blue 24 | } 25 | 26 | 27 | 28 | 29 | \usepackage{upquote} 30 | \usepackage{listings} 31 | \usepackage{xcolor} 32 | \lstset{ 33 | basicstyle=\ttfamily\footnotesize, 34 | upquote=true, 35 | breaklines=true, 36 | breakindent=0pt, 37 | keepspaces=true, 38 | showspaces=false, 39 | columns=fullflexible, 40 | showtabs=false, 41 | showstringspaces=false, 42 | escapeinside={(*@}{@*)}, 43 | extendedchars=true, 44 | } 45 | \newcommand{\HLJLt}[1]{#1} 46 | \newcommand{\HLJLw}[1]{#1} 47 | \newcommand{\HLJLe}[1]{#1} 48 | \newcommand{\HLJLeB}[1]{#1} 49 | \newcommand{\HLJLo}[1]{#1} 50 | \newcommand{\HLJLk}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 51 | \newcommand{\HLJLkc}[1]{\textcolor[RGB]{59,151,46}{\textit{#1}}} 52 | \newcommand{\HLJLkd}[1]{\textcolor[RGB]{214,102,97}{\textit{#1}}} 53 | \newcommand{\HLJLkn}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 54 | \newcommand{\HLJLkp}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 55 | \newcommand{\HLJLkr}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 56 | \newcommand{\HLJLkt}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 57 | \newcommand{\HLJLn}[1]{#1} 58 | \newcommand{\HLJLna}[1]{#1} 59 | \newcommand{\HLJLnb}[1]{#1} 60 | \newcommand{\HLJLnbp}[1]{#1} 61 | \newcommand{\HLJLnc}[1]{#1} 62 | \newcommand{\HLJLncB}[1]{#1} 63 | \newcommand{\HLJLnd}[1]{\textcolor[RGB]{214,102,97}{#1}} 64 | \newcommand{\HLJLne}[1]{#1} 65 | \newcommand{\HLJLneB}[1]{#1} 66 | \newcommand{\HLJLnf}[1]{\textcolor[RGB]{66,102,213}{#1}} 67 | \newcommand{\HLJLnfm}[1]{\textcolor[RGB]{66,102,213}{#1}} 68 | \newcommand{\HLJLnp}[1]{#1} 69 | \newcommand{\HLJLnl}[1]{#1} 70 | \newcommand{\HLJLnn}[1]{#1} 71 | \newcommand{\HLJLno}[1]{#1} 72 | \newcommand{\HLJLnt}[1]{#1} 73 | \newcommand{\HLJLnv}[1]{#1} 74 | \newcommand{\HLJLnvc}[1]{#1} 75 | \newcommand{\HLJLnvg}[1]{#1} 76 | \newcommand{\HLJLnvi}[1]{#1} 77 | \newcommand{\HLJLnvm}[1]{#1} 78 | \newcommand{\HLJLl}[1]{#1} 79 | \newcommand{\HLJLld}[1]{\textcolor[RGB]{148,91,176}{\textit{#1}}} 80 | \newcommand{\HLJLs}[1]{\textcolor[RGB]{201,61,57}{#1}} 81 | \newcommand{\HLJLsa}[1]{\textcolor[RGB]{201,61,57}{#1}} 82 | \newcommand{\HLJLsb}[1]{\textcolor[RGB]{201,61,57}{#1}} 83 | \newcommand{\HLJLsc}[1]{\textcolor[RGB]{201,61,57}{#1}} 84 | \newcommand{\HLJLsd}[1]{\textcolor[RGB]{201,61,57}{#1}} 85 | \newcommand{\HLJLsdB}[1]{\textcolor[RGB]{201,61,57}{#1}} 86 | \newcommand{\HLJLsdC}[1]{\textcolor[RGB]{201,61,57}{#1}} 87 | \newcommand{\HLJLse}[1]{\textcolor[RGB]{59,151,46}{#1}} 88 | \newcommand{\HLJLsh}[1]{\textcolor[RGB]{201,61,57}{#1}} 89 | \newcommand{\HLJLsi}[1]{#1} 90 | \newcommand{\HLJLso}[1]{\textcolor[RGB]{201,61,57}{#1}} 91 | \newcommand{\HLJLsr}[1]{\textcolor[RGB]{201,61,57}{#1}} 92 | \newcommand{\HLJLss}[1]{\textcolor[RGB]{201,61,57}{#1}} 93 | \newcommand{\HLJLssB}[1]{\textcolor[RGB]{201,61,57}{#1}} 94 | \newcommand{\HLJLnB}[1]{\textcolor[RGB]{59,151,46}{#1}} 95 | \newcommand{\HLJLnbB}[1]{\textcolor[RGB]{59,151,46}{#1}} 96 | \newcommand{\HLJLnfB}[1]{\textcolor[RGB]{59,151,46}{#1}} 97 | \newcommand{\HLJLnh}[1]{\textcolor[RGB]{59,151,46}{#1}} 98 | \newcommand{\HLJLni}[1]{\textcolor[RGB]{59,151,46}{#1}} 99 | \newcommand{\HLJLnil}[1]{\textcolor[RGB]{59,151,46}{#1}} 100 | \newcommand{\HLJLnoB}[1]{\textcolor[RGB]{59,151,46}{#1}} 101 | \newcommand{\HLJLoB}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 102 | \newcommand{\HLJLow}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 103 | \newcommand{\HLJLp}[1]{#1} 104 | \newcommand{\HLJLc}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 105 | \newcommand{\HLJLch}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 106 | \newcommand{\HLJLcm}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 107 | \newcommand{\HLJLcp}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 108 | \newcommand{\HLJLcpB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 109 | \newcommand{\HLJLcs}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 110 | \newcommand{\HLJLcsB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 111 | \newcommand{\HLJLg}[1]{#1} 112 | \newcommand{\HLJLgd}[1]{#1} 113 | \newcommand{\HLJLge}[1]{#1} 114 | \newcommand{\HLJLgeB}[1]{#1} 115 | \newcommand{\HLJLgh}[1]{#1} 116 | \newcommand{\HLJLgi}[1]{#1} 117 | \newcommand{\HLJLgo}[1]{#1} 118 | \newcommand{\HLJLgp}[1]{#1} 119 | \newcommand{\HLJLgs}[1]{#1} 120 | \newcommand{\HLJLgsB}[1]{#1} 121 | \newcommand{\HLJLgt}[1]{#1} 122 | 123 | 124 | \def\endash{–} 125 | \def\bbD{ {\mathbb D} } 126 | \def\bbZ{ {\mathbb Z} } 127 | \def\bbR{ {\mathbb R} } 128 | \def\bbC{ {\mathbb C} } 129 | 130 | \def\x{ {\vc x} } 131 | \def\a{ {\vc a} } 132 | \def\b{ {\vc b} } 133 | \def\e{ {\vc e} } 134 | \def\f{ {\vc f} } 135 | \def\u{ {\vc u} } 136 | \def\v{ {\vc v} } 137 | \def\y{ {\vc y} } 138 | \def\z{ {\vc z} } 139 | 140 | \def\red#1{ {\color{red} #1} } 141 | \def\blue#1{ {\color{blue} #1} } 142 | \def\green#1{ {\color{ForestGreen} #1} } 143 | \def\magenta#1{ {\color{magenta} #1} } 144 | 145 | \input{somacros} 146 | 147 | \begin{document} 148 | 149 | 150 | 151 | \textbf{Numerical Analysis MATH50003 (2023\ensuremath{\endash}24) Problem Sheet 3} 152 | 153 | \textbf{Problem 1} With 8-bit unsigned integers, what is the result for the following computations: 154 | \[ 155 | 127 \ensuremath{\oplus}_{256} 200,\quad 2 \ensuremath{\otimes}_{256} 127,\quad 2 \ensuremath{\otimes}_{256} 128, \quad 0 \ensuremath{\ominus}_{256} 1 156 | \] 157 | \textbf{Problem 2(a)} With 8-bit signed integers, what are the bits for the following: $10, 120, -10$. 158 | 159 | \textbf{Problem 2(b)} With 8-bit signed integers, what is the result for the following computations: 160 | \[ 161 | 127 \ensuremath{\oplus}_{256}^{\rm s} 200,\quad 2 \ensuremath{\otimes}_{256}^{\rm s} 127, \quad 2 \ensuremath{\otimes}_{256}^{\rm s} 128, \quad 0 \ensuremath{\ominus}_{256}^{\rm s} 1 162 | \] 163 | \textbf{Problem 3} What is $\ensuremath{\pi}$ to 5 binary places? Hint: recall that $\ensuremath{\pi} \ensuremath{\approx} 3.14$. 164 | 165 | \textbf{Problem 4} What are the single precision $F_{32} = F_{127,8,23}$ floating point representations for the following: 166 | \[ 167 | 2, \quad 31, \quad 32, \quad 23/4, \quad (23/4)\times 2^{100} 168 | \] 169 | \textbf{Problem 5} Let $m(y) = \min\{x \in F_{32} : x > y \}$ be the smallest single precision number greater than $y$. What is $m(2) - 2$ and $m(1024) - 1024$? 170 | 171 | 172 | 173 | \end{document} -------------------------------------------------------------------------------- /sheets/sheet3s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet3s.pdf -------------------------------------------------------------------------------- /sheets/sheet4.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 4** 2 | 3 | 4 | **Problem 1** Suppose $x = 1.25$ and consider 16-bit floating point arithmetic ($F_{16}$). 5 | What is the error in approximating $x$ by the nearest float point number ${\rm fl}(x)$? 6 | What is the error in approximating $2x$, $x/2$, $x + 2$ and $x - 2$ by $2 \otimes x$, $x \oslash 2$, $x ⊕ 2$ and $x \ominus 2$? 7 | 8 | 9 | 10 | 11 | 12 | **Problem 2** Show that $1/5 = 2^{-3} (1.1001100110011…)_2$. 13 | What are the exact bits for $1 ⊘ 5$, $1 ⊘ 5 ⊕ 1$ computed 14 | using half-precision arithmetic ($F_{16} := F_{15,5,10}$) (using default rounding)? 15 | 16 | 17 | 18 | 19 | **Problem 3** Prove the following bounds on the _absolute error_ of a floating point calculation 20 | in idealised floating-point arithmetic $F_{∞,S}$ (i.e., you may assume all operations involve normal floating point numbers): 21 | $$ 22 | \begin{align*} 23 | ({\rm fl}(1.1) ⊗ {\rm fl}(1.2)) &⊕ {\rm fl}(1.3) = 2.62 + ε_1 \\ 24 | ({\rm fl}(1.1) ⊖ 1) & ⊘ {\rm fl}(0.1) = 1 + ε_2 25 | \end{align*} 26 | $$ 27 | such that $|ε_1| ≤ 11 ϵ_{\rm m}$ and $|ε_2| ≤ 40 ϵ_{\rm m}$, where $ϵ_{\rm m}$ is 28 | machine epsilon. 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | **Problem 4** 40 | Let $x ∈ [0,1] ∩ F_{∞,S}$. 41 | Assume that $f^{\rm FP} : F_{∞,S} → F_{∞,S}$ satisfies $f^{\rm FP}(x) = f(x) + δ_x$ where $|δ_x| ≤ c ϵ_{\rm m}$ for all $x ∈ [0,1]$. 42 | Show that 43 | $$ 44 | {f^{\rm FP}(x+h) ⊖ f^{\rm FP}(x-h) \over 2h} = f'(x) + ε 45 | $$ 46 | where absolute error is bounded by 47 | $$ 48 | |ε| ≤ {|f'(x)| \over 2} ϵ_{\rm m} + {M \over 3} h^2 + {2 c ϵ_{\rm m} \over h}, 49 | $$ 50 | where we assume that $h = 2^{-n}$ for $n ≤ S$. 51 | 52 | 53 | 54 | **Problem 5** For intervals $X = [a,b]$ and $Y = [c,d]$ satisfying 55 | $0 < a < b$ and $0 < c < d$, and $n > 0$ prove that 56 | $$ 57 | \meeq{ 58 | X/n = [a/n,b/n] \ccr 59 | XY = [ac, bd] 60 | } 61 | $$ 62 | Generalise (without proof) these formulæ to the case $n < 0$ and to where there are no restrictions 63 | on positivity of $a,b,c,d$. You may use the $\min$ or $\max$ functions. 64 | 65 | 66 | 67 | 68 | **Problem 6(a)** Compute the following floating point interval arithmetic 69 | expression assuming half-precision $F_{16}$ arithmetic: 70 | $$ 71 | [1,1] ⊖ ([1,1] ⊘ 6) 72 | $$ 73 | Hint: it might help to write $1 = (0.1111…)_2$ when doing subtraction. 74 | 75 | 76 | 77 | 78 | **Problem 6(b)** Writing 79 | $$ 80 | \sin\ x = ∑_{k=0}^n {(-1)^k x^{2k+1} \over (2k+1)!} + δ_{x,2n+1} 81 | $$ 82 | Prove the bound $|δ_{x,2n+1}| ≤ 1/(2n+3)!$, assuming $x ∈ [0,1]$. 83 | 84 | 85 | 86 | 87 | **Problem 6(c)** Combine the previous parts to prove that: 88 | $$ 89 | \sin 1 ∈ [(0.11010011000)_2, (0.11010111101)_2] = [0.82421875, 0.84228515625] 90 | $$ 91 | You may use without proof that $1/120 = 2^{-7} (1.000100010001…)_2$. 92 | 93 | -------------------------------------------------------------------------------- /sheets/sheet4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet4.pdf -------------------------------------------------------------------------------- /sheets/sheet4s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet4s.pdf -------------------------------------------------------------------------------- /sheets/sheet5.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 5** 2 | 3 | 4 | 5 | **Problem 1(a)** Suppose $|ϵ_k| ≤ ϵ$ and $n ϵ < 1$. Use induction to show that 6 | $$ 7 | ∏_{k=1}^n (1+ϵ_k) = 1+θ_n 8 | $$ 9 | for some constant $θ_n$ satisfying 10 | $$ 11 | |θ_n| ≤ \underbrace{n ϵ \over 1-nϵ}_{E_{n,ϵ}} 12 | $$ 13 | 14 | 15 | 16 | 17 | **Problem 1(b)** Show for an idealised floating point vector $𝐱 ∈ F_{∞,S}^n$ 18 | that 19 | $$ 20 | x_1 ⊕ ⋯ ⊕ x_n = x_1 + ⋯ + x_n + σ_n 21 | $$ 22 | where 23 | $$ 24 | |σ_n| ≤ \| 𝐱 \|_1 E_{n-1,ϵ_{\rm m}/2}, 25 | $$ 26 | assuming $n ϵ_{\rm m} < 2$ and where 27 | $$ 28 | \|𝐱\|_1 := ∑_{k=1}^n |x_k|. 29 | $$ 30 | Hint: use the previous part to first write 31 | $$ 32 | x_1 ⊕ ⋯ ⊕ x_n = x_1(1+θ_{n-1}) + ∑_{j=2}^n x_j (1 + θ_{n-j+1}). 33 | $$ 34 | 35 | 36 | 37 | 38 | **Problem 1(c)** For $A ∈ F_{∞,S}^{n × n}$ and $𝐱 ∈ F_{∞,S}^n$ consider the error 39 | in approximating matrix multiplication with idealised floating point: for 40 | $$ 41 | A 𝐱 = \begin{pmatrix} 42 | ⨁_{j=1}^n A_{1,j} ⊗ x_j\\ 43 | ⋮ \\ 44 | ⨁_{j=1}^n A_{1,j} ⊗ x_j 45 | \end{pmatrix} + δ 46 | $$ 47 | show that 48 | $$ 49 | \| δ \|_∞ ≤ 2 \|A\|_∞ \| 𝐱 \|_∞ E_{n,ϵ_{\rm m}/2} 50 | $$ 51 | where $n ϵ_{\rm m} < 2$ and the matrix norm is $\|A \|_∞ := \max_k ∑_{j=1}^n |a_{kj}|$. 52 | 53 | 54 | 55 | 56 | 57 | --------- 58 | 59 | 60 | **Problem 2** Derive Backward Euler: use the left-sided divided difference approximation 61 | $$ 62 | u'(x) ≈ {u(x) - u(x-h) \over h} 63 | $$ 64 | to reduce the first order ODE 65 | $$ 66 | \meeq{ 67 | u(a) = c, \qquad u'(x) + ω(x) u(x) = f(x) 68 | } 69 | $$ 70 | to a lower triangular system by discretising on the grid $x_j = a + j h$ for $h = (b-a)/n$. Hint: only impose the ODE on the 71 | gridpoints $x_1,…,x_n$ so that the divided difference does not depend on behaviour at $x_{-1}$. 72 | 73 | 74 | 75 | 76 | 77 | **Problem 3** Reduce a Schrödinger equation to a tridiagonal linear system by discretising on the grid $x_j = a + j h$ for $h = (b-a)/n$: 78 | $$ 79 | \meeq{ 80 | u(a) = c,\qquad u''(x) + V(x) u(x) = f(x), \qquad u(b) = d. 81 | } 82 | $$ 83 | 84 | -------------------------------------------------------------------------------- /sheets/sheet5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet5.pdf -------------------------------------------------------------------------------- /sheets/sheet5s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet5s.pdf -------------------------------------------------------------------------------- /sheets/sheet6.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 6** 2 | 3 | **Problem 1** By computing the Cholesky factorisation, determine 4 | which of the following matrices are symmetric positive definite: 5 | $$ 6 | \begin{bmatrix} 1 & -1 \\ 7 | -1 & 3 8 | \end{bmatrix}, \begin{bmatrix} 1 & 2 & 2 \\ 9 | 2 & 1 & 2\\ 10 | 2 & 2 & 1 11 | \end{bmatrix}, \begin{bmatrix} 3 & 2 & 1 \\ 12 | 2 & 4 & 2\\ 13 | 1 & 2 & 5 14 | \end{bmatrix}, 15 | \begin{bmatrix} 4 & 2 & 2 & 1 \\ 16 | 2 & 4 & 2 & 2\\ 17 | 2 & 2 & 4 & 2 \\ 18 | 1 & 2 & 2 & 4 19 | \end{bmatrix} 20 | $$ 21 | 22 | 23 | 24 | **Problem 2** Show that a matrix $A ∈ ℝ^{n × n}$ is symmetric positive definite if and only if it has a _reverse_ Cholesky 25 | factorisation of the form 26 | $$ 27 | A = U U^⊤ 28 | $$ 29 | where $U$ is upper triangular with positive entries on the diagonal. 30 | 31 | 32 | 33 | 34 | **Problem 3(a)** Use the Cholesky factorisation to prove that the following $n × n$ matrix is symmetric positive definite 35 | for any $n$: 36 | $$ 37 | Δ_n := \begin{bmatrix} 38 | 2 & -1 \\ 39 | -1 & 2 & -1 \\ 40 | & -1 & 2 & ⋱ \\ 41 | && ⋱ & ⋱ & -1 \\ 42 | &&& -1 & 2 43 | \end{bmatrix} 44 | $$ 45 | Hint: consider a matrix $K_n^α$ that equals $Δ_n$ apart from the top left entry which is $α > 1$ and use a proof by induction. 46 | 47 | 48 | 49 | 50 | **Problem 3(b)** 51 | Deduce its Cholesky factorisations: $Δ_n = L_n L_n^⊤$ where 52 | $L_n$ is lower triangular. 53 | 54 | 55 | 56 | 57 | **Problem 4** Use Lagrange interpolation to 58 | interpolate the function $\cos x$ by a polynomial at the points 59 | $[0,2,3,4]$ and evaluate at $x = 1$. 60 | 61 | 62 | 63 | **Problem 5** Compute the LU factorisation of the following transposed Vandermonde matrices: 64 | $$ 65 | \begin{bmatrix} 66 | 1 & 1 \\ 67 | x & y 68 | \end{bmatrix}, 69 | \begin{bmatrix} 70 | 1 & 1 & 1 \\ 71 | x & y & z \\ 72 | x^2 & y^2 & z^2 73 | \end{bmatrix}, 74 | \begin{bmatrix} 75 | 1 & 1 & 1 & 1 \\ 76 | x & y & z & t \\ 77 | x^2 & y^2 & z^2 & t^2 \\ 78 | x^3 & y^3 & z^3 & t^3 79 | \end{bmatrix} 80 | $$ 81 | Can you spot a pattern? Test your conjecture with a $5 × 5$ Vandermonde matrix. 82 | 83 | -------------------------------------------------------------------------------- /sheets/sheet6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet6.pdf -------------------------------------------------------------------------------- /sheets/sheet6.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,a4paper]{article} 2 | 3 | \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry} 4 | \usepackage{lmodern} 5 | \usepackage{amssymb,amsmath} 6 | \usepackage{bm} 7 | \usepackage{graphicx} 8 | \usepackage{microtype} 9 | \usepackage{hyperref} 10 | \usepackage[usenames,dvipsnames]{xcolor} 11 | \setlength{\parindent}{0pt} 12 | \setlength{\parskip}{1.2ex} 13 | 14 | 15 | 16 | 17 | \hypersetup 18 | { pdfauthor = { }, 19 | pdftitle={ }, 20 | colorlinks=TRUE, 21 | linkcolor=black, 22 | citecolor=blue, 23 | urlcolor=blue 24 | } 25 | 26 | 27 | 28 | 29 | \usepackage{upquote} 30 | \usepackage{listings} 31 | \usepackage{xcolor} 32 | \lstset{ 33 | basicstyle=\ttfamily\footnotesize, 34 | upquote=true, 35 | breaklines=true, 36 | breakindent=0pt, 37 | keepspaces=true, 38 | showspaces=false, 39 | columns=fullflexible, 40 | showtabs=false, 41 | showstringspaces=false, 42 | escapeinside={(*@}{@*)}, 43 | extendedchars=true, 44 | } 45 | \newcommand{\HLJLt}[1]{#1} 46 | \newcommand{\HLJLw}[1]{#1} 47 | \newcommand{\HLJLe}[1]{#1} 48 | \newcommand{\HLJLeB}[1]{#1} 49 | \newcommand{\HLJLo}[1]{#1} 50 | \newcommand{\HLJLk}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 51 | \newcommand{\HLJLkc}[1]{\textcolor[RGB]{59,151,46}{\textit{#1}}} 52 | \newcommand{\HLJLkd}[1]{\textcolor[RGB]{214,102,97}{\textit{#1}}} 53 | \newcommand{\HLJLkn}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 54 | \newcommand{\HLJLkp}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 55 | \newcommand{\HLJLkr}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 56 | \newcommand{\HLJLkt}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 57 | \newcommand{\HLJLn}[1]{#1} 58 | \newcommand{\HLJLna}[1]{#1} 59 | \newcommand{\HLJLnb}[1]{#1} 60 | \newcommand{\HLJLnbp}[1]{#1} 61 | \newcommand{\HLJLnc}[1]{#1} 62 | \newcommand{\HLJLncB}[1]{#1} 63 | \newcommand{\HLJLnd}[1]{\textcolor[RGB]{214,102,97}{#1}} 64 | \newcommand{\HLJLne}[1]{#1} 65 | \newcommand{\HLJLneB}[1]{#1} 66 | \newcommand{\HLJLnf}[1]{\textcolor[RGB]{66,102,213}{#1}} 67 | \newcommand{\HLJLnfm}[1]{\textcolor[RGB]{66,102,213}{#1}} 68 | \newcommand{\HLJLnp}[1]{#1} 69 | \newcommand{\HLJLnl}[1]{#1} 70 | \newcommand{\HLJLnn}[1]{#1} 71 | \newcommand{\HLJLno}[1]{#1} 72 | \newcommand{\HLJLnt}[1]{#1} 73 | \newcommand{\HLJLnv}[1]{#1} 74 | \newcommand{\HLJLnvc}[1]{#1} 75 | \newcommand{\HLJLnvg}[1]{#1} 76 | \newcommand{\HLJLnvi}[1]{#1} 77 | \newcommand{\HLJLnvm}[1]{#1} 78 | \newcommand{\HLJLl}[1]{#1} 79 | \newcommand{\HLJLld}[1]{\textcolor[RGB]{148,91,176}{\textit{#1}}} 80 | \newcommand{\HLJLs}[1]{\textcolor[RGB]{201,61,57}{#1}} 81 | \newcommand{\HLJLsa}[1]{\textcolor[RGB]{201,61,57}{#1}} 82 | \newcommand{\HLJLsb}[1]{\textcolor[RGB]{201,61,57}{#1}} 83 | \newcommand{\HLJLsc}[1]{\textcolor[RGB]{201,61,57}{#1}} 84 | \newcommand{\HLJLsd}[1]{\textcolor[RGB]{201,61,57}{#1}} 85 | \newcommand{\HLJLsdB}[1]{\textcolor[RGB]{201,61,57}{#1}} 86 | \newcommand{\HLJLsdC}[1]{\textcolor[RGB]{201,61,57}{#1}} 87 | \newcommand{\HLJLse}[1]{\textcolor[RGB]{59,151,46}{#1}} 88 | \newcommand{\HLJLsh}[1]{\textcolor[RGB]{201,61,57}{#1}} 89 | \newcommand{\HLJLsi}[1]{#1} 90 | \newcommand{\HLJLso}[1]{\textcolor[RGB]{201,61,57}{#1}} 91 | \newcommand{\HLJLsr}[1]{\textcolor[RGB]{201,61,57}{#1}} 92 | \newcommand{\HLJLss}[1]{\textcolor[RGB]{201,61,57}{#1}} 93 | \newcommand{\HLJLssB}[1]{\textcolor[RGB]{201,61,57}{#1}} 94 | \newcommand{\HLJLnB}[1]{\textcolor[RGB]{59,151,46}{#1}} 95 | \newcommand{\HLJLnbB}[1]{\textcolor[RGB]{59,151,46}{#1}} 96 | \newcommand{\HLJLnfB}[1]{\textcolor[RGB]{59,151,46}{#1}} 97 | \newcommand{\HLJLnh}[1]{\textcolor[RGB]{59,151,46}{#1}} 98 | \newcommand{\HLJLni}[1]{\textcolor[RGB]{59,151,46}{#1}} 99 | \newcommand{\HLJLnil}[1]{\textcolor[RGB]{59,151,46}{#1}} 100 | \newcommand{\HLJLnoB}[1]{\textcolor[RGB]{59,151,46}{#1}} 101 | \newcommand{\HLJLoB}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 102 | \newcommand{\HLJLow}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 103 | \newcommand{\HLJLp}[1]{#1} 104 | \newcommand{\HLJLc}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 105 | \newcommand{\HLJLch}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 106 | \newcommand{\HLJLcm}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 107 | \newcommand{\HLJLcp}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 108 | \newcommand{\HLJLcpB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 109 | \newcommand{\HLJLcs}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 110 | \newcommand{\HLJLcsB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 111 | \newcommand{\HLJLg}[1]{#1} 112 | \newcommand{\HLJLgd}[1]{#1} 113 | \newcommand{\HLJLge}[1]{#1} 114 | \newcommand{\HLJLgeB}[1]{#1} 115 | \newcommand{\HLJLgh}[1]{#1} 116 | \newcommand{\HLJLgi}[1]{#1} 117 | \newcommand{\HLJLgo}[1]{#1} 118 | \newcommand{\HLJLgp}[1]{#1} 119 | \newcommand{\HLJLgs}[1]{#1} 120 | \newcommand{\HLJLgsB}[1]{#1} 121 | \newcommand{\HLJLgt}[1]{#1} 122 | 123 | 124 | \def\endash{–} 125 | \def\bbD{ {\mathbb D} } 126 | \def\bbZ{ {\mathbb Z} } 127 | \def\bbR{ {\mathbb R} } 128 | \def\bbC{ {\mathbb C} } 129 | 130 | \def\x{ {\vc x} } 131 | \def\a{ {\vc a} } 132 | \def\b{ {\vc b} } 133 | \def\e{ {\vc e} } 134 | \def\f{ {\vc f} } 135 | \def\u{ {\vc u} } 136 | \def\v{ {\vc v} } 137 | \def\y{ {\vc y} } 138 | \def\z{ {\vc z} } 139 | 140 | \def\red#1{ {\color{red} #1} } 141 | \def\blue#1{ {\color{blue} #1} } 142 | \def\green#1{ {\color{ForestGreen} #1} } 143 | \def\magenta#1{ {\color{magenta} #1} } 144 | 145 | \input{somacros} 146 | 147 | \begin{document} 148 | 149 | 150 | 151 | \textbf{Numerical Analysis MATH50003 (2023\ensuremath{\endash}24) Problem Sheet 6} 152 | 153 | \textbf{Problem 1} By computing the Cholesky factorisation, determine which of the following matrices are symmetric positive definite: 154 | \[ 155 | \begin{bmatrix} 1 & -1 \\ 156 | -1 & 3 157 | \end{bmatrix}, \begin{bmatrix} 1 & 2 & 2 \\ 158 | 2 & 1 & 2\\ 159 | 2 & 2 & 1 160 | \end{bmatrix}, \begin{bmatrix} 3 & 2 & 1 \\ 161 | 2 & 4 & 2\\ 162 | 1 & 2 & 5 163 | \end{bmatrix}, 164 | \begin{bmatrix} 4 & 2 & 2 & 1 \\ 165 | 2 & 4 & 2 & 2\\ 166 | 2 & 2 & 4 & 2 \\ 167 | 1 & 2 & 2 & 4 168 | \end{bmatrix} 169 | \] 170 | \textbf{Problem 2} Show that a matrix $A \ensuremath{\in} \ensuremath{\bbR}^{n \ensuremath{\times} n}$ is symmetric positive definite if and only if it has a \emph{reverse} Cholesky factorisation of the form 171 | \[ 172 | A = U U^\ensuremath{\top} 173 | \] 174 | where $U$ is upper triangular with positive entries on the diagonal. 175 | 176 | \textbf{Problem 3(a)} Use the Cholesky factorisation to prove that the following $n \ensuremath{\times} n$ matrix is symmetric positive definite for any $n$: 177 | \[ 178 | \ensuremath{\Delta}_n := \begin{bmatrix} 179 | 2 & -1 \\ 180 | -1 & 2 & -1 \\ 181 | & -1 & 2 & \ensuremath{\ddots} \\ 182 | && \ensuremath{\ddots} & \ensuremath{\ddots} & -1 \\ 183 | &&& -1 & 2 184 | \end{bmatrix} 185 | \] 186 | Hint: consider a matrix $K_n^\ensuremath{\alpha}$ that equals $\ensuremath{\Delta}_n$ apart from the top left entry which is $\ensuremath{\alpha} > 1$ and use a proof by induction. 187 | 188 | \textbf{Problem 3(b)} Deduce its Cholesky factorisations: $\ensuremath{\Delta}_n = L_n L_n^\ensuremath{\top}$ where $L_n$ is lower triangular. 189 | 190 | \textbf{Problem 4} Use Lagrange interpolation to interpolate the function $\cos x$ by a polynomial at the points $[0,2,3,4]$ and evaluate at $x = 1$. 191 | 192 | \textbf{Problem 5} Compute the LU factorisation of the following transposed Vandermonde matrices: 193 | \[ 194 | \begin{bmatrix} 195 | 1 & 1 \\ 196 | x & y 197 | \end{bmatrix}, 198 | \begin{bmatrix} 199 | 1 & 1 & 1 \\ 200 | x & y & z \\ 201 | x^2 & y^2 & z^2 202 | \end{bmatrix}, 203 | \begin{bmatrix} 204 | 1 & 1 & 1 & 1 \\ 205 | x & y & z & t \\ 206 | x^2 & y^2 & z^2 & t^2 \\ 207 | x^3 & y^3 & z^3 & t^3 208 | \end{bmatrix} 209 | \] 210 | Can you spot a pattern? Test your conjecture with a $5 \ensuremath{\times} 5$ Vandermonde matrix. 211 | 212 | 213 | 214 | \end{document} -------------------------------------------------------------------------------- /sheets/sheet6s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet6s.pdf -------------------------------------------------------------------------------- /sheets/sheet7.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 7** 2 | 3 | **Problem 1(a)** Show for a unitary matrix $Q ∈ U(n)$ and a vector $𝐱 ∈ ℂ^n$ that multiplication by $Q$ preserve the 2-norm: $\|Q 𝐱\| = \|𝐱\|.$ 4 | 5 | 6 | 7 | **Problem 1(b)** Show that the eigenvalues $λ$ of a unitary matrix $Q$ are 8 | on the unit circle: $|λ| = 1$. Hint: recall for any eigenvalue $λ$ that there exists a unit eigenvector 9 | $𝐯 ∈ ℂ^n$ (satisfying $\| 𝐯 \| = 1$). 10 | 11 | 12 | 13 | **Problem 1(c)** Show for an orthogonal matrix $Q ∈ O(n)$ that $\det Q = ±1$. 14 | Give an example of $Q ∈ U(n)$ such that $\det Q ≠ ±1$. 15 | Hint: recall for any real matrices $A$ and $B$ that $\det A = \det A^⊤$ and 16 | $\det(AB) = \det A \det B$. 17 | 18 | 19 | 20 | **Problem 1(d)** A normal matrix commutes with its adjoint. Show that $Q ∈ U(n)$ is normal. 21 | 22 | 23 | 24 | **Problem 1(e)** 25 | The spectral theorem states that any normal matrix is unitarily diagonalisable: if $A$ is normal then $A = V Λ V^⋆$ 26 | where $V ∈ U(n)$ and $Λ$ is diagonal. 27 | Use this to show that $Q ∈ U(n)$ is equal to $I$ if and only if all its eigenvalues are 1. 28 | 29 | 30 | 31 | 32 | **Problem 2** Consider the vectors 33 | $$ 34 | 𝐚 = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}\qquad\hbox{and}\qquad 𝐛 = \begin{bmatrix} 1 \\ 2{\rm i} \\ 2 \end{bmatrix}. 35 | $$ 36 | Use reflections to determine the entries of orthogonal/unitary matrices $Q_1, Q_2, Q_3$ such that 37 | $$ 38 | Q_1 𝐚 = \begin{bmatrix} 3 \\ 0 \\ 0 \end{bmatrix}, Q_2 𝐚 = \begin{bmatrix} -3 \\ 0 \\ 0 \end{bmatrix}, 39 | Q_3 𝐛 = \begin{bmatrix} -3 \\ 0 \\ 0 \end{bmatrix} 40 | $$ 41 | 42 | 43 | 44 | **Problem 3(a)** What simple rotation matrices $Q_1,Q_2 ∈ SO(2)$ have the property that: 45 | $$ 46 | Q_1 \begin{bmatrix} 1 \\ 2 \end{bmatrix} =\begin{bmatrix} \sqrt{5} \\ 0 \end{bmatrix}, Q_2 \begin{bmatrix} \sqrt{5} \\ 2 \end{bmatrix} = \begin{bmatrix} 3 \\ 0 \end{bmatrix} 47 | $$ 48 | 49 | 50 | 51 | **Problem 3(b)** Find an orthogonal matrix that is a product of two simple rotations but 52 | acting on two different subspaces: 53 | $$ 54 | Q = \underbrace{\begin{bmatrix} \cos θ_2 & & -\sin θ_2 \\ & 1 \\ 55 | \sin θ_2 & & \cos θ_2 \end{bmatrix}}_{Q_2} \underbrace{\begin{bmatrix} \cos θ_1 & -\sin θ_1 \\ \sin θ_1 & \cos θ_1 \\ && 1 \end{bmatrix}}_{Q_1} 56 | $$ 57 | so that, for $𝐚$ defined above, 58 | $$ 59 | Q 𝐚 = \begin{bmatrix} \|𝐚\| \\ 0 \\ 0 \end{bmatrix}. 60 | $$ 61 | Hint: you do not need to determine $θ_1, θ_2$, instead you can write the entries of $Q_1, Q_2$ 62 | directly using just square-roots. 63 | 64 | 65 | 66 | 67 | 68 | **Problem 4(a)** Show that every matrix $A ∈ ℝ^{m × n}$ has a QR factorisation such that the diagonal of $R$ is non-negative. 69 | Make sure to include the case of more columns than rows (i.e. $m < n$). 70 | 71 | 72 | 73 | **Problem 4(b)** Show that the QR factorisation of a square invertible matrix $A ∈ ℝ^{n × n}$ is unique, 74 | provided that the diagonal of $R$ is positive. 75 | 76 | 77 | -------------------------------------------------------------------------------- /sheets/sheet7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet7.pdf -------------------------------------------------------------------------------- /sheets/sheet7s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet7s.pdf -------------------------------------------------------------------------------- /sheets/sheet8.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 8** 2 | 3 | 4 | 5 | **Problem 1** Give explicit formulae for $\hat f_k$ and $\hat f_k^n$ for the following functions: 6 | $$ 7 | \cos θ, \cos 4θ, \sin^4θ, {3 \over 3 - {\rm e}^{\rm i θ}}, {1 \over 1 - 2{\rm e}^{\rm i θ}} 8 | $$ 9 | 10 | 11 | 12 | 13 | **Problem 2** Prove that if the first $λ-1$ derivatives $f(θ), f'(θ), …, f^{(λ-1)}(θ)$ 14 | are 2π-periodic and $f^{(λ)}$ is uniformly bounded that 15 | $$ 16 | |\hat f_k| = O(|k|^{-λ})\qquad \hbox{as $|k| → ∞$} 17 | $$ 18 | Use this to show for the Taylor case ($0 = \hat f_{-1} = \hat f_{-2} = ⋯$) that 19 | $$ 20 | |f(θ) - ∑_{k=0}^{n-1} \hat f_k {\rm e}^{{\rm i}kθ}| = O(n^{1-λ})\qquad \hbox{as $n → ∞$} 21 | $$ 22 | 23 | 24 | 25 | 26 | **Problem 3(a)** 27 | If $f$ is a trigonometric polynomial ($\hat f_k = 0$ for $|k| > m$) show 28 | for $n ≥ 2m+1$ that we can exactly recover $f$: 29 | $$ 30 | f(θ) = \sum_{k=-m}^m \hat f_k^n {\rm e}^{{\rm i} k θ} 31 | $$ 32 | 33 | 34 | 35 | **Problem 3(b)** For the general (non-Taylor) case and $n = 2m+1$, prove convergence for 36 | $$ 37 | f_{-m:m}(θ) := ∑_{k=-m}^m \hat f_k^n {\rm e}^{{\rm i} k θ} 38 | $$ 39 | to $f(θ)$ as $n → ∞$. 40 | What is the rate of convergence if we know that the first $λ-1$ derivatives $f(θ), f'(θ), …, f^{(λ-1)}(θ)$ 41 | are 2π-periodic and $f^{(λ)}$ is uniformly bounded? 42 | 43 | 44 | 45 | **Problem 3(c)** Show that $f_{-m:m}(θ)$ interpolates $f$ at $θ_j = 2πj/n$ for $n = 2m+1$. 46 | 47 | 48 | 49 | 50 | **Problem 4(a)** Show for $0 ≤ k,ℓ ≤ n-1$ 51 | $$ 52 | {1 \over n} \sum_{j=1}^n \cos k θ_j \cos ℓ θ_j = \begin{cases} 1 & k = ℓ = 0 \\ 53 | 1/2 & k = ℓ \\ 54 | 0 & \hbox{otherwise} 55 | \end{cases} 56 | $$ 57 | for $θ_j = π(j-1/2)/n$. Hint: Be careful as the $θ_j$ differ from before, 58 | and only cover half the period, $[0,π]$. Using symmetry may help. 59 | You may also consider replacing $\cos$ with 60 | complex exponentials: 61 | $$ 62 | \cos θ = {{\rm e}^{{\rm i}θ} + {\rm e}^{-{\rm i}θ} \over 2}. 63 | $$ 64 | 65 | 66 | 67 | 68 | 69 | **Problem 4(b)** Consider the Discrete Cosine Transform (DCT) 70 | $$ 71 | C_n := \begin{bmatrix} 72 | \sqrt{1/n} \\ 73 | & \sqrt{2/n} \\ 74 | && ⋱ \\ 75 | &&& \sqrt{2/n} 76 | \end{bmatrix} 77 | \begin{bmatrix} 78 | 1 & ⋯ & 1\\ 79 | \cos θ_1 & ⋯ & \cos θ_n \\ 80 | ⋮ & ⋱ & ⋮ \\ 81 | \cos (n-1)θ_1 & ⋯ & \cos (n-1)θ_n 82 | \end{bmatrix} 83 | $$ 84 | for $θ_j = π(j-1/2)/n$. Prove that $C_n$ is orthogonal: $C_n^⊤ C_n = C_n C_n^⊤ = I$. 85 | 86 | 87 | 88 | 89 | 90 | **Problem 5** What polynomial interpolates $\cos z$ at $1, \exp(2π\I/3)$ and $\exp(-2π\I/3)$? 91 | 92 | -------------------------------------------------------------------------------- /sheets/sheet8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet8.pdf -------------------------------------------------------------------------------- /sheets/sheet8s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet8s.pdf -------------------------------------------------------------------------------- /sheets/sheet9.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 9** 2 | 3 | 4 | 5 | 6 | **Problem 1** Construct the monic and orthonormal polynomials up to degree $3$ 7 | for the weights $\sqrt{1-x^2}$ and $1-x$ on $[-1,1]$. What are the top $3 × 3$ entries of the corresponding Jacobi matrices? 8 | Hint: for the first weight, find a recursive formula for $\int_{-1}^1 x^k \sqrt{1-x^2} {\rm d} x$ 9 | using a change-of-variables. 10 | 11 | 12 | 13 | 14 | 15 | 16 | **Problem 2** Prove Theorem 13: a precisely degree $n$ polynomial 17 | $$ 18 | p(x) = k_n x^n + O(x^{n-1}) 19 | $$ 20 | satisfies 21 | $$ 22 | ⟨p,f_m⟩ = 0 23 | $$ 24 | for all polynomials $f_m$ of degree $m < n$ of degree less than $n$ if and only if $p(x) = c π_n$ for some constant 25 | $c$, where $π_n$ are monic orthogonal polynomials. 26 | 27 | 28 | 29 | 30 | 31 | **Problem 3** If $w(-x) = w(x)$ for a weight supported on $[-b,b]$ show that $a_n = 0$. 32 | Hint: first show that the (monic) polynomials $p_{2n}(x)$ are even and $p_{2n+1}(x)$ are odd. 33 | 34 | 35 | 36 | 37 | 38 | 39 | **Problem 4(a)** Prove that 40 | $$ 41 | U_n(\cos θ) = {\sin(n+1) θ \over \sin θ}. 42 | $$ 43 | 44 | 45 | 46 | 47 | **Problem 4(b)** Show that 48 | $$ 49 | \begin{align*} 50 | x U_0(x) &= U_1(x)/2 \\ 51 | x U_n(x) &= {U_{n-1}(x) \over 2} + {U_{n+1}(x) \over 2}. 52 | \end{align*} 53 | $$ 54 | 55 | 56 | 57 | **Problem 5** Use the fact that orthogonal polynomials are uniquely determined by their leading order coefficient and orthogonality to lower 58 | dimensional polynomials to show that: 59 | $$ 60 | T_n'(x) = n U_{n-1}(x). 61 | $$ 62 | 63 | 64 | 65 | 66 | 67 | **Problem 6(a)** Consider Hermite polynomials orthogonal with respect to the weight $\exp(-x^2)$ on $ℝ$ 68 | with the normalisation 69 | $$ 70 | H_n(x) = 2^n x^n + O(x^{n-1}). 71 | $$ 72 | Prove the Rodrigues formula 73 | $$ 74 | H_n(x) = (-1)^n \exp(x^2) {{\rm d}^n \over {\rm d}x^n} \exp(-x^2). 75 | $$ 76 | Hint: use integration-by-parts. 77 | 78 | 79 | 80 | **Problem 6(b)** What are $k_n^{(1)}$ and $k_n^{(2)}$ such that 81 | $$ 82 | H_n(x) = 2^n x^n + k_n^{(1)} x^{n-1} + k_n^{(2)} x^{n-2} + O(x^{n-3}) 83 | $$ 84 | 85 | 86 | 87 | **Problem 6(c)** Deduce the 3-term recurrence relationship for $H_n(x)$. 88 | 89 | 90 | 91 | **Problem 6(d)** Prove that $H_n'(x) = 2n H_{n-1}(x)$. Hint: show orthogonality of $H_n'$ to all lower degree polynomials, 92 | and that the normalisation constants match. 93 | 94 | -------------------------------------------------------------------------------- /sheets/sheet9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet9.pdf -------------------------------------------------------------------------------- /sheets/sheet9.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,a4paper]{article} 2 | 3 | \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry} 4 | \usepackage{lmodern} 5 | \usepackage{amssymb,amsmath} 6 | \usepackage{bm} 7 | \usepackage{graphicx} 8 | \usepackage{microtype} 9 | \usepackage{hyperref} 10 | \usepackage[usenames,dvipsnames]{xcolor} 11 | \setlength{\parindent}{0pt} 12 | \setlength{\parskip}{1.2ex} 13 | 14 | 15 | 16 | 17 | \hypersetup 18 | { pdfauthor = { }, 19 | pdftitle={ }, 20 | colorlinks=TRUE, 21 | linkcolor=black, 22 | citecolor=blue, 23 | urlcolor=blue 24 | } 25 | 26 | 27 | 28 | 29 | \usepackage{upquote} 30 | \usepackage{listings} 31 | \usepackage{xcolor} 32 | \lstset{ 33 | basicstyle=\ttfamily\footnotesize, 34 | upquote=true, 35 | breaklines=true, 36 | breakindent=0pt, 37 | keepspaces=true, 38 | showspaces=false, 39 | columns=fullflexible, 40 | showtabs=false, 41 | showstringspaces=false, 42 | escapeinside={(*@}{@*)}, 43 | extendedchars=true, 44 | } 45 | \newcommand{\HLJLt}[1]{#1} 46 | \newcommand{\HLJLw}[1]{#1} 47 | \newcommand{\HLJLe}[1]{#1} 48 | \newcommand{\HLJLeB}[1]{#1} 49 | \newcommand{\HLJLo}[1]{#1} 50 | \newcommand{\HLJLk}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 51 | \newcommand{\HLJLkc}[1]{\textcolor[RGB]{59,151,46}{\textit{#1}}} 52 | \newcommand{\HLJLkd}[1]{\textcolor[RGB]{214,102,97}{\textit{#1}}} 53 | \newcommand{\HLJLkn}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 54 | \newcommand{\HLJLkp}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 55 | \newcommand{\HLJLkr}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 56 | \newcommand{\HLJLkt}[1]{\textcolor[RGB]{148,91,176}{\textbf{#1}}} 57 | \newcommand{\HLJLn}[1]{#1} 58 | \newcommand{\HLJLna}[1]{#1} 59 | \newcommand{\HLJLnb}[1]{#1} 60 | \newcommand{\HLJLnbp}[1]{#1} 61 | \newcommand{\HLJLnc}[1]{#1} 62 | \newcommand{\HLJLncB}[1]{#1} 63 | \newcommand{\HLJLnd}[1]{\textcolor[RGB]{214,102,97}{#1}} 64 | \newcommand{\HLJLne}[1]{#1} 65 | \newcommand{\HLJLneB}[1]{#1} 66 | \newcommand{\HLJLnf}[1]{\textcolor[RGB]{66,102,213}{#1}} 67 | \newcommand{\HLJLnfm}[1]{\textcolor[RGB]{66,102,213}{#1}} 68 | \newcommand{\HLJLnp}[1]{#1} 69 | \newcommand{\HLJLnl}[1]{#1} 70 | \newcommand{\HLJLnn}[1]{#1} 71 | \newcommand{\HLJLno}[1]{#1} 72 | \newcommand{\HLJLnt}[1]{#1} 73 | \newcommand{\HLJLnv}[1]{#1} 74 | \newcommand{\HLJLnvc}[1]{#1} 75 | \newcommand{\HLJLnvg}[1]{#1} 76 | \newcommand{\HLJLnvi}[1]{#1} 77 | \newcommand{\HLJLnvm}[1]{#1} 78 | \newcommand{\HLJLl}[1]{#1} 79 | \newcommand{\HLJLld}[1]{\textcolor[RGB]{148,91,176}{\textit{#1}}} 80 | \newcommand{\HLJLs}[1]{\textcolor[RGB]{201,61,57}{#1}} 81 | \newcommand{\HLJLsa}[1]{\textcolor[RGB]{201,61,57}{#1}} 82 | \newcommand{\HLJLsb}[1]{\textcolor[RGB]{201,61,57}{#1}} 83 | \newcommand{\HLJLsc}[1]{\textcolor[RGB]{201,61,57}{#1}} 84 | \newcommand{\HLJLsd}[1]{\textcolor[RGB]{201,61,57}{#1}} 85 | \newcommand{\HLJLsdB}[1]{\textcolor[RGB]{201,61,57}{#1}} 86 | \newcommand{\HLJLsdC}[1]{\textcolor[RGB]{201,61,57}{#1}} 87 | \newcommand{\HLJLse}[1]{\textcolor[RGB]{59,151,46}{#1}} 88 | \newcommand{\HLJLsh}[1]{\textcolor[RGB]{201,61,57}{#1}} 89 | \newcommand{\HLJLsi}[1]{#1} 90 | \newcommand{\HLJLso}[1]{\textcolor[RGB]{201,61,57}{#1}} 91 | \newcommand{\HLJLsr}[1]{\textcolor[RGB]{201,61,57}{#1}} 92 | \newcommand{\HLJLss}[1]{\textcolor[RGB]{201,61,57}{#1}} 93 | \newcommand{\HLJLssB}[1]{\textcolor[RGB]{201,61,57}{#1}} 94 | \newcommand{\HLJLnB}[1]{\textcolor[RGB]{59,151,46}{#1}} 95 | \newcommand{\HLJLnbB}[1]{\textcolor[RGB]{59,151,46}{#1}} 96 | \newcommand{\HLJLnfB}[1]{\textcolor[RGB]{59,151,46}{#1}} 97 | \newcommand{\HLJLnh}[1]{\textcolor[RGB]{59,151,46}{#1}} 98 | \newcommand{\HLJLni}[1]{\textcolor[RGB]{59,151,46}{#1}} 99 | \newcommand{\HLJLnil}[1]{\textcolor[RGB]{59,151,46}{#1}} 100 | \newcommand{\HLJLnoB}[1]{\textcolor[RGB]{59,151,46}{#1}} 101 | \newcommand{\HLJLoB}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 102 | \newcommand{\HLJLow}[1]{\textcolor[RGB]{102,102,102}{\textbf{#1}}} 103 | \newcommand{\HLJLp}[1]{#1} 104 | \newcommand{\HLJLc}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 105 | \newcommand{\HLJLch}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 106 | \newcommand{\HLJLcm}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 107 | \newcommand{\HLJLcp}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 108 | \newcommand{\HLJLcpB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 109 | \newcommand{\HLJLcs}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 110 | \newcommand{\HLJLcsB}[1]{\textcolor[RGB]{153,153,119}{\textit{#1}}} 111 | \newcommand{\HLJLg}[1]{#1} 112 | \newcommand{\HLJLgd}[1]{#1} 113 | \newcommand{\HLJLge}[1]{#1} 114 | \newcommand{\HLJLgeB}[1]{#1} 115 | \newcommand{\HLJLgh}[1]{#1} 116 | \newcommand{\HLJLgi}[1]{#1} 117 | \newcommand{\HLJLgo}[1]{#1} 118 | \newcommand{\HLJLgp}[1]{#1} 119 | \newcommand{\HLJLgs}[1]{#1} 120 | \newcommand{\HLJLgsB}[1]{#1} 121 | \newcommand{\HLJLgt}[1]{#1} 122 | 123 | 124 | \def\endash{–} 125 | \def\bbD{ {\mathbb D} } 126 | \def\bbZ{ {\mathbb Z} } 127 | \def\bbR{ {\mathbb R} } 128 | \def\bbC{ {\mathbb C} } 129 | 130 | \def\x{ {\vc x} } 131 | \def\a{ {\vc a} } 132 | \def\b{ {\vc b} } 133 | \def\e{ {\vc e} } 134 | \def\f{ {\vc f} } 135 | \def\u{ {\vc u} } 136 | \def\v{ {\vc v} } 137 | \def\y{ {\vc y} } 138 | \def\z{ {\vc z} } 139 | \def\w{ {\vc w} } 140 | 141 | \def\bt{ {\tilde b} } 142 | \def\ct{ {\tilde c} } 143 | \def\Ut{ {\tilde U} } 144 | \def\Qt{ {\tilde Q} } 145 | \def\Rt{ {\tilde R} } 146 | \def\Xt{ {\tilde X} } 147 | \def\acos{ {\rm acos}\, } 148 | 149 | \def\red#1{ {\color{red} #1} } 150 | \def\blue#1{ {\color{blue} #1} } 151 | \def\green#1{ {\color{ForestGreen} #1} } 152 | \def\magenta#1{ {\color{magenta} #1} } 153 | 154 | \input{somacros} 155 | 156 | \begin{document} 157 | 158 | 159 | 160 | \textbf{Numerical Analysis MATH50003 (2023\ensuremath{\endash}24) Problem Sheet 9} 161 | 162 | \textbf{Problem 1} Construct the monic and orthonormal polynomials up to degree $3$ for the weights $\sqrt{1-x^2}$ and $1-x$ on $[-1,1]$. What are the top $3 \ensuremath{\times} 3$ entries of the corresponding Jacobi matrices? Hint: for the first weight, find a recursive formula for $\int_{-1}^1 x^k \sqrt{1-x^2} {\rm d} x$ using a change-of-variables. 163 | 164 | \textbf{Problem 2} Prove Theorem 13: a precisely degree $n$ polynomial 165 | \[ 166 | p(x) = k_n x^n + O(x^{n-1}) 167 | \] 168 | satisfies 169 | \[ 170 | \ensuremath{\langle}p,f_m\ensuremath{\rangle} = 0 171 | \] 172 | for all polynomials $f_m$ of degree $m < n$ of degree less than $n$ if and only if $p(x) = c \ensuremath{\pi}_n$ for some constant $c$, where $\ensuremath{\pi}_n$ are monic orthogonal polynomials. 173 | 174 | \textbf{Problem 3} If $w(-x) = w(x)$ for a weight supported on $[-b,b]$ show that $a_n = 0$. Hint: first show that the (monic) polynomials $p_{2n}(x)$ are even and $p_{2n+1}(x)$ are odd. 175 | 176 | \textbf{Problem 4(a)} Prove that 177 | \[ 178 | U_n(\cos \ensuremath{\theta}) = {\sin(n+1) \ensuremath{\theta} \over \sin \ensuremath{\theta}}. 179 | \] 180 | \textbf{Problem 4(b)} Show that 181 | \begin{align*} 182 | x U_0(x) &= U_1(x)/2 \\ 183 | x U_n(x) &= {U_{n-1}(x) \over 2} + {U_{n+1}(x) \over 2}. 184 | \end{align*} 185 | \textbf{Problem 5} Use the fact that orthogonal polynomials are uniquely determined by their leading order coefficient and orthogonality to lower dimensional polynomials to show that: 186 | \[ 187 | T_n'(x) = n U_{n-1}(x). 188 | \] 189 | \textbf{Problem 6(a)} Consider Hermite polynomials orthogonal with respect to the weight $\exp(-x^2)$ on $\ensuremath{\bbR}$ with the normalisation 190 | \[ 191 | H_n(x) = 2^n x^n + O(x^{n-1}). 192 | \] 193 | Prove the Rodrigues formula 194 | \[ 195 | H_n(x) = (-1)^n \exp(x^2) {{\rm d}^n \over {\rm d}x^n} \exp(-x^2). 196 | \] 197 | Hint: use integration-by-parts. 198 | 199 | \textbf{Problem 6(b)} What are $k_n^{(1)}$ and $k_n^{(2)}$ such that 200 | \[ 201 | H_n(x) = 2^n x^n + k_n^{(1)} x^{n-1} + k_n^{(2)} x^{n-2} + O(x^{n-3}) 202 | \] 203 | \textbf{Problem 6(c)} Deduce the 3-term recurrence relationship for $H_n(x)$. 204 | 205 | \textbf{Problem 6(d)} Prove that $H_n'(x) = 2n H_{n-1}(x)$. Hint: show orthogonality of $H_n'$ to all lower degree polynomials, and that the normalisation constants match. 206 | 207 | 208 | 209 | \end{document} -------------------------------------------------------------------------------- /sheets/sheet9s.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/sheets/sheet9s.pdf -------------------------------------------------------------------------------- /slides/I.1.RectangularRule.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.1.RectangularRule.key -------------------------------------------------------------------------------- /slides/I.1.RectangularRule.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.1.RectangularRule.pdf -------------------------------------------------------------------------------- /slides/I.2.DividedDifferences.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.2.DividedDifferences.key -------------------------------------------------------------------------------- /slides/I.2.DividedDifferences.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.2.DividedDifferences.pdf -------------------------------------------------------------------------------- /slides/I.3.DualNumbers.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.3.DualNumbers.key -------------------------------------------------------------------------------- /slides/I.3.DualNumbers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.3.DualNumbers.pdf -------------------------------------------------------------------------------- /slides/I.4.NewtonMethod.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.4.NewtonMethod.key -------------------------------------------------------------------------------- /slides/I.4.NewtonMethod.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/I.4.NewtonMethod.pdf -------------------------------------------------------------------------------- /slides/II.1.Integers.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.1.Integers.key -------------------------------------------------------------------------------- /slides/II.1.Integers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.1.Integers.pdf -------------------------------------------------------------------------------- /slides/II.2.Reals.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.2.Reals.key -------------------------------------------------------------------------------- /slides/II.2.Reals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.2.Reals.pdf -------------------------------------------------------------------------------- /slides/II.3.Arithmetic.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.3.Arithmetic.key -------------------------------------------------------------------------------- /slides/II.3.Arithmetic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.3.Arithmetic.pdf -------------------------------------------------------------------------------- /slides/II.4.Intervals.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.4.Intervals.key -------------------------------------------------------------------------------- /slides/II.4.Intervals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/II.4.Intervals.pdf -------------------------------------------------------------------------------- /slides/III.1.StructuredMatrices.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.1.StructuredMatrices.key -------------------------------------------------------------------------------- /slides/III.1.StructuredMatrices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.1.StructuredMatrices.pdf -------------------------------------------------------------------------------- /slides/III.2.DifferentialEquations.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.2.DifferentialEquations.key -------------------------------------------------------------------------------- /slides/III.2.DifferentialEquations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.2.DifferentialEquations.pdf -------------------------------------------------------------------------------- /slides/III.3.Cholesky.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.3.Cholesky.key -------------------------------------------------------------------------------- /slides/III.3.Cholesky.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.3.Cholesky.pdf -------------------------------------------------------------------------------- /slides/III.4.Regression.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.4.Regression.key -------------------------------------------------------------------------------- /slides/III.4.Regression.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.4.Regression.pdf -------------------------------------------------------------------------------- /slides/III.5.OrthogonalMatrices.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.5.OrthogonalMatrices.key -------------------------------------------------------------------------------- /slides/III.5.OrthogonalMatrices.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.5.OrthogonalMatrices.pdf -------------------------------------------------------------------------------- /slides/III.6.QR.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.6.QR.key -------------------------------------------------------------------------------- /slides/III.6.QR.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/III.6.QR.pdf -------------------------------------------------------------------------------- /slides/IV.1.Fourier.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.1.Fourier.key -------------------------------------------------------------------------------- /slides/IV.1.Fourier.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.1.Fourier.pdf -------------------------------------------------------------------------------- /slides/IV.2.DFT.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.2.DFT.key -------------------------------------------------------------------------------- /slides/IV.2.DFT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.2.DFT.pdf -------------------------------------------------------------------------------- /slides/IV.3.OrthogonalPolynomials.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.3.OrthogonalPolynomials.key -------------------------------------------------------------------------------- /slides/IV.4.ClassicalOPs.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.4.ClassicalOPs.key -------------------------------------------------------------------------------- /slides/IV.5.GaussianQuadrature.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/IV.5.GaussianQuadrature.key -------------------------------------------------------------------------------- /slides/Revision.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/Revision.key -------------------------------------------------------------------------------- /slides/V.FutureDirections.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/V.FutureDirections.key -------------------------------------------------------------------------------- /slides/commandlinetutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/commandlinetutorial.pdf -------------------------------------------------------------------------------- /slides/figures/cheb_exp_101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/cheb_exp_101.pdf -------------------------------------------------------------------------------- /slides/figures/cheb_exp_11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/cheb_exp_11.pdf -------------------------------------------------------------------------------- /slides/figures/cheb_exp_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/cheb_exp_5.pdf -------------------------------------------------------------------------------- /slides/figures/chebyshevt.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/chebyshevt.pdf -------------------------------------------------------------------------------- /slides/figures/chebyshevu.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/chebyshevu.pdf -------------------------------------------------------------------------------- /slides/figures/gausserror.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/gausserror.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_analytic_10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_analytic_10.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_analytic_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_analytic_5.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_m_runge_101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_m_runge_101.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_m_runge_11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_m_runge_11.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_m_runge_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_m_runge_5.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_runge_10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_runge_10.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_runge_100.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_runge_100.pdf -------------------------------------------------------------------------------- /slides/figures/imagfft_runge_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/imagfft_runge_5.pdf -------------------------------------------------------------------------------- /slides/figures/legendrep.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/legendrep.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_analytic_10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_analytic_10.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_analytic_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_analytic_5.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_exp_101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_exp_101.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_exp_11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_exp_11.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_exp_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_exp_5.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_runge_101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_runge_101.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_runge_11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_runge_11.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_m_runge_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_m_runge_5.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_runge_10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_runge_10.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_runge_100.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_runge_100.pdf -------------------------------------------------------------------------------- /slides/figures/realfft_runge_5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/realfft_runge_5.pdf -------------------------------------------------------------------------------- /slides/figures/slides1_leftrect.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides1_leftrect.pdf -------------------------------------------------------------------------------- /slides/figures/slides1_rightrect.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides1_rightrect.pdf -------------------------------------------------------------------------------- /slides/figures/slides1_trap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides1_trap.pdf -------------------------------------------------------------------------------- /slides/figures/slides2_rightdiff_h=0.01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides2_rightdiff_h=0.01.pdf -------------------------------------------------------------------------------- /slides/figures/slides2_rightdiff_h=0.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides2_rightdiff_h=0.1.pdf -------------------------------------------------------------------------------- /slides/figures/slides2_rightdiff_h=0.3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides2_rightdiff_h=0.3.pdf -------------------------------------------------------------------------------- /slides/figures/slides2_rightdiff_h=1.0e-16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides2_rightdiff_h=1.0e-16.pdf -------------------------------------------------------------------------------- /slides/figures/slides4_funct.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides4_funct.pdf -------------------------------------------------------------------------------- /slides/figures/slides_regress_l.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides_regress_l.pdf -------------------------------------------------------------------------------- /slides/figures/slides_regress_l1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/slides/figures/slides_regress_l1.pdf -------------------------------------------------------------------------------- /somacros.tex: -------------------------------------------------------------------------------- 1 | 2 | \def\addtab#1={#1\;&=} 3 | 4 | \def\meeq#1{\def\ccr{\\\addtab} 5 | %\tabskip=\@centering 6 | \begin{align*} 7 | \addtab#1 8 | \end{align*} 9 | } 10 | 11 | \def\leqaddtab#1\leq{#1\;&\leq} 12 | \def\mleeq#1{\def\ccr{\\\addtab} 13 | %\tabskip=\@centering 14 | \begin{align*} 15 | \leqaddtab#1 16 | \end{align*} 17 | } 18 | 19 | 20 | \def\vc#1{\mbox{\boldmath$#1$\unboldmath}} 21 | 22 | \def\vcsmall#1{\mbox{\boldmath$\scriptstyle #1$\unboldmath}} 23 | 24 | \def\vczero{{\mathbf 0}} 25 | 26 | 27 | %\def\beginlist{\begin{itemize}} 28 | % 29 | %\def\endlist{\end{itemize}} 30 | 31 | 32 | \def\pr(#1){\left({#1}\right)} 33 | \def\br[#1]{\left[{#1}\right]} 34 | \def\fbr[#1]{\!\left[{#1}\right]} 35 | \def\set#1{\left\{{#1}\right\}} 36 | \def\ip<#1>{\left\langle{#1}\right\rangle} 37 | \def\iip<#1>{\left\langle\!\langle{#1}\right\rangle\!\rangle} 38 | 39 | \def\norm#1{\left\| #1 \right\|} 40 | 41 | \def\abs#1{\left|{#1}\right|} 42 | \def\fpr(#1){\!\pr({#1})} 43 | 44 | \def\Re{{\rm Re}\,} 45 | \def\Im{{\rm Im}\,} 46 | 47 | \def\floor#1{\left\lfloor#1\right\rfloor} 48 | \def\ceil#1{\left\lceil#1\right\rceil} 49 | 50 | 51 | \def\mapengine#1,#2.{\mapfunction{#1}\ifx\void#2\else\mapengine #2.\fi } 52 | 53 | \def\map[#1]{\mapengine #1,\void.} 54 | 55 | \def\mapenginesep_#1#2,#3.{\mapfunction{#2}\ifx\void#3\else#1\mapengine #3.\fi } 56 | 57 | \def\mapsep_#1[#2]{\mapenginesep_{#1}#2,\void.} 58 | 59 | 60 | \def\vcbr{\br} 61 | 62 | 63 | \def\bvect[#1,#2]{ 64 | { 65 | \def\dots{\cdots} 66 | \def\mapfunction##1{\ | \ ##1} 67 | \begin{pmatrix} 68 | \,#1\map[#2]\, 69 | \end{pmatrix} 70 | } 71 | } 72 | 73 | \def\vect[#1]{ 74 | {\def\dots{\ldots} 75 | \vcbr[{#1}] 76 | }} 77 | 78 | \def\vectt[#1]{ 79 | {\def\dots{\ldots} 80 | \vect[{#1}]^{\top} 81 | }} 82 | 83 | \def\Vectt[#1]{ 84 | { 85 | \def\mapfunction##1{##1 \cr} 86 | \def\dots{\vdots} 87 | \begin{pmatrix} 88 | \map[#1] 89 | \end{pmatrix} 90 | }} 91 | 92 | 93 | 94 | \def\thetaB{\mbox{\boldmath$\theta$}} 95 | \def\zetaB{\mbox{\boldmath$\zeta$}} 96 | 97 | 98 | \def\newterm#1{{\it #1}\index{#1}} 99 | 100 | 101 | \def\TT{{\mathbb T}} 102 | \def\C{{\mathbb C}} 103 | \def\R{{\mathbb R}} 104 | \def\II{{\mathbb I}} 105 | \def\F{{\mathcal F}} 106 | \def\E{{\rm e}} 107 | \def\I{{\rm i}} 108 | \def\D{{\rm d}} 109 | \def\dx{\D x} 110 | \def\ds{\D s} 111 | \def\dt{\D t} 112 | \def\CC{{\cal C}} 113 | \def\DD{{\cal D}} 114 | \def\U{{\mathbb U}} 115 | \def\A{{\cal A}} 116 | \def\K{{\cal K}} 117 | \def\DTU{{\cal D}_{{\rm T} \rightarrow {\rm U}}} 118 | \def\LL{{\cal L}} 119 | \def\B{{\cal B}} 120 | \def\T{{\cal T}} 121 | \def\W{{\cal W}} 122 | 123 | 124 | \def\tF_#1{{\tt F}_{#1}} 125 | \def\Fm{\tF_m} 126 | \def\Fab{\tF_{\alpha,\beta}} 127 | \def\FC{\T} 128 | \def\FCpmz{\FC^{\pm {\rm z}}} 129 | \def\FCz{\FC^{\rm z}} 130 | 131 | \def\tFC_#1{{\tt T}_{#1}} 132 | \def\FCn{\tFC_n} 133 | 134 | \def\rmz{{\rm z}} 135 | 136 | \def\chapref#1{Chapter~\ref{Chapter:#1}} 137 | \def\secref#1{Section~\ref{Section:#1}} 138 | \def\exref#1{Exercise~\ref{Exercise:#1}} 139 | \def\lmref#1{Lemma~\ref{Lemma:#1}} 140 | \def\propref#1{Proposition~\ref{Proposition:#1}} 141 | \def\warnref#1{Warning~\ref{Warning:#1}} 142 | \def\thref#1{Theorem~\ref{Theorem:#1}} 143 | \def\defref#1{Definition~\ref{Definition:#1}} 144 | \def\probref#1{Problem~\ref{Problem:#1}} 145 | \def\corref#1{Corollary~\ref{Corollary:#1}} 146 | 147 | \def\sgn{{\rm sgn}\,} 148 | \def\Ai{{\rm Ai}\,} 149 | \def\Bi{{\rm Bi}\,} 150 | \def\wind{{\rm wind}\,} 151 | \def\erf{{\rm erf}\,} 152 | \def\erfc{{\rm erfc}\,} 153 | \def\qqquad{\qquad\quad} 154 | \def\qqqquad{\qquad\qquad} 155 | 156 | 157 | \def\spand{\hbox{ and }} 158 | \def\spodd{\hbox{ odd}} 159 | \def\speven{\hbox{ even}} 160 | \def\qand{\quad\hbox{and}\quad} 161 | \def\qqand{\qquad\hbox{and}\qquad} 162 | \def\qfor{\quad\hbox{for}\quad} 163 | \def\qqfor{\qquad\hbox{for}\qquad} 164 | \def\qas{\quad\hbox{as}\quad} 165 | \def\qqas{\qquad\hbox{as}\qquad} 166 | \def\qor{\quad\hbox{or}\quad} 167 | \def\qqor{\qquad\hbox{or}\qquad} 168 | \def\qqwhere{\qquad\hbox{where}\qquad} 169 | 170 | 171 | 172 | %%% Words 173 | 174 | \def\naive{na\"\i ve\xspace} 175 | \def\Jmap{Joukowsky map\xspace} 176 | \def\Mobius{M\"obius\xspace} 177 | \def\Holder{H\"older\xspace} 178 | \def\Mathematica{{\sc Mathematica}\xspace} 179 | \def\apriori{apriori\xspace} 180 | \def\WHf{Weiner--Hopf factorization\xspace} 181 | \def\WHfs{Weiner--Hopf factorizations\xspace} 182 | 183 | \def\Jup{J_\uparrow^{-1}} 184 | \def\Jdown{J_\downarrow^{-1}} 185 | \def\Jin{J_+^{-1}} 186 | \def\Jout{J_-^{-1}} 187 | 188 | 189 | 190 | \def\bD{\D\!\!\!^-} 191 | 192 | 193 | 194 | 195 | \def\questionequals{= \!\!\!\!\!\!{\scriptstyle ? \atop }\,\,\,} 196 | 197 | \def\elll#1{\ell^{\lambda,#1}} 198 | \def\elllp{\ell^{\lambda,p}} 199 | \def\elllRp{\ell^{(\lambda,R),p}} 200 | 201 | 202 | \def\elllRpz_#1{\ell_{#1{\rm z}}^{(\lambda,R),p}} 203 | 204 | 205 | \def\sopmatrix#1{\begin{pmatrix}#1\end{pmatrix}} 206 | 207 | 208 | \def\bbR{{\mathbb R}} 209 | \def\bbC{{\mathbb C}} 210 | -------------------------------------------------------------------------------- /src/MATH50003NumericalAnalysis.jl: -------------------------------------------------------------------------------- 1 | module MATH50003NumericalAnalysis 2 | 3 | using Weave 4 | 5 | export compilenotes, compilesheet, compilesheetsolution, compilelab, compilelabsolution 6 | 7 | 8 | ##### 9 | # notes 10 | ##### 11 | 12 | 13 | 14 | function replacetheorem(path, thrm, Thrm) 15 | str = read(path, String) 16 | str = replace(str, Regex("\\\\textbf{$Thrm \\(([^}]*)\\)}(.*?)\\\\textbf{Proof}", "s") => SubstitutionString("\\\\begin{$thrm}[\\1]\\2\\\\end{$thrm}\\n\\\\textbf{Proof}")) 17 | write(path, str) 18 | end 19 | 20 | function replacedefinition(path, thrm, Thrm) 21 | str = read(path, String) 22 | str = replace(str, Regex("\\\\textbf{$Thrm \\(([^}]*)\\)}(.*?)\\\\ensuremath{\\\\QED}", "s") => SubstitutionString("\\\\begin{$thrm}[\\1]\\2\\\\end{$thrm}")) 23 | write(path, str) 24 | end 25 | 26 | function fixwhitespace(path) 27 | write(path, replace(read(path, String), "\n\n\\[" => "\n\\[")) 28 | write(path, replace(read(path, String), "\n\n\n\\begin{align" => "\n\\begin{align")) 29 | end 30 | 31 | function compilenotes(filename) 32 | weave("src/notes/$filename.jmd"; out_path="notes/", doctype="md2tex", template="src/notes/template.tpl") 33 | path = "notes/$filename.tex" 34 | replacetheorem(path, "theorem", "Theorem") 35 | replacetheorem(path, "lemma", "Lemma") 36 | replacetheorem(path, "proposition", "Proposition") 37 | replacetheorem(path, "corollary", "Corollary") 38 | replacedefinition(path, "example", "Example") 39 | replacedefinition(path, "definition", "Definition") 40 | # work around double newline before equation 41 | fixwhitespace(path) 42 | # work around meeq 43 | write(path, replace(read(path, String), r"\\\[\n\\meeq\{(.*?)\}\n\\\]"s => s"\\meeq{\1}")) 44 | end 45 | 46 | 47 | 48 | 49 | ### 50 | # sheets 51 | ### 52 | 53 | function compilesheet(k) 54 | filename = "sheet$k" 55 | write("sheets/$filename.jmd", replace(read("src/sheets/$(filename)s.jmd", String), r"\*\*SOLUTION\*\*(.*?)\*\*END\*\*"s => "")) 56 | weave("sheets/$filename.jmd"; out_path="sheets/", doctype="md2tex", template="src/sheets/template.tpl") 57 | path = "sheets/$filename.tex" 58 | # work around double newline before equation 59 | fixwhitespace(path) 60 | # work around meeq 61 | write(path, replace(read(path, String), r"\\\[\n\\meeq\{(.*?)\}\n\\\]"s => s"\\meeq{\1}")) 62 | end 63 | 64 | 65 | function compilesheetsolution(k) 66 | filename = "sheet$k" 67 | weave("src/sheets/$(filename)s.jmd"; out_path="sheets/", doctype="md2tex", template="src/sheets/template.tpl") 68 | path = "sheets/$(filename)s.tex" 69 | # work around double newline before equation 70 | fixwhitespace(path) 71 | # work around meeq 72 | write(path, replace(read(path, String), r"\\\[\n\\meeq\{(.*?)\}\n\\\]"s => s"\\meeq{\1}")) 73 | end 74 | 75 | 76 | 77 | 78 | # notebook("sheets/sheet1.jmd"; pkwds...) 79 | # notebook("src/sheets/sheet1s.jmd"; pkwds...) 80 | 81 | 82 | ##### 83 | # labs 84 | ##### 85 | 86 | import Literate 87 | 88 | function compilelab(k) 89 | write("labs/lab$k.jl", replace(replace(read("src/labs/lab$(k)s.jl", String), r"## SOLUTION(.*?)## END"s => ""))) 90 | Literate.notebook("labs/lab$k.jl", "labs/"; execute=false) 91 | end 92 | 93 | function compilelabsolution(k) 94 | Literate.notebook("src/labs/lab$(k)s.jl", "labs/"; execute=false) 95 | end 96 | 97 | end # module -------------------------------------------------------------------------------- /src/compile.jl: -------------------------------------------------------------------------------- 1 | using MATH50003NumericalAnalysis 2 | 3 | 4 | #### 5 | # Notes 6 | #### 7 | 8 | compilenotes("I.1.RectangularRule") 9 | compilenotes("I.2.DividedDifferences") 10 | compilenotes("I.3.DualNumbers") 11 | compilenotes("I.4.NewtonMethod") 12 | 13 | compilenotes("II.1.Integers") 14 | compilenotes("II.2.Reals") 15 | compilenotes("II.3.Arithmetic") 16 | compilenotes("II.4.Intervals") 17 | 18 | compilenotes("III.1.StructuredMatrices") 19 | compilenotes("III.2.DifferentialEquations") 20 | compilenotes("III.3.Cholesky") 21 | compilenotes("III.4.Regression") 22 | compilenotes("III.5.OrthogonalMatrices") 23 | compilenotes("III.6.QR") 24 | 25 | compilenotes("IV.1.Fourier") 26 | compilenotes("IV.2.DFT") 27 | compilenotes("IV.3.OrthogonalPolynomials") 28 | compilenotes("IV.4.ClassicalOPs") 29 | compilenotes("IV.5.GaussianQuadrature") 30 | 31 | 32 | compilenotes("A.Asymptotics") 33 | compilenotes("A.Permutations") 34 | 35 | 36 | #### 37 | # Sheets 38 | #### 39 | 40 | for k = 1:11 41 | compilesheet(k) 42 | end 43 | 44 | for k = 1:9 45 | compilesheetsolution(k) 46 | end 47 | 48 | #### 49 | # Labs 50 | #### 51 | for k = 1:8 52 | compilelab(k) 53 | end 54 | 55 | 56 | compilelabsolution(1) 57 | compilelabsolution(2) 58 | compilelabsolution(3) 59 | compilelabsolution(4) 60 | compilelabsolution(5) 61 | compilelabsolution(6) 62 | compilelabsolution(7) 63 | compilelabsolution(8) 64 | 65 | 66 | 67 | ##### 68 | # extras 69 | ##### 70 | 71 | using Weave 72 | 73 | nkwds = (out_path="notes/", jupyter_path="$(homedir())/.julia/conda/3/x86_64/bin/jupyter", nbconvert_options="--allow-errors") 74 | notebook("src/notes/A.Julia.jmd"; nkwds...) 75 | 76 | 77 | ### 78 | # exams 79 | ### 80 | 81 | import Literate 82 | 83 | function compileexam(str) 84 | write("exams/$str.jl", replace(replace(read("src/exams/$(str)s.jl", String), r"## SOLUTION(.*?)## END"s => ""))) 85 | Literate.notebook("exams/$str.jl", "exams/"; execute=false) 86 | end 87 | 88 | function compileexamsolution(str) 89 | Literate.notebook("src/exams/$(str)s.jl", "exams/") 90 | end 91 | 92 | compileexam("mockexam") 93 | compileexamsolution("mockexam") -------------------------------------------------------------------------------- /src/notes/A.Asymptotics.jmd: -------------------------------------------------------------------------------- 1 | We introduce Big-O, little-o and asymptotic notation and see 2 | how they can be used to describe computational cost. 3 | 4 | 5 | # Asymptotics as $n → ∞$ 6 | 7 | Big-O, little-o, and “asymptotic to" are used to describe behaviour of functions 8 | at infinity. 9 | 10 | **Definition (Big-O)** 11 | $$ 12 | f(n) = O(ϕ(n)) \qquad \hbox{(as $n → ∞$)} 13 | $$ 14 | means $\left|{f(n) \over ϕ(n)}\right|$ 15 | is bounded for sufficiently large $n$. That is, 16 | there exist constants $C$ and $N_0$ such 17 | that, for all $n \geq N_0$, $|{f(n) \over ϕ(n)}| \leq C$. 18 | ∎ 19 | 20 | **Definition (little-O)** 21 | $$ 22 | f(n) = o(ϕ(n)) \qquad \hbox{(as $n → ∞$)} 23 | $$ 24 | means $\lim_{n → ∞} {f(n) \over ϕ(n)} = 0.$ 25 | ∎ 26 | 27 | **Definition (asymptotic to)** 28 | $$ 29 | f(n) ∼ ϕ(n) \qquad \hbox{(as $n → ∞$)} 30 | $$ 31 | means $\lim_{n → ∞} {f(n) \over ϕ(n)} = 1.$ 32 | ∎ 33 | 34 | **Example (asymptotics with $n$)** 35 | 1. ${\cos n \over n^2 -1} = O(n^{-2})$ as 36 | $$ 37 | \left|{{\cos n \over n^2 -1} \over n^{-2}} \right| \leq \left| n^2 \over n^2 -1 \right| \leq 2 38 | $$ 39 | for $n \geq N_0 = 2$. 40 | 41 | 2. $\log n = o(n)$ as $\lim_{n → ∞} {\log n \over n} = 0.$ 42 | 43 | 3. $n^2 + 1 ∼ n^2$ as ${n^2 +1 \over n^2} → 1.$ 44 | ∎ 45 | 46 | Note we sometimes write $f(O(ϕ(n)))$ for a function of the form 47 | $f(g(n))$ such that $g(n) = O(ϕ(n))$. 48 | 49 | 50 | We have some simple algebraic rules: 51 | 52 | **Proposition (Big-O rules)** 53 | $$ 54 | \begin{align*} 55 | O(ϕ(n))O(ψ(n)) = O(ϕ(n)ψ(n)) \qquad \hbox{(as $n → ∞$)} \\ 56 | O(ϕ(n)) + O(ψ(n)) = O(|ϕ(n)| + |ψ(n)|) \qquad \hbox{(as $n → ∞$)}. 57 | \end{align*} 58 | $$ 59 | **Proof** 60 | See any standard book on asymptotics, eg [F.W.J. Olver, Asymptotics and Special Functions](https://www.taylorfrancis.com/books/mono/10.1201/9781439864548/asymptotics-special-functions-frank-olver). 61 | ∎ 62 | 63 | 64 | # Asymptotics as $x → x_0$ 65 | 66 | We also have Big-O, little-o and "asymptotic to" at a point: 67 | 68 | **Definition (Big-O)** 69 | $$ 70 | f(x) = O(ϕ(x)) \qquad \hbox{(as $x → x_0$)} 71 | $$ 72 | means $|{f(x) \over ϕ(x)}|$ 73 | is bounded in a neighbourhood of $x_0$. That is, 74 | there exist constants $C$ and $r$ such 75 | that, for all $0 \leq |x - x_0| \leq r$, $|{f(x) \over ϕ(x)}| \leq C$. 76 | ∎ 77 | 78 | **Definition (little-O)** 79 | $$ 80 | f(x) = o(ϕ(x)) \qquad \hbox{(as $x → x_0$)} 81 | $$ 82 | means $\lim_{x → x_0} {f(x) \over ϕ(x)} = 0.$ 83 | ∎ 84 | 85 | **Definition (asymptotic to)** 86 | $$ 87 | f(x) ∼ ϕ(x) \qquad \hbox{(as $x → x_0$)} 88 | $$ 89 | means $\lim_{x → x_0} {f(x) \over ϕ(x)} = 1.$ 90 | ∎ 91 | 92 | **Example (asymptotics with $x$)** 93 | $$ 94 | \exp x = 1 + x + O(x^2) \qquad \hbox{as $x → 0$} 95 | $$ 96 | since $\exp x = 1 + x + {\exp t \over 2} x^2$ for some $t \in [0,x]$ and 97 | $$ 98 | \left|{{\exp t \over 2} x^2 \over x^2}\right| \leq {3 \over 2} 99 | $$ 100 | provided $x \leq 1$. 101 | ∎ 102 | 103 | 104 | # Computational cost 105 | 106 | We will use Big-O notation to describe the computational cost of algorithms. 107 | Consider the following simple sum 108 | $$ 109 | \sum_{k=1}^n x_k^2 110 | $$ 111 | which we might implement as: 112 | ```julia 113 | function sumsq(x) 114 | n = length(x) 115 | ret = 0.0 116 | for k = 1:n 117 | ret = ret + x[k]^2 118 | end 119 | ret 120 | end 121 | ``` 122 | Each step of this algorithm consists of one memory look-up (`z = x[k]`), 123 | one multiplication (`w = z*z`) and one addition (`ret = ret + w`). 124 | We will ignore the memory look-up in the following discussion. 125 | The number of CPU operations per step is therefore 2 (the addition and multiplication). 126 | Thus the total number of CPU operations is $2n$. But the constant $2$ here is 127 | misleading: we didn't count the memory look-up, thus it is more sensible to 128 | just talk about the asymptotic complexity, that is, the _computational cost_ is $O(n)$. 129 | 130 | Now consider a double sum like: 131 | $$ 132 | \sum_{k=1}^n \sum_{j=1}^k x_j^2 133 | $$ 134 | which we might implement as: 135 | ```julia 136 | function sumsq2(x) 137 | n = length(x) 138 | ret = 0.0 139 | for k = 1:n 140 | for j = 1:k 141 | ret = ret + x[j]^2 142 | end 143 | end 144 | ret 145 | end 146 | ``` 147 | 148 | Now the inner loop is $O(1)$ operations (we don't try to count the precise number), 149 | which we do $k$ times for $O(k)$ operations as $k → ∞$. The outer loop therefore takes 150 | $$ 151 | ∑_{k = 1}^n O(k) = O\left(∑_{k = 1}^n k\right) = O\left( {n (n+1) \over 2} \right) = O(n^2) 152 | $$ 153 | operations. -------------------------------------------------------------------------------- /src/notes/A.Permutations.jmd: -------------------------------------------------------------------------------- 1 | Permutation matrices are matrices that represent the action of permuting the entries of a vector, 2 | that is, matrix representations of the symmetric group $S_n$, acting on $ℝ^n$. 3 | Recall every $σ ∈ S_n$ is a bijection between $\{1,2,…,n\}$ and itself. 4 | We can write a permutation $σ$ in _Cauchy notation_: 5 | $$ 6 | \begin{pmatrix} 7 | 1 & 2 & 3 & ⋯ & n \cr 8 | σ_1 & σ_2 & σ_3 & ⋯ & σ_n 9 | \end{pmatrix} 10 | $$ 11 | where $\{σ_1,…,σ_n\} = \{1,2,…,n\}$ (that is, each integer appears precisely once). 12 | We denote the _inverse permutation_ by $σ^{-1}$, which can be constructed by swapping the rows of 13 | the Cauchy notation and reordering. 14 | 15 | We can encode a permutation in vector $\mathbf σ = [σ_1,…,σ_n]$. 16 | This induces an action on a vector (using indexing notation) 17 | $$ 18 | 𝐯[\mathbf σ] = \begin{bmatrix}v_{σ_1}\\ \vdots \\ v_{σ_n} \end{bmatrix} 19 | $$ 20 | 21 | 22 | **Example (permutation of a vector)** 23 | Consider the permutation $σ$ given by 24 | $$ 25 | \begin{pmatrix} 26 | 1 & 2 & 3 & 4 & 5 \cr 27 | 1 & 4 & 2 & 5 & 3 28 | \end{pmatrix} 29 | $$ 30 | We can apply it to a vector: 31 | ```julia 32 | using LinearAlgebra 33 | σ = [1, 4, 2, 5, 3] 34 | v = [6, 7, 8, 9, 10] 35 | v[σ] # we permutate entries of v 36 | ``` 37 | Its inverse permutation $σ^{-1}$ has Cauchy notation coming from swapping the rows of 38 | the Cauchy notation of $σ$ and sorting: 39 | $$ 40 | \begin{pmatrix} 41 | 1 & 4 & 2 & 5 & 3 \cr 42 | 1 & 2 & 3 & 4 & 5 43 | \end{pmatrix} \rightarrow \begin{pmatrix} 44 | 1 & 2 & 4 & 3 & 5 \cr 45 | 1 & 3 & 2 & 5 & 4 46 | \end{pmatrix} 47 | $$ 48 | ∎ 49 | 50 | Note that the operator 51 | $$ 52 | P_σ(𝐯) = 𝐯[{\mathbf σ}] 53 | $$ 54 | is linear in $𝐯$, therefore, we can identify it with a matrix whose action is: 55 | $$ 56 | P_σ \begin{bmatrix} v_1\\ \vdots \\ v_n \end{bmatrix} = \begin{bmatrix}v_{σ_1} \\ \vdots \\ v_{σ_n} \end{bmatrix}. 57 | $$ 58 | The entries of this matrix are 59 | $$ 60 | P_σ[k,j] = 𝐞_k^⊤ P_σ 𝐞_j = 𝐞_k^⊤ 𝐞_{σ^{-1}_j} = δ_{k,σ^{-1}_j} = δ_{σ_k,j} 61 | $$ 62 | where $δ_{k,j}$ is the _Kronecker delta_: 63 | $$ 64 | δ_{k,j} := \begin{cases} 1 & k = j \\ 65 | 0 & \hbox{otherwise} 66 | \end{cases}. 67 | $$ 68 | 69 | 70 | This construction motivates the following definition: 71 | 72 | **Definition (permutation matrix)** $P \in ℝ^{n × n}$ is a permutation matrix if it is equal to 73 | the identity matrix with its rows permuted. 74 | ∎ 75 | 76 | **Proposition (permutation matrix inverse)** 77 | Let $P_σ$ be a permutation matrix corresponding to the permutation $σ$. Then 78 | $$ 79 | P_σ^⊤ = P_{σ^{-1}} = P_σ^{-1} 80 | $$ 81 | That is, $P_σ$ is _orthogonal_: 82 | $$ 83 | P_σ^⊤ P_σ = P_σ P_σ^⊤ = I. 84 | $$ 85 | 86 | **Proof** 87 | 88 | We prove orthogonality via: 89 | $$ 90 | 𝐞_k^⊤ P_σ^⊤ P_σ 𝐞_j = (P_σ 𝐞_k)^⊤ P_σ 𝐞_j = 𝐞_{σ^{-1}_k}^⊤ 𝐞_{σ^{-1}_j} = δ_{k,j} 91 | $$ 92 | This shows $P_σ^⊤ P_σ = I$ and hence $P_σ^{-1} = P_σ^⊤$. 93 | 94 | ∎ 95 | -------------------------------------------------------------------------------- /src/notes/I.1.RectangularRule.jmd: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Rectangular rule 4 | 5 | One possible definition for an integral is the limit of a Riemann sum, for example: 6 | $$ 7 | ∫_a^b f(x) {\rm d}x = \lim_{n → ∞} h ∑_{j=1}^n f(x_j) 8 | $$ 9 | where $x_j = a+jh$ are evenly spaced points dividing up the interval $[a,b]$, that 10 | is with the _step size_ $h = (b-a)/n$. 11 | This suggests an algorithm known as the _(right-sided) rectangular rule_ 12 | for approximating an integral: choose $n$ large so that 13 | $$ 14 | ∫_a^b f(x) {\rm d}x ≈ h ∑_{j=1}^n f(x_j). 15 | $$ 16 | In the lab we explore practical implementation of this approximation, and observe that 17 | the error in approximation is bounded by $C/n$ for some constant $C$. This can be expressed using 18 | “Big-O" notation: 19 | $$ 20 | ∫_a^b f(x) {\rm d}x = h ∑_{j=1}^n f(x_j) + O(1/n). 21 | $$ 22 | 23 | In these notes we consider the “Analysis" part of “Numerical Analysis": we want to _prove_ the 24 | convergence rate of the approximation, including finding an explicit expression for the constant $C$. 25 | 26 | To tackle this question we consider the error incurred on a single panel $(x_{j-1},x_j)$, then sum up the errors on rectangles. 27 | 28 | Now for a secret. There are only so many tools available in analysis (especially at this stage of your career), and 29 | one can make a safe bet that the right tool in any analysis proof is either (1) integration-by-parts, (2) geometric series 30 | or (3) Taylor series. In this case we use (1): 31 | 32 | **Lemma ((Right-sided) Rectangular Rule error on one panel)** 33 | Assuming $f$ is differentiable we have 34 | $$ 35 | ∫_a^b f(x) {\rm d}x = (b-a) f(b) + δ 36 | $$ 37 | where $|δ| ≤ M (b-a)^2$ for $M = \sup_{a ≤ x ≤ b}|f'(x)|$. 38 | 39 | **Proof** 40 | We write 41 | $$ 42 | \meeq{ 43 | ∫_a^b f(x) {\rm d}x = ∫_a^b (x-a)' f(x) {\rm d}x = [(x-a) f(x)]_a^b - ∫_a^b (x-a) f'(x) {\rm d} x \ccr 44 | = (b-a) f(b) + \underbrace{\left(-∫_a^b (x-a) f'(x) {\rm d} x \right)}_δ. 45 | } 46 | $$ 47 | Recall that we can bound the absolute value of an integral by the supremum of the integrand 48 | times the width of the integration interval: 49 | $$ 50 | \abs{∫_a^b g(x) {\rm d} x} ≤ (b-a) \sup_{a ≤ x ≤ b}|g(x)|. 51 | $$ 52 | The lemma thus follows since 53 | $$ 54 | \begin{align*} 55 | \abs{∫_a^b (x-a) f'(x) {\rm d} x} &≤ (b-a) \sup_{a ≤ x ≤ b}|(x-a) f'(x)| \\ 56 | &≤ (b-a) \sup_{a ≤ x ≤ b}|x-a| \sup_{a ≤ x ≤ b}|f'(x)|\\ 57 | &≤ M (b-a)^2. 58 | \end{align*} 59 | $$ 60 | ∎ 61 | 62 | Now summing up the errors in each panel gives us the error of using the Rectangular rule: 63 | 64 | **Theorem (Rectangular Rule error)** 65 | Assuming $f$ is differentiable we have 66 | $$ 67 | ∫_a^b f(x) {\rm d}x = h ∑_{j=1}^n f(x_j) + δ 68 | $$ 69 | where $|δ| ≤ M (b-a) h$ for $M = \sup_{a ≤ x ≤ b}|f'(x)|$, $h = (b-a)/n$ and $x_j = a + jh$. 70 | 71 | **Proof** 72 | We split the integral into a sum of smaller integrals: 73 | $$ 74 | ∫_a^b f(x) {\rm d}x = ∑_{j=1}^n ∫_{x_{j-1}}^{x_j} f(x) {\rm d}x = 75 | ∑_{j=1}^n \br[(x_j - x_{j-1}) f(x_j) + δ_j] = h ∑_{j=1}^n f(x_j) + \underbrace{∑_{j=1}^n δ_j}_δ 76 | $$ 77 | where $δ_j$, the error on each panel as in the preceding lemma, satisfies 78 | $$ 79 | |δ_j| ≤ (x_j-x_{j-1})^2 \sup_{x_{j-1} ≤ x ≤ x_j}|f'(x)| ≤ M h^2. 80 | $$ 81 | Thus using the triangular inequality we have 82 | $$ 83 | |δ| = \abs{ ∑_{j=1}^n δ_j} ≤ ∑_{j=1}^n |δ_j| ≤ M n h^2 = M(b-a)h. 84 | $$ 85 | ∎ 86 | 87 | Note a consequence of this lemma is that the approximation converges as $n → ∞$ (i.e. $h → 0$). In the labs and problem 88 | sheets we will consider the left-sided rule: 89 | $$ 90 | ∫_a^b f(x) {\rm d}x ≈ h ∑_{j=0}^{n-1} f(x_j). 91 | $$ 92 | We also consider the _Trapezium rule_. Here we approximate an integral by an affine function: 93 | $$ 94 | ∫_a^b f(x) {\rm d} x ≈ ∫_a^b {(b-x)f(a) + (x-a)f(b) \over b-a} \dx 95 | = {b-a \over 2} \br[f(a) + f(b)]. 96 | $$ 97 | Subdividing an interval $a = x_0 < x_1 < … < x_n = b$ and applying this approximation separately on 98 | each subinterval $[x_{j-1},x_j]$, where $h = (b-a)/n$ and $x_j = a + jh$, leads to the approximation 99 | $$ 100 | ∫_a^b f(x) {\rm d}x ≈ {h \over 2} f(a) + h ∑_{j=1}^{n-1} f(x_j) + {h \over 2} f(b) 101 | $$ 102 | We shall see both experimentally and provably that this approximation converges faster than the 103 | rectangular rule. 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /src/notes/I.2.DividedDifferences.jmd: -------------------------------------------------------------------------------- 1 | # Divided Differences 2 | 3 | Given a function, how can we approximate its derivative at a 4 | point? We consider an intuitive approach to this problem using _(Right-sided) Divided Differences_: 5 | $$ 6 | f'(x) ≈ {f(x+h) - f(x) \over h} 7 | $$ 8 | Note by the definition of the derivative we know that this approximation will converge 9 | to the true derivative as $h → 0$. But in numerical approimxations we also 10 | need to consider the rate of convergence. 11 | 12 | Now in the previous section I mentioned there are three basic tools in analysis: 13 | (1) integration-by-parts, (2) geometric series 14 | or (3) Taylor series. In this case we use (3): 15 | 16 | **Proposition (divided differences error)** 17 | Suppose that $f$ is twice-differentiable on the interval $[x,x+h]$. 18 | The error in approximating the derivative using divided differences is 19 | $$ 20 | f'(x) = {f(x+h) - f(x) \over h} + δ 21 | $$ 22 | where $|δ| ≤ Mh/2$ for $M = \sup_{x ≤ t ≤ x+h} |f''(t)|$. 23 | 24 | **Proof** 25 | Follows immediately from Taylor's theorem: 26 | $$ 27 | f(x+h) = f(x) + f'(x) h + \underbrace{{f''(t) \over 2} h^2}_{h δ} 28 | $$ 29 | for some $x ≤ t ≤ x+h$, by bounding: 30 | $$ 31 | |δ| ≤ \abs{{f''(t) \over 2} h} ≤ {M h \over 2}. 32 | $$ 33 | 34 | ∎ 35 | 36 | Unlike the rectangular rule, the computational cost of computing the divided difference is 37 | independent of $h$! We only need to evaluate a function $f$ twice and do a single division. 38 | Here we are assuming that the computational cost of evaluating $f$ is independent of the point 39 | of evaluation. Later we will investigate the details of how computers work with numbers via 40 | floating point, and confirm that this is a sensible assumption. 41 | 42 | So why not just set $h$ ridiculously small? In the lab we explore this question and observe that 43 | there are significant errors introduced in the numerical realisation of this algorithm. 44 | We will return to the question of understanding these errors after learning floating point numbers. 45 | 46 | 47 | There are alternative versions of divided differences. Left-side divided differences evaluates to the left 48 | of the point where we wish to know the derivative: 49 | $$ 50 | f'(x) ≈ {f(x) - f(x-h) \over h} 51 | $$ 52 | and central differences: 53 | $$ 54 | f'(x) ≈ {f(x + h) - f(x - h) \over 2h} 55 | $$ 56 | We can further arrive at an approximation to the second derivative by composing a left- and right-sided 57 | finite difference: 58 | $$ 59 | f''(x) ≈ {f'(x+h) - f'(x) \over h} ≈ {{f(x+h) - f(x) \over h} - {f(x) - f(x-h) \over h} \over h} 60 | = {f(x+h) - 2f(x) + f(x-h) \over h^2} 61 | $$ 62 | In the lab we investigate the convergence rate of these approximations (in particular, that 63 | central differences is more accurate than standard divided differences) and observe that they too 64 | suffer from unexplained (for now) loss of accuracy as $h → 0$. In the problem sheet we prove the theoretical 65 | convergence rate, which is never realised because of these errors. -------------------------------------------------------------------------------- /src/notes/I.3.DualNumbers.jmd: -------------------------------------------------------------------------------- 1 | # Dual Numbers 2 | 3 | In this section we introduce a mathematically beautiful alternative to divided differences for computing 4 | derivatives: _dual numbers_. These are a commutative ring that _exactly_ compute derivatives, 5 | which when implemented on a computer gives very high-accuracy approximations to derivatives. They 6 | underpin forward-mode [automatic differentation](https://en.wikipedia.org/wiki/Automatic_differentiation). 7 | Automatic differentiation is a basic tool in Machine Learning for computing gradients necessary 8 | for training neural networks. 9 | 10 | 11 | **Definition (Dual numbers)** Dual numbers $𝔻$ are a commutative ring (over $ℝ$) 12 | generated by $1$ and $ϵ$ such that $ϵ^2 = 0$. 13 | Dual numbers are typically written as $a + b ϵ$ where $a$ and $b$ are real. 14 | ∎ 15 | 16 | This is very much analoguous to complex numbers, which are a field generated by $1$ and $\I$ such that 17 | $\I^2 = -1$. Compare multiplication of each number type: 18 | $$ 19 | \meeq{ 20 | (a + b \I) (c + d \I) = ac + (bc + ad) \I + bd \I^2 = ac -bd + (bc + ad) \I \ccr 21 | (a + b ϵ) (c + d ϵ) = ac + (bc + ad) ϵ + bd ϵ^2 = ac + (bc + ad) ϵ 22 | } 23 | $$ 24 | And just as we view $ℝ ⊂ ℂ$ by equating $a ∈ ℝ$ with $a + 0\I ∈ ℂ$, 25 | we can view $ℝ ⊂ 𝔻$ by equating $a ∈ ℝ$ with $a + 0{\rm ϵ} ∈ 𝔻$. 26 | 27 | ## Differentiating polynomials 28 | 29 | Polynomials evaluated on dual numbers are well-defined as 30 | they depend only on the operations $+$ and $*$. From the formula for multiplication of dual numbers 31 | we deduce that evaluating a polynomial at a dual number $a + b ϵ$ tells us the derivative of the polynomial at $a$: 32 | 33 | **Theorem (polynomials on dual numbers)** Suppose $p$ is a polynomial. Then 34 | $$ 35 | p(a + b ϵ) = p(a) + b p'(a) ϵ 36 | $$ 37 | 38 | **Proof** 39 | 40 | First consider $p(x) = x^n$ for $n ≥ 0$. 41 | The cases $n = 0$ and $n = 1$ are immediate. For $n > 1$ we have by induction: 42 | $$ 43 | (a + b ϵ)^n = (a + b ϵ) (a + b ϵ)^{n-1} = (a + b ϵ) (a^{n-1} + (n-1) b a^{n-2} ϵ) = a^n + b n a^{n-1} ϵ. 44 | $$ 45 | For a more general polynomial 46 | $$ 47 | p(x) = ∑_{k=0}^n c_k x^k 48 | $$ 49 | the result follows from linearity: 50 | $$ 51 | p(a + b ε) = ∑_{k=0}^n c_k (a+bϵ)^k = c_0 + ∑_{k=1}^n c_k (a^k +k b a^{k-1}ϵ) 52 | = ∑_{k=0}^n c_k a^k + b ∑_{k=1}^n c_k k a^{k-1}ϵ = p(a) + b p'(a) ϵ. 53 | $$ 54 | 55 | ∎ 56 | 57 | **Example (differentiating polynomial)** Consider computing $p'(2)$ where 58 | $$ 59 | p(x) = (x-1)(x-2) + x^2. 60 | $$ 61 | We can use dual numbers to differentiate, avoiding expanding in monomials 62 | or applying rules of differentiating: 63 | $$ 64 | p(2+ϵ) = (1+ϵ)ϵ + (2+ϵ)^2 = ϵ + 4 + 4ϵ = 4 + \underbrace{5}_{p'(2)}ϵ 65 | $$ 66 | ∎ 67 | 68 | 69 | ## Differentiating other functions 70 | 71 | 72 | We can extend real-valued differentiable functions to dual numbers in a similar manner. 73 | First, consider a standard function with a Taylor series (e.g. ${\rm cos}$, ${\rm sin}$, ${\rm exp}$, etc.) 74 | $$ 75 | f(x) = ∑_{k=0}^∞ f_k x^k 76 | $$ 77 | so that $a$ is inside the radius of convergence. This leads naturally to a definition on dual numbers: 78 | $$ 79 | \meeq{ 80 | f(a + b ϵ) = ∑_{k=0}^∞ f_k (a + b ϵ)^k = f_0 + ∑_{k=1}^∞ f_k (a^k + k a^{k-1} b ϵ) = ∑_{k=0}^∞ f_k a^k + ∑_{k=1}^∞ f_k k a^{k-1} b ϵ \ccr 81 | = f(a) + b f'(a) ϵ 82 | } 83 | $$ 84 | More generally, given a differentiable function we can extend it to dual numbers: 85 | 86 | **Definition (dual extension)** Suppose a real-valued function $f$ 87 | is differentiable at $a$. If 88 | $$ 89 | f(a + b ϵ) = f(a) + b f'(a) ϵ 90 | $$ 91 | then we say that it is a _dual extension at_ $a$. 92 | 93 | Thus, for basic functions we have natural extensions: 94 | $$ 95 | \begin{align*} 96 | \exp(a + b ϵ) &:= \exp(a) + b \exp(a) ϵ \\ 97 | \sin(a + b ϵ) &:= \sin(a) + b \cos(a) ϵ \\ 98 | \cos(a + b ϵ) &:= \cos(a) - b \sin(a) ϵ \\ 99 | \log(a + b ϵ) &:= \log(a) + {b \over a} ϵ \\ 100 | \sqrt{a+b ϵ} &:= \sqrt{a} + {b \over 2 \sqrt{a}} ϵ \\ 101 | |a + b ϵ| &:= |a| + b\, {\rm sign} a\, ϵ 102 | \end{align*} 103 | $$ 104 | provided the function is differentiable at $a$. Note the last example does not have 105 | a convergent Taylor series (at 0) but we can still extend it where it is differentiable. 106 | 107 | Going further, we can add, multiply, and compose such functions: 108 | 109 | **Lemma (product and chain rule)** 110 | If $f$ is a dual extension at $g(a)$ and $g$ 111 | is a dual extension at $a$, then $q(x) := f(g(x))$ is a dual extension at $a$. 112 | If $f$ and $g$ are dual extensions at $a$ then 113 | $r(x) := f(x) g(x)$ is also dual extensions at $a$. In other words: 114 | $$ 115 | \meeq{ 116 | q(a+b ϵ) = q(a) + b q'(a) ϵ \ccr 117 | r(a+b ϵ) = r(a) + b r'(a) ϵ 118 | } 119 | $$ 120 | 121 | **Proof** 122 | For $q$ it follows immediately: 123 | $$ 124 | \meeq{ 125 | q(a + b ϵ) = f(g(a + b ϵ)) = f(g(a) + b g'(a) ϵ) \ccr 126 | = f(g(a)) + b g'(a) f'(g(a))ϵ = q(a) + b q'(a) ϵ. 127 | } 128 | $$ 129 | For $r$ we have 130 | $$ 131 | \meeq{ 132 | r(a + b ϵ) = f(a+b ϵ )g(a+b ϵ )= (f(a) + b f'(a) ϵ)(g(a) + b g'(a) ϵ) \ccr 133 | = f(a)g(a) + b (f'(a)g(a) + f(a)g'(a)) ϵ = r(a) +b r'(a) ϵ. 134 | } 135 | $$ 136 | 137 | ∎ 138 | 139 | A simple corollary is that any function defined in terms of addition, multiplication, composition, etc. 140 | of functions that are dual with differentiation will be differentiable via dual numbers. 141 | 142 | **Example (differentiating non-polynomial)** 143 | 144 | Consider differentiating $f(x) = \exp(x^2 + \E^x)$ at the point $a = 1$ by evaluating on the duals: 145 | $$ 146 | f(1 + ϵ) = \exp(1 + 2ϵ + \E + \E ϵ) = \exp(1 + \E) + \exp(1 + \E) (2 + \E) ϵ. 147 | $$ 148 | Therefore we deduce that 149 | $$ 150 | f'(1) = \exp(1 + \E) (2 + \E). 151 | $$ 152 | 153 | ∎ 154 | 155 | -------------------------------------------------------------------------------- /src/notes/I.4.NewtonMethod.jmd: -------------------------------------------------------------------------------- 1 | # Newton's method 2 | 3 | In school you may recall learning Newton's method: a way of approximating zeros/roots to 4 | a function by using a local approximation by an affine function. That is, approximate a function 5 | $f(x)$ locally around an initial guess $x_0$ by its first order Taylor series: 6 | $$ 7 | f(x) ≈ f(x_0) + f'(x_0) (x-x_0) 8 | $$ 9 | and then find the root of the right-hand side which is 10 | $$ 11 | f(x_0) + f'(x_0) (x-x_0) = 0 ⇔ x = x_0 - {f(x_0) \over f'(x_0)}. 12 | $$ 13 | We can then repeat using this root as the new initial guess. In other words 14 | we have a sequence of _hopefully_ more accurate approximations: 15 | $$ 16 | x_{k+1} = x_k - {f(x_k) \over f'(x_k)}. 17 | $$ 18 | The convergence theory of Newton's method is rich and beautiful but outside 19 | the scope of this module. But provided $f$ is smooth, if $x_0$ is sufficiently 20 | close to a root this iteration will converge. 21 | 22 | Thus _if_ we can compute derivatives, we can (sometimes) compute roots. The lab 23 | will explore using dual numbers to accomplish this task. This is in some sense 24 | a baby version of how Machine Learning algorithms train neural networks. -------------------------------------------------------------------------------- /src/notes/II.4.Intervals.jmd: -------------------------------------------------------------------------------- 1 | # Interval Arithmetic 2 | 3 | 4 | It is possible to use rounding modes (up/down) 5 | to do rigorous computation 6 | to compute bounds on the error in, for example, the digits of $\E$. To do this we will use set/interval 7 | arithmetic. For sets $X,Y ⊆ ℝ$, the set arithmetic operations are defined as 8 | $$ 9 | \begin{align*} 10 | X + Y &:= \{x + y : x ∈ X, y ∈ Y\}, \\ 11 | XY &:= \{xy : x ∈ X, y ∈ Y\}, \\ 12 | X/Y &:= \{x/y : x ∈ X, y ∈ Y\} 13 | \end{align*} 14 | $$ 15 | We will use floating point arithmetic to construct approximate set operations $⊕$, $⊗$ so that 16 | $$ 17 | \begin{align*} 18 | X + Y &⊆ X ⊕ Y, \\ 19 | XY &⊆ X ⊗ Y,\\ 20 | X/Y &⊆ X ⊘ Y 21 | \end{align*} 22 | $$ 23 | thereby a complicated algorithm can be run on sets and the true result is guaranteed to be 24 | a subset of the output. 25 | 26 | 27 | When our sets are intervals we can deduce simple formulas for basic arithmetic operations. 28 | For simplicity we only consider the case where all values are positive. 29 | 30 | **Proposition (interval bounds)** For intervals $X = [a,b]$ and $Y = [c,d]$ satisfying 31 | $0 < a ≤ b$ and $0 < c ≤ d$, and $n > 0$, we have: 32 | $$ 33 | \meeq{ 34 | X + Y = [a+c, b+d] \ccr 35 | X/n = [a/n,b/n] \ccr 36 | XY = [ac, bd] 37 | } 38 | $$ 39 | 40 | **Proof** 41 | We first show $X+Y ⊆ [a+c,b+d]$. If $z ∈ X + Y$ then $z = x+y$ such that $a ≤ x ≤ b$ and $c ≤ y ≤ d$ and 42 | therefore $a + c ≤ z ≤ c + d$ and $z ∈ [a+c,b+d]$. Equality follows from convexity. First note that 43 | $a+c, b+d ∈ X+Y$. Any point $z ∈ [a+b,c+d]$ can be written as a convex combination of the two endpoints: 44 | there exists $0 ≤ t ≤ 1$ such that 45 | $$ 46 | z = (1-t) (a+c) + t (b+d) = \underbrace{(1-t) a + t b}_x + \underbrace{(1-t) c + t d}_y 47 | $$ 48 | Because intervals are convex we have $x ∈ X$ and $y ∈ Y$ and hence $z ∈ X+Y$. 49 | 50 | The remaining two proofs are left for the problem sheet. 51 | 52 | ∎ 53 | 54 | We want to implement floating point variants of these operations that are guaranteed to contain 55 | the true set arithmetic operations. We do so as follows: 56 | 57 | 58 | **Definition (floating point interval arithmetic)** 59 | For intervals $A = [a,b]$ and $B = [c,d]$ satisfying 60 | $0 < a ≤ b$ and $0 < c ≤ d$, and $n > 0$, define: 61 | $$ 62 | \begin{align*} 63 | [a,b] ⊕ [c,d] &:= [\fldown(a+c), \flup(b+d)] \\ 64 | [a,b] ⊖ [c,d] &:= [\fldown(a-d), \flup(b-c)] \\ 65 | [a,b] ⊘ n &:= [\fldown(a/n), \flup(b/n)] \\ 66 | [a,b] ⊗ [c,d] &:= [\fldown(ac), \flup(bd)] 67 | \end{align*} 68 | $$ 69 | ∎ 70 | 71 | **Example (small sum)** consider evaluating the first few terms in the Taylor series of the exponential 72 | at $x = 1$ using interval arithmetic with half-precision $F_{16}$ arithmetic. 73 | The first three terms are exact since all numbers involved are exactly floats, in particular if we evaluate 74 | $1 + x + x^2/2$ with $x = 1$ we get 75 | $$ 76 | 1 + 1 + 1/2 ∈ 1 ⊕ [1,1] ⊕ ([1,1] ⊗ [1,1]) ⊘ 2 = [5/2, 5/2] 77 | $$ 78 | Noting that 79 | $$ 80 | 1/6 = (1/3)/2 = 2^{-3} (1.01010101…)_2 81 | $$ 82 | we can extend the computation to another term: 83 | $$ 84 | \begin{align*} 85 | 1 + 1 + 1/2 + 1/6 &∈ [5/2,5/2] ⊕ ([1,1] ⊘ 6) \ccr 86 | = [2 (1.01)_2, 2 (1.01)_2] ⊕ 2^{-3}[(1.0101010101)_2, (1.0101010110)_2] \ccr 87 | = [\fldown(2 (1.0101010101\red{0101})_2), \flup(2 (1.0101010101\red{011})_2)] \ccr 88 | = [2(1.0101010101)_2, 2(1.0101010110)_2] \ccr 89 | = [2.666015625, 2.66796875] 90 | \end{align*} 91 | $$ 92 | ∎ 93 | 94 | 95 | **Example (exponential with intervals)** Consider computing $\exp(x)$ for $0 ≤ x ≤ 1$ 96 | from the Taylor series approximation: 97 | $$ 98 | \exp(x) = \sum_{k=0}^n {x^k \over k!} + \underbrace{\exp(t){x^{n+1} \over (n+1)!}}_{δ_{x,n}} 99 | $$ 100 | where we can bound the error by (using the fact that $ℯ = 2.718… ≤ 3$) 101 | $$ 102 | |δ_{x,n}| ≤ {\exp(1) \over (n+1)!} ≤ {3 \over (n+1)!}. 103 | $$ 104 | Put another way: $δ_{x,n} ∈ \left[-{3 \over (n+1)!}, {3 \over (n+1)!}\right]$. 105 | We can use this to adjust the bounds derived from interval arithmetic for the interval 106 | arithmetic expression: 107 | $$ 108 | \exp(X) ⊆ \left(⨁_{k=0}^n {X \ocaret k ⊘ k!}\right) ⊕ \left[\fldown\fpr(-{3 \over (n+1)!}), \flup\fpr({3 \over (n+1)!})\right] 109 | $$ 110 | For example, with $n = 3$ we have $|δ_{1,2}| ≤ 3/4! = 1/2^3$. Thus we can prove that: 111 | $$ 112 | \meeq{ 113 | ℯ = 1 + 1 + 1/2 + 1/6 + δ_x ∈ [2(1.0101010101)_2, 2(1.0101010110)_2] ⊕ [-1/2^3, 1/2^3] \ccr 114 | = [2(1.0100010101)_2, 2(1.0110010110)_2] = [2.541015625,2.79296875] 115 | } 116 | $$ 117 | In the lab we get many more digits by using a computer to compute the bounds. 118 | ∎ -------------------------------------------------------------------------------- /src/notes/III.4.Regression.jmd: -------------------------------------------------------------------------------- 1 | # Polynomial Interpolation and Regression 2 | 3 | In this section we switch tracks and begin to consider numerical linear algebra related to rectangular matrices and least squares systems, which we motivate with an application to polynomial regression. 4 | _Polynomial interpolation_ is the process of finding a polynomial that equals data at a precise set of points. 5 | A more robust scheme is _polynomial regression_ where we use more data than the degrees of freedom in the polynomial. We therefore determine the polynomial using _least squares_: find 6 | the polynomial whose samples at the points are as close as possible to the data, as measured in the $2$-norm. This least squares problem is done numerically which will be discussed in the next few sections. 7 | 8 | 9 | ## Polynomial interpolation 10 | 11 | Our prelimary goal is given a set of points $x_j$ and data $f_j$, usually samples of a function 12 | $f_j = f(x_j)$, find a polynomial that interpolates the data at the points: 13 | 14 | **Definition (interpolatory polynomial)** Given _distinct_ points $𝐱 = \vectt[x_1,…,x_n] ∈ 𝔽^n$ 15 | and _data_ $𝐟 = \vectt[f_1,…,f_n] ∈ 𝔽^n$, a degree $n-1$ 16 | _interpolatory polynomial_ $p(x)$ satisfies 17 | $$ 18 | p(x_j) = f_j 19 | $$ 20 | ∎ 21 | 22 | The easiest way to solve this problem is to invert the Vandermonde system: 23 | 24 | **Definition (Vandermonde)** The _Vandermonde matrix_ associated with $𝐱 ∈ 𝔽^m$ 25 | is the matrix 26 | $$ 27 | V_{𝐱,n} := \begin{bmatrix} 1 & x_1 & ⋯ & x_1^{n-1} \\ 28 | ⋮ & ⋮ & ⋱ & ⋮ \\ 29 | 1 & x_m & ⋯ & x_m^{n-1} 30 | \end{bmatrix} ∈ 𝔽^{m × n}. 31 | $$ 32 | When it is clear from context we omit the subscripts $𝐱,n$. 33 | ∎ 34 | 35 | Writing the coefficients of a polynomial 36 | $$ 37 | p(x) = ∑_{k=0}^{n-1} c_k x^k 38 | $$ 39 | as a vector 40 | $𝐜 = \vectt[c_0,…,c_{n-1}] ∈ 𝔽^n$, we note that $V$ encodes the linear map from coefficients to values at a grid, that is, 41 | $$ 42 | V𝐜 = \Vectt[c_0 + c_1 x_1 + ⋯ + c_{n-1} x_1^{n-1}, ⋮, c_0 + c_1 x_m + ⋯ + c_{n-1} x_m^{n-1}] = \Vectt[p(x_1),⋮,p(x_m)]. 43 | $$ 44 | In the square case (where $m=n$), the coefficients of an interpolatory polynomial are given by $𝐜 = V^{-1} 𝐟$, so that 45 | $$ 46 | \Vectt[p(x_1),⋮,p(x_n)] = V 𝐜 = V V^{-1} 𝐟 = \Vectt[f_1,⋮,f_n]. 47 | $$ 48 | This inversion is justified by the following: 49 | 50 | **Proposition (interpolatory polynomial uniqueness)** 51 | Interpolatory polynomials are unique and therefore square Vandermonde matrices are invertible. 52 | 53 | **Proof** 54 | Suppose $p$ and $\pt$ are both interpolatory polynomials of the same function. Then $p(x) - \pt(x)$ vanishes at $n$ distinct points $x_j$. By the fundamental theorem of 55 | algebra it must be zero, i.e., $p = \pt$. 56 | 57 | For the second part, if $V 𝐜 = 0$ for $𝐜 = \vectt[c_0,…,c_{n-1}] ∈ 𝔽^n$ then for $q(x) = c_0 + ⋯ + c_{n-1} x^{n-1}$ we have 58 | $$ 59 | q(x_j) = 𝐞_j^⊤ V 𝐜 = 0 60 | $$ 61 | hence $q$ vanishes at $n$ distinct points and is therefore 0, i.e., $𝐜 = 0$. 62 | 63 | ∎ 64 | 65 | 66 | We can invert square Vandermonde matrix numerically in $O(n^3)$ operations using the PLU factorisation. 67 | But it turns out we can also construct the interpolatory polynomial directly, and evaluate the polynomial in only $O(n^2)$ operations. 68 | We will use the following polynomials which equal $1$ at one grid point and zero at the others: 69 | 70 | **Definition (Lagrange basis polynomial)** The _Lagrange basis polynomial_ is defined as 71 | $$ 72 | ℓ_k(x) := ∏_{j ≠ k} {x-x_j \over x_k - x_j} = {(x-x_1) ⋯(x-x_{k-1})(x-x_{k+1}) ⋯ (x-x_n) \over (x_k - x_1) ⋯ (x_k - x_{k-1}) (x_k - x_{k+1}) ⋯ (x_k - x_n)} 73 | $$ 74 | ∎ 75 | 76 | Plugging in the grid points verifies that: $ℓ_k(x_j) = δ_{kj}$. 77 | 78 | We can use these to construct the interpolatory polynomial: 79 | 80 | **Theorem (Lagrange interpolation)** 81 | The unique interpolation polynomial is: 82 | $$ 83 | p(x) = f_1 ℓ_1(x) + ⋯ + f_n ℓ_n(x) 84 | $$ 85 | 86 | **Proof** 87 | Note that 88 | $$ 89 | p(x_j) = ∑_{k=1}^n f_k ℓ_k(x_j) = f_j. 90 | $$ 91 | ∎ 92 | 93 | **Example (interpolating an exponential)** We can interpolate $\exp(x)$ at the points $0,1,2$. 94 | That is, our data is $𝐟 = \vectt[1, {\rm e},{\rm e}^2]$ and the interpolatory polynomial is 95 | $$ 96 | \begin{align*} 97 | p(x) &= ℓ_1(x) + {\rm e} ℓ_2(x) + {\rm e}^2 ℓ_3(x) = 98 | {(x - 1) (x-2) \over (-1)(-2)} + {\rm e} {x (x-2) \over (-1)} + 99 | {\rm e}^2 {x (x-1) \over 2} \\ 100 | &= (1/2 - {\rm e} +{\rm e}^2/2)x^2 + (-3/2 + 2 {\rm e} - {\rm e}^2 /2) x + 1 101 | \end{align*} 102 | $$ 103 | ∎ 104 | 105 | 106 | **Remark** Interpolating at evenly spaced points is a really _bad_ idea: 107 | interpolation is inherently ill-conditioned. 108 | The labs will explore this issue experimentally. Another 109 | serious issue is that monomials are a horrible basis for interpolation. This is intuitive: when $n$ is large $x^n$ is basically zero near the origin and hence $x_j^n$ numerically lose linear independence, that is, on a computer they appear to be linearly dependent (up to rounding errors). We will discuss alternative bases in Part IV. 110 | 111 | ## Polynomial regression 112 | 113 | 114 | 115 | In many settings interpolation is not an accurate or appropriate tool. Data is often on an evenly spaced grid in which case (as seen in the labs) 116 | interpolation breaks down catastrophically. Or the data is noisy and one ends up over resolving: approximating the 117 | noise rather than the signal. A simple solution is _polynomial regression_ use more sample points than than the degrees of freedom in the polynomial. The special case of an affine polynomial is called _linear regression_. 118 | 119 | More precisely, for $𝐱 ∈ 𝔽^m$ and for $n < m$ we want to find a degree $n-1$ polynomial 120 | $$ 121 | p(x) = ∑_{k=0}^{n-1} c_k x^k 122 | $$ 123 | such that 124 | $$ 125 | \Vectt[p(x_1), ⋮, p(x_m)] ≈ \underbrace{\Vectt[f_1,⋮,f_m]}_{𝐟}. 126 | $$ 127 | Mapping between coefficients $𝐜 ∈ 𝔽^n$ to polynomial values on a grid can be accomplished 128 | via rectangular Vandermonde matrices. In particular, our goal is to choose $𝐜 ∈ 𝔽^n$ so that 129 | $$ 130 | V 𝐜 = \Vectt[p(x_1), ⋮, p(x_m)] ≈ 𝐟. 131 | $$ 132 | We do so by solving the _least squares_ system: given $V ∈ 𝔽^{m × n}$ and $𝐟 ∈ 𝔽^m$ we want to find $𝐜 ∈ 𝔽^n$ such that 133 | $$ 134 | \| V 𝐜 - 𝐟 \| 135 | $$ 136 | is minimal. Note interpolation is a special case where this norm is precisely zero (which is indeed minimal), but in general this norm may be rather large. We will discuss the numerical solution 137 | of least squares problems in the next few sections. 138 | 139 | 140 | **Remark** Using regression instead of interpolation can overcome the issues with evenly spaced grids. However, the monomial basis is still very problematic. -------------------------------------------------------------------------------- /src/notes/IV.2.DFT.jmd: -------------------------------------------------------------------------------- 1 | # Discrete Fourier Transform 2 | 3 | 4 | In the previous section we explored using the trapezium rule for approximating Fourier coefficients. 5 | This is a linear map from function values to coefficients and thus can be reinterpreted as 6 | a matrix-vector product, called the the Discrete Fourier Transform. It turns out the matrix 7 | is unitary which leads to important properties including interpolation. 8 | 9 | 10 | **Remark** A clever way of decomposing the DFT leads to a fast way of applying and inverting it, which is one of the 11 | most influencial algorithms of the 20th century: the Fast Fourier Transform. But this is beyond the scope of this module. 12 | 13 | 14 | ## The Discrete Fourier transform 15 | 16 | **Definition (DFT)** 17 | The _Discrete Fourier Transform (DFT)_ is defined as: 18 | $$ 19 | \begin{align*} 20 | Q_n &:= {1 \over \sqrt{n}} \begin{bmatrix} 1 & 1 & 1& ⋯ & 1 \\ 21 | 1 & {\rm e}^{-\I θ_1} & {\rm e}^{-\I θ_2} & ⋯ & {\rm e}^{-\I θ_{n-1}} \\ 22 | 1 & {\rm e}^{-\I 2 θ_1} & {\rm e}^{-\I 2 θ_2} & ⋯ & {\rm e}^{-\I 2θ_{n-1}} \\ 23 | ⋮ & ⋮ & ⋮ & ⋱ & ⋮ \\ 24 | 1 & {\rm e}^{-\I (n-1) θ_1} & {\rm e}^{-\I (n-1) θ_2} & ⋯ & {\rm e}^{-\I (n-1) θ_{n-1}} 25 | \end{bmatrix} \\ 26 | &= {1 \over \sqrt{n}} \begin{bmatrix} 1 & 1 & 1& ⋯ & 1 \\ 27 | 1 & ω^{-1} & ω^{-2} & ⋯ & ω^{-(n-1)}\\ 28 | 1 & ω^{-2} & ω^{-4} & ⋯ & ω^{-2(n-1)}\\ 29 | ⋮ & ⋮ & ⋮ & ⋱ & ⋮ \\ 30 | 1 & ω^{-(n-1)} & ω^{-2(n-1)} & ⋯ & ω^{-(n-1)^2} 31 | \end{bmatrix} 32 | \end{align*} 33 | $$ 34 | for the $n$-th root of unity $ω = {\rm e}^{2π\I/n}$. 35 | ∎ 36 | 37 | Note that 38 | $$ 39 | \begin{align*} 40 | Q_n^⋆ &= {1 \over \sqrt{n}} \begin{bmatrix} 41 | 1 & 1 & 1& ⋯ & 1 \\ 42 | 1 & {\rm e}^{\I θ_1} & {\rm e}^{\I 2 θ_1} & ⋯ & {\rm e}^{\I (n-1) θ_1} \\ 43 | 1 & {\rm e}^{\I θ_2} & {\rm e}^{\I 2 θ_2} & ⋯ & {\rm e}^{\I (n-1)θ_2} \\ 44 | ⋮ & ⋮ & ⋮ & ⋱ & ⋮ \\ 45 | 1 & {\rm e}^{\I θ_{n-1}} & {\rm e}^{\I 2 θ_{n-1}} & ⋯ & {\rm e}^{\I (n-1) θ_{n-1}} 46 | \end{bmatrix} \\ 47 | &= {1 \over \sqrt{n}} \begin{bmatrix} 48 | 1 & 1 & 1& ⋯ & 1 \\ 49 | 1 & ω^{1} & ω^{2} & ⋯ & ω^{(n-1)}\\ 50 | 1 & ω^{2} & ω^{4} & ⋯ & ω^{2(n-1)}\\ 51 | ⋮ & ⋮ & ⋮ & ⋱ & ⋮ \\ 52 | 1 & ω^{(n-1)} & ω^{2(n-1)} & ⋯ & ω^{(n-1)^2} 53 | \end{bmatrix} 54 | \end{align*} 55 | $$ 56 | Hence we have 57 | $$ 58 | \underbrace{\begin{bmatrix} \hat f_0^n \\ ⋮ \\ \hat f_{n-1}^n \end{bmatrix}}_{\vchatf^n} = 59 | {1 \over \sqrt{n}} Q_n \underbrace{\begin{bmatrix} f(θ_0) \\ ⋮ \\ f(θ_{n-1}) \end{bmatrix}}_{𝐟^n} 60 | $$ 61 | 62 | The choice of normalisation constant is motivated by the following: 63 | 64 | **Proposition 1 (DFT is Unitary)** $Q_n ∈ U(n)$, that is, $Q_n^⋆ Q_n = Q_n Q_n^⋆ = I$. 65 | 66 | **Proof** 67 | $$ 68 | Q_n Q_n^⋆ = \begin{bmatrix} Σ_n[1] & Σ_n[{\rm e}^{\I θ}] & ⋯ & Σ_n[{\rm e}^{\I (n-1) θ}] \\ 69 | Σ_n[{\rm e}^{-\I θ}] & Σ_n[1] & ⋯ & Σ_n[{\rm e}^{\I (n-2) θ}] \\ 70 | ⋮ & ⋮ & ⋱ & ⋮ \\ 71 | Σ_n[{\rm e}^{-\I(n-1) θ}] & Σ_n[{\rm e}^{-\I(n-2) θ}] & ⋯ & Σ_n[1] 72 | \end{bmatrix} = I 73 | $$ 74 | ∎ 75 | 76 | In other words, $Q_n$ is easily inverted and we also have a map from discrete Fourier coefficients back to values: 77 | $$ 78 | \sqrt{n} Q_n^⋆ \vchatf^n = 𝐟^n 79 | $$ 80 | 81 | **Example (Computing Sum)** Define the following infinite sum (which has no name apparently, 82 | according to Mathematica): 83 | $$ 84 | S_n(k) := ∑_{p=0}^∞ {1 \over (k+pn)!} 85 | $$ 86 | We can use the DFT to compute $S_n(0), …, S_n(n-1)$. 87 | Consider 88 | $$ 89 | f(θ) = \exp({\rm e}^{\I θ}) = ∑_{k=0}^∞ {{\rm e}^{\I k θ} \over k!} 90 | $$ 91 | where we know the Fourier coefficients from the Taylor series of ${\rm e}^z$. 92 | The discrete Fourier coefficients satisfy for $0 ≤ k ≤ n-1$: 93 | $$ 94 | \hat f_k^n = \hat f_k + \hat f_{k+n} + \hat f_{k+2n} + ⋯ = S_n(k) 95 | $$ 96 | Thus we have 97 | $$ 98 | \begin{bmatrix} 99 | S_n(0) \\ 100 | ⋮ \\ 101 | S_n(n-1) 102 | \end{bmatrix} = {1 \over \sqrt{n}} Q_n \begin{bmatrix} {\rm e} \\ 103 | \exp({\rm e}^{2\I π/n}) \\ 104 | ⋮ \\ 105 | \exp({\rm e}^{2\I (n-1) π/n}) \end{bmatrix} 106 | $$ 107 | ∎ 108 | 109 | 110 | 111 | ## Interpolation 112 | 113 | We investigated interpolation and least squares using polynomials at evenly spaced points, observing that 114 | there were issues with stability. We now show that the DFT actually gives coefficients that interpolate using Fourier 115 | expansions. As the DFT is a unitary matrix multiplication is “stable", i.e. it preserves norms and hence we know 116 | it cannot cause the same huge blow-up we saw for polynomials. That is: whilst polynomials are bad for interpolation at 117 | evenly spaced points, trigonometric polynomials are great. 118 | 119 | The following guarantees that our approximate Fourier series actually interpolates the data: 120 | 121 | **Corollary (Interpolation)** 122 | $$ 123 | f_n(θ) := ∑_{k=0}^{n-1} \hat f_k^n {\rm e}^{\I k θ} 124 | $$ 125 | interpolates $f$ at $θ_j$: 126 | $$ 127 | f_n(θ_j) = f(θ_j) 128 | $$ 129 | 130 | **Proof** 131 | We have 132 | $$ 133 | f_n(θ_j) = ∑_{k=0}^{n-1} \hat f_k^n {\rm e}^{\I k θ_j} = \sqrt n 𝐞_{j+1}^⊤ Q_n^⋆ \vchatf^n = 𝐞_{j+1}^⊤ Q_n^⋆ Q_n 𝐟^n = f(θ_j). 134 | $$ 135 | 136 | ∎ 137 | 138 | 139 | 140 | **Example (DFT versus Lagrange)** Consider interpolating $\exp z$ by a polynomial at the points $1, \I, -1, -\I$. 141 | We can use Lagrange polynomials: 142 | $$ 143 | \meeq{ 144 | ℓ_1(z) ={ (z - \I)(z + 1)(z + \I) \over 2(1 - \I)(1 + \I)} = { z^3 + z^2 + z + 1 \over 4} \ccr 145 | ℓ_2(z) ={ (z - 1)(z + 1)(z + \I) \over (\I - 1) (\I + 1) 2\I} = { \I z^3 - z^2 - \I z + 1 \over 4} \ccr 146 | ℓ_3(z) ={ (z - 1)(z - \I)(z + \I) \over -2 (-1-\I)(-1+\I)} = {-z^3 + z^2 - z + 1 \over 4} \ccr 147 | ℓ_4(z) ={ (z - 1)(z - \I)(z+1) \over (-\I-1)(-2\I)(-\I+1)} = {- \I z^3 -z^2 + \I z + 1 \over 4} 148 | } 149 | $$ 150 | So we get the interpolant: 151 | $$ 152 | \begin{align*} 153 | \E & ℓ_1(z) + \E^\I ℓ_2(z) + \E^{-1} ℓ_3(z) + \E^{-\I} ℓ_4(z) \\ 154 | &= 155 | {\E + \E^\I + \E^{-1} + \E^{-\I} \over 4} + 156 | {\E - \I \E^\I - \E^{-1} + \I \E^{-\I} \over 4} z + 157 | {\E - \E^\I + \E^{-1} - \I \E^{-\I} \over 4} z^2 + 158 | {\E + \I \E^\I - \E^{-1} - \I \E^{-\I} \over 4} z^3 159 | \end{align*} 160 | $$ 161 | 162 | Alternatively we could have deduced this directly from the DFT. In particular, we know the coefficients of the interpolating polynomial must be, 163 | for $ω = \I$ and $f(θ) = \exp(\E^{\I θ})$, 164 | $$ 165 | \Vectt[\hat f_0^4, \hat f_1^4, \hat f_2^4, \hat f_3^4] = 166 | {1 \over 4} \begin{bmatrix}1 & 1 & 1 & 1 \\ 167 | 1 & -\I & -1 & \I \\ 168 | 1 & -1 & 1 & -1 \\ 169 | 1 & \I & -1 & -\I 170 | \end{bmatrix} 171 | \Vectt[\E, \E^\I, \E^{-1}, \E^{-\I}] = {1 \over 4} \Vectt[\E + \E^\I + \E^{-1} + \E^{-\I} , 172 | \E -\I \E^\I - \E^{-1} + \I \E^{-\I} , 173 | \E - \E^\I + \E^{-1} - \E^{-\I} , 174 | \E + \I \E^\I - \E^{-1} - \I \E^{-\I} 175 | ] = {1 \over 2} \Vectt[\cosh 1 + \cos 1 , 176 | \sinh 1 + \sin 1 , 177 | \cosh 1 - \cos 1 , 178 | \sinh 1 - \sin 1 179 | ]. 180 | $$ 181 | ∎ 182 | 183 | The interpolation property also applies to the approximation 184 | $$ 185 | f_n(θ) = ∑_{k=-⌈n/2⌉}^{⌊n/2⌋} \hat f_k {\rm e}^{{\rm i} k θ} 186 | $$ 187 | for general Fourier series, which is investigated in the problem sheet. -------------------------------------------------------------------------------- /src/notes/template.tpl: -------------------------------------------------------------------------------- 1 | 2 | {{{ :body }}} 3 | 4 | -------------------------------------------------------------------------------- /src/notes/unused.jmd: -------------------------------------------------------------------------------- 1 | ## Rectangular rule floating point error bound 2 | 3 | We can use bounds on floating point arithmetic to derive bounds on the error in a 4 | more complicated computation. For example, consider our rectangular rule approximation 5 | $$ 6 | \int_0^1 f(x) {\rm d} x ≈ {1 \over n} \sum_{j=1}^n f(j/n) 7 | $$ 8 | Assume for simplicity that $n = 2^s$, i.e., a power of 2, so that in idealised floating 9 | point we have $x ⊘ n = x / n$ for any $x ∈ F_{∞,S}$. Thus our actual approximation is 10 | $$ 11 | \int_0^1 f(x) {\rm d} x ≈ {1 \over n} ⨁_{j=1}^n f^{\rm FP}(j/n) 12 | $$ 13 | where 14 | $$ 15 | f^{\rm FP}(x) = f(x) + δ_x^f 16 | $$ 17 | represents the function $f$ implemented in idealised floating point, that is $f^{\rm FP} : F_{S,∞} → F_{S,∞}$. 18 | We can generally assume we have a bound of the form $|δ_x^f| ≤ c ϵ_{\rm m}$ for some $c ∈ ℝ$ and all $x ∈ [0,1]$. 19 | 20 | We will use the following proposition: 21 | 22 | **Proposition (floating point addition error)** If $x_1,…,x_n ∈ F_{∞,S}$ then 23 | $$ 24 | x_1 ⊕ ⋯ ⊕ x_n = x_1 + ⋯ + x_n + σ_n 25 | $$ 26 | where, for $M = Σ_{k=1}^n |x_k|$, $|σ_n| ≤ M E_{n-1,ϵ_{\rm m}/2}$ assuming $n ϵ_{\rm m} < 2$ and where 27 | $$ 28 | E_{n,ϵ} := {n ϵ \over 1-nϵ}. 29 | $$ 30 | **Proof** 31 | See problem sheet 4. 32 | ∎ 33 | 34 | We can now relate the error in the rectangular rule to that of the 35 | 36 | **Theorem (rectangular rule error)** Assume $f$ is differentiable and approximated 37 | in idealised floating point arithmetic by $f^{\rm FP} : F_{∞,S} → F_{∞,S}$ satisfying 38 | $$ 39 | f^{\rm FP}(x) = f(x) + δ_x^f 40 | $$ 41 | where $|δ_x^f| ≤ c ϵ_{\rm m}$, and that $n = 2^s$. Then 42 | $$ 43 | \int_0^1 f(x) {\rm d} x = {1 \over n} ⨁_{j=1}^n f^{\rm FP}(j/n) + δ^{\rm FP} 44 | $$ 45 | where, for $M = \sup_{0 ≤ x ≤ 1}|f'(x)|$, 46 | $$ 47 | |δ^{\rm FP}| ≤ {M \over n} + c ϵ_{\rm m} n + E_{n-1,ϵ_{\rm m}/2} \sup |f| 48 | $$ 49 | 50 | **Proof** 51 | 52 | From the preceding proposition we have 53 | $$ 54 | {1 \over n} ⨁_{j=1}^n f^{\rm FP}(j/n) = {1 \over n}∑_{j=1}^n f^{\rm FP}(j/n) + {σ_n \over n} 55 | = {1 \over n}∑_{j=1}^n f(j/n) + ∑_{j=1}^n δ_{j/n}^{\rm f} + {σ_n \over n} 56 | = ∫_0^1 f(x) {\rm d}x + \underbrace{δ + ∑_{j=1}^n δ_{j/n}^{\rm f} + {σ_n \over n}}_{-δ^{\rm FP}} 57 | $$ 58 | where $δ$ is the error of the rectangular rule deduced in I.1 satisfying $|δ| ≤ M/n$. 59 | We then bound 60 | $$ 61 | |δ^{\rm FP}| ≤ |δ| + ∑_{j=1}^n |δ_{j/n}^{\rm f}| + {|σ_n| \over n} 62 | ≤ {M \over n} + c ϵ_{\rm m} n + E_{n-1,ϵ_{\rm m}/2} \sup |f| 63 | $$ 64 | ∎ 65 | 66 | 67 | 68 | 69 | 70 | **Problem 4(a)** Suppose $|ϵ_k| ≤ ϵ$ and $n ϵ < 1$. Show that 71 | $$ 72 | ∏_{k=1}^n (1+ϵ_k) = 1+θ_n 73 | $$ 74 | for some constant $θ_n$ satisfying 75 | $$ 76 | |θ_n| ≤ \underbrace{n ϵ \over 1-nϵ}_{E_{n,ϵ}} 77 | $$ 78 | Hint: use induction. 79 | 80 | **SOLUTION** 81 | 82 | $$ 83 | ∏_{k=1}^{n+1} (1+ϵ_k) = ∏_{k=1}^n (1+ϵ_k) (1+ϵ_{n+1}) = (1+θ_n)(1+ϵ_{n+1}) = 1 + \underbrace{θ_n + ϵ_{n+1} + θ_nϵ_{n+1}}_{θ_{n+1}} 84 | $$ 85 | where 86 | $$ 87 | \begin{align*} 88 | |θ_{n+1}| &≤ {n ϵ \over 1-nϵ}(1+ϵ) + ϵ \\ 89 | &={n ϵ + n ϵ^2 \over 1-(n+1)ϵ} {1-(n+1)ϵ \over 1-nϵ} + {ϵ-(n+1)ϵ^2 \over 1-(n+1)ϵ} \\ 90 | &≤ {(n+1) - n ϵ \over 1-(n+1)ϵ} ϵ ≤ {(n+1) ϵ \over 1-(n+1)ϵ} 91 | \end{align*} 92 | $$ 93 | 94 | **END** 95 | 96 | **Problem 4(b)** Show if $x_1,…,x_n ∈ F$ then 97 | $$ 98 | x_1 ⊗ ⋯ ⊗ x_n = x_1 ⋯ x_n (1 + θ_{n-1}) 99 | $$ 100 | where $|θ_n| ≤ E_{n,ϵ_{\rm m}/2}$, assuming $n ϵ_{\rm m} < 2$. 101 | You may assume all operations are within the normalised range. 102 | 103 | **SOLUTION** 104 | 105 | We can expand out: 106 | $$ 107 | x_1 ⊗ ⋯ ⊗ x_n = (⋯((x_1 x_2)(1+δ_1) x_3(1+δ_2)⋯ x_n(1+δ_{n-1})) = x_1 ⋯ x_n (1+δ_1) ⋯ (1+δ_{n-1}) 108 | $$ 109 | where $|δ_k| ≤ ϵ_{\rm m}/2$. The result then follows from the previous result. 110 | 111 | **END** 112 | 113 | **Problem 4(c)** Show if $x_1,…,x_n ∈ F_{∞,S}$ then 114 | $$ 115 | x_1 ⊕ ⋯ ⊕ x_n = x_1 + ⋯ + x_n + σ_n 116 | $$ 117 | where, for $M = Σ_{k=1}^n |x_k|$, $|σ_n| ≤ M E_{n-1,ϵ_{\rm m}/2},$ assuming $n ϵ_{\rm m} < 2$. 118 | Hint: use part (a) to first write 119 | $$ 120 | x_1 ⊕ ⋯ ⊕ x_n = x_1(1+θ_{n-1}) + ∑_{j=2}^n x_j (1 + θ_{n-j+1}). 121 | $$ 122 | 123 | 124 | **SOLUTION** 125 | 126 | Using part (a) we write: 127 | $$ 128 | (⋯((x_1 + x_2)(1+δ_1) + x_3)(1+δ_2)⋯+x_n)(1+δ_{n-1}) 129 | = x_1 ∏_{k=1}^{n-1} (1+δ_k) + ∑_{j=2}^n x_j ∏_{k=j-1}^{n-1} (1+δ_j) 130 | = x_1(1+θ_{n-1}) + ∑_{j=2}^n x_j (1 + θ_{n-j+1}) 131 | $$ 132 | where we have for $j = 2,…,n$ 133 | $$ 134 | |θ_{n-j+1}| ≤ E_{n-j+1,ϵ_{\rm m}/2} ≤ E_{n-1,ϵ_{\rm m}/2}. 135 | $$ 136 | Thus we have 137 | $$ 138 | ∑_{j=1}^n x_j (1 + θ_{n-j+1})= ∑_{j=1}^n x_j + \underbrace{∑_{j=1}^n x_j θ_{n-j+1}}_{σ_n} 139 | $$ 140 | where $|σ_n| ≤ M E_{n-1,ϵ_{\rm m}/2}$. 141 | ∎ 142 | 143 | 144 | **END** -------------------------------------------------------------------------------- /src/sheets/sheet1s.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 1** 2 | 3 | 4 | 5 | ------ 6 | 7 | **Problem 1** Assuming $f$ is differentiable, prove the left-point Rectangular rule error formula 8 | $$ 9 | ∫_a^b f(x) {\rm d}x = h ∑_{j=0}^{n-1} f(x_j) + δ 10 | $$ 11 | where $|δ| ≤ M (b-a) h$ for $M = \sup_{a ≤ x ≤ b}|f'(x)|$, $h = (b-a)/n$ and $x_j = a + jh$. 12 | 13 | **SOLUTION** 14 | 15 | This proof is very similar to the right-point rule, the only 16 | difference is we use a different constant in the indefinite integration in 17 | the integration-by-parts. First we need to adapt **Lemma 1 (Rect. rule error on one panel)**: 18 | $$ 19 | \meeq{ 20 | ∫_a^b f(x) {\rm d}x = ∫_a^b (x-b)' f(x) {\rm d}x = [(x-b) f(x)]_a^b - ∫_a^b (x-b) f'(x) {\rm d} x \ccr 21 | = (b-a) f(a) + \underbrace{\left(-∫_a^b (x-b) f'(x) {\rm d} x \right)}_ε. 22 | } 23 | $$ 24 | where 25 | $$ 26 | \abs{ε} ≤ (b-a) \sup_{a ≤ x ≤ b}|(x-b) f'(x)| ≤ M (b-a)^2 27 | $$ 28 | Applying this result on $[x_{j-1},x_j]$ we get 29 | $$ 30 | ∫_{x_{j-1}}^{x_j} f(x) {\rm d}x = h f(x_{j-1}) + δ_j 31 | $$ 32 | where $|δ_j| ≤ M h^2$. 33 | Splitting the integral into a sum of smaller integrals: 34 | $$ 35 | ∫_a^b f(x) {\rm d}x = ∑_{j=1}^n ∫_{x_{j-1}}^{x_j} f(x) {\rm d}x = 36 | h ∑_{j=1}^n f(x_{j-1}) + \underbrace{∑_{j=1}^n δ_j}_δ 37 | $$ 38 | where using the triangular inequality we have 39 | $$ 40 | |δ| = \abs{ ∑_{j=1}^n δ_j} ≤ ∑_{j=1}^n |δ_j| ≤ M n h^2 = M(b-a)h. 41 | $$ 42 | 43 | **END** 44 | 45 | 46 | 47 | 48 | 49 | **Problem 2(a)** Assuming $f$ is twice-differentiable, prove a one-panel Trapezium rule error bound: 50 | $$ 51 | ∫_a^b f(x) {\rm d}x = (b-a) {f(a) + f(b) \over 2} + δ 52 | $$ 53 | where $|δ| ≤ M (b-a)^3$ for $M = \sup_{a ≤ x ≤ b}|f''(x)|$. 54 | 55 | _Hint_: Recall from the notes 56 | $$ 57 | ∫_a^b {(b-x) f(a) + (x-a) f(b) \over b-a} \dx = (b-a) {f(a) + f(b) \over 2} 58 | $$ 59 | and you may need to use Taylor's theorem. Note that the bound is not sharp and so you may arrive at something sharper like $|δ| ≤ 3(b-a)^3 M/4$. 60 | The sharpest bound is $|δ| ≤ (b-a)^3 M/12$ but that would be a significantly harder challenge to show! 61 | 62 | **SOLUTION** 63 | 64 | 65 | Recall from the notes: 66 | $$ 67 | ∫_a^b {(b-x) f(a) + (x-a) f(b) \over b-a} \dx = (b-a) {f(a) + f(b) \over 2} 68 | $$ 69 | Thus we can find by integration by parts twice (noting that the integrand vanishes at $a$ and $b$): 70 | $$ 71 | \meeq{ 72 | δ = ∫_a^b \br[f(x) - {(b-x) f(a) + (x-a) f(b) \over b-a}] {\rm d}x \ccr 73 | = -∫_a^b (x-b) \br[f'(x) - {f(b)-f(a) \over b-a}] {\rm d}x \ccr 74 | = {(b-a)^2 \over 2} \br[f'(a) - {f(b)-f(a) \over b-a}] + ∫_a^b {(x-b)^2 \over 2} f''(x) {\rm d}x 75 | } 76 | $$ 77 | Applying **Proposition 1** we know 78 | $$ 79 | \abs{f'(a) - {f(b)-f(a) \over b-a}} ≤ M (b-a)/2 80 | $$ 81 | Further we have 82 | $$ 83 | \abs{∫_a^b {(x-b)^2 \over 2} f''(x) {\rm d}x } ≤ {(b-a)^3 \over 2} M 84 | $$ 85 | Thus we have the bound 86 | $$ 87 | |δ| ≤ {(b-a)^2 \over 2} M (b-a)/2 + {(b-a)^3 \over 2} M ≤ {3 (b-a)^3 \over 4} M ≤ (b-a)^3 M. 88 | $$ 89 | For the sharper $1/12$ constant check out the [Euler–Maclaurin formula](https://en.wikipedia.org/wiki/Euler–Maclaurin_formula). 90 | 91 | **END** 92 | 93 | 94 | **Problem 2(b)** Assuming $f$ is twice-differentiable, prove a bound for the Trapezium rule error: 95 | $$ 96 | ∫_a^b f(x) {\rm d}x = h \br[{f(a) \over 2} + ∑_{j=1}^{n-1} f(x_j) + {f(b) \over 2}] + δ 97 | $$ 98 | where $|δ| ≤ M (b-a) h^2$ for $M = \sup_{a ≤ x ≤ b}|f''(x)|$. 99 | 100 | **SOLUTION** 101 | 102 | This is very similar to the rectangular rules: applying the preceding result on $[x_{j-1},x_j]$ we get 103 | $$ 104 | ∫_{x_{j-1}}^{x_j} f(x) {\rm d}x = h {f(x_{j-1}) + f(x_j) \over 2} + δ_j 105 | $$ 106 | where $|δ_j| ≤ M h^3$. 107 | Splitting the integral into a sum of smaller integrals: 108 | $$ 109 | ∫_a^b f(x) {\rm d}x = ∑_{j=1}^n ∫_{x_{j-1}}^{x_j} f(x) {\rm d}x = 110 | h \br[{f(a) \over 2} + ∑_{j=1}^{n-1} f(x_j) + {f(b) \over 2}] + \underbrace{∑_{j=1}^n δ_j}_δ 111 | $$ 112 | where using the triangular inequality we have 113 | $$ 114 | |δ| = \abs{ ∑_{j=1}^n δ_j} ≤ ∑_{j=1}^n |δ_j| ≤ M n h^3 = M(b-a) h^2. 115 | $$ 116 | 117 | 118 | 119 | **END** 120 | 121 | 122 | ----- 123 | 124 | **Problem 3** Assuming $f$ is twice-differentiable, 125 | for the left difference approximation 126 | $$ 127 | f'(x) = {f(x) - f(x - h) \over h} + δ, 128 | $$ 129 | show that $|δ| ≤ Mh/2$ for $M = \sup_{x-h ≤ t ≤ x}\abs{f''(t)}$. 130 | 131 | 132 | **SOLUTION** 133 | 134 | Almost identical to the right-difference. Use Taylor series to write: 135 | $$ 136 | f(x-h) = f(x) + f'(x) (-h) + {f''(t) \over 2} h^2 137 | $$ 138 | where $t ∈ [x-h,x]$, so that 139 | $$ 140 | f'(x) = {f(x) - f(x-h) \over h} + \underbrace{f''(t)/2 h}_δ 141 | $$ 142 | The bound follows immediately: 143 | $$ 144 | |δ| ≤ |f''(t)/2 h| ≤ Mh/2. 145 | $$ 146 | 147 | **END** 148 | 149 | 150 | 151 | **Problem 4** Assuming $f$ is thrice-differentiable, 152 | for the central differences approximation 153 | $$ 154 | f'(x) = {f(x + h) - f(x - h) \over 2h} + δ, 155 | $$ 156 | show that $|δ| ≤ Mh^2/6$ for $M = \sup_{x-h ≤ t ≤ x+h}\abs{f'''(t)}$. 157 | 158 | **SOLUTION** 159 | 160 | 161 | By Taylor's theorem, the approximation around $x+h$ is 162 | $$ 163 | f(x+h) = f(x) + f'(x)h + \frac{f''(x)}{2}h^2 + \frac{f'''(t_1)}{6}h^3, 164 | $$ 165 | for some $t_1 ∈ (x, x+h)$ and similarly 166 | $$f(x-h) = f(x) + f'(x)(-h) + \frac{f''(x)}{2}h^2 - \frac{f'''(t_2)}{6}h^3,$$ 167 | for some $t_2 ∈ (x-h, x)$. 168 | 169 | Subtracting the second expression from the first we obtain 170 | $$f(x+h)-f(x-h) = f'(x)(2h) + \frac{f'''(t_1)+f'''(t_2)}{6}h^3.$$ 171 | Hence, 172 | 173 | $$ 174 | \frac{f(x+h)-f(x-h)}{2h} = f'(x) + \underbrace{\frac{f'''(t_1)+f'''(t_2)}{12}h^2}_{δ}. 175 | $$ 176 | 177 | Thus, the error can be bounded by 178 | $$\left|δ\right| ≤ {M \over 6} h^2.$$ 179 | 180 | **END** 181 | 182 | 183 | 184 | 185 | **Problem 5** Assuming $f$ is thrice-differentiable, 186 | for the second-order derivative approximation 187 | $$ 188 | {f(x+h) - 2f(x) + f(x-h) \over h^2} = f''(x) + δ 189 | $$ 190 | show that $|δ| ≤ Mh/3$ for $M = \sup_{x-h ≤ t ≤ x+h}\abs{f'''(t)}$. 191 | 192 | **SOLUTION** 193 | Using the same two formulas as in the previous problem we have 194 | $$f(x+h) = f(x) + f'(x)h + \frac{f''(x)}{2}h^2 + \frac{f'''(t_1)}{6}h^3,$$ 195 | for some $t_1 ∈ (x, x+h)$ 196 | and 197 | $$f(x-h) = f(x) + f'(x)(-h) + \frac{f''(x)}{2}h^2 - \frac{f'''(t_2)}{6}h^3,$$ 198 | for some $t_2 ∈ (x-h, x)$. 199 | 200 | Summing the two we obtain 201 | $$f(x+h) + f(x-h) = 2f(x) + f''(x)h^2 + \frac{f'''(t_1)}{6}h^3 - \frac{f'''(t_2)}{6}h^3.$$ 202 | 203 | Thus, 204 | $$f''(x) = \frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + \frac{f'''(t_2) - f'''(t_1)}{6}h.$$ 205 | 206 | Hence, the error is 207 | $$ 208 | |δ| = \left|f''(x) - {f(x+h) - 2f(x) + f(x-h) \over h^2} \right| = \left|\frac{f'''(t_2) - f'''(t_1)}{6}h\right|≤ {Mh \over 3}. 209 | $$ 210 | 211 | 212 | **END** -------------------------------------------------------------------------------- /src/sheets/sheet2s.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 2** 2 | 3 | **Problem 1** Using dual number arithmetic, compute the following polynomials evaluated at the 4 | dual number $2+ϵ$ and use this to deduce their derivative at $2$: 5 | $$ 6 | 2x^2 + 3x + 4, (x+1)(x+2)(x+3), (2x+1)x^3 7 | $$ 8 | 9 | **SOLUTION** 10 | (a) 11 | $$ 12 | 2(2+ϵ)^2 + 3(2+ϵ) + 4 = 2(4+4ϵ) + 6+3ϵ + 4 = 18 + 11ϵ 13 | $$ 14 | so the derivative is 11. 15 | 16 | (b) 17 | $$ 18 | (3+ϵ)(4+ϵ)(5+ϵ) = (12+7ϵ)(5+ϵ) = 60+47ϵ 19 | $$ 20 | so the derivative is 47. 21 | 22 | (c) 23 | $$ 24 | (2(2+ϵ)+1)(2+ϵ)^3 = 25 | (5+2ϵ)(4+4ϵ)(2+ϵ) = 26 | (20 + 28ϵ)(2+ϵ) = 40 + 76ϵ 27 | $$ 28 | so the derivative is 76. 29 | 30 | **END** 31 | 32 | 33 | 34 | **Problem 2** What should the following functions applied to dual numbers return for $x = a+b ϵ$: 35 | $$ 36 | f(x) = x^{100} + 1, g(x) = 1/x, h(x) = \tan x 37 | $$ 38 | State the domain where these definitions are valid. 39 | 40 | **SOLUTION** 41 | 42 | $$ 43 | f(a+ bϵ) = f(a) + b f'(a) ϵ = a^{100} + 1 + 100ba^{99} ϵ 44 | $$ 45 | valid everywhere. 46 | 47 | $$ 48 | g(a+ bϵ) = {1 \over a} - {b \over a^2} ϵ 49 | $$ 50 | valid for $a ≠ 0$. 51 | 52 | $$ 53 | h(a+bϵ) = \tan a + b \sec^2 a ϵ 54 | $$ 55 | valid for $a ∉ \{ kπ+π/2 : k ∈ ℤ\}$. 56 | 57 | **END** 58 | 59 | 60 | **Problem 3(a)** What is the correct definition of division on dual numbers, i.e., 61 | $$ 62 | (a + b ϵ )/(c + d ϵ ) = s + t ϵ 63 | $$ 64 | for what choice of $s$ and $t$? 65 | 66 | **SOLUTION** 67 | 68 | As with complex numbers, division is easiest to understand by first multiplying with the conjugate, that is: 69 | $$ 70 | \frac{a+bϵ}{c+dϵ} = \frac{(a+bϵ)(c-dϵ)}{(c+dϵ)(c-dϵ)}. 71 | $$ 72 | Expanding the products and dropping terms with $ϵ^2$ then leaves us with the definition of division for dual numbers (where the denominator must have non-zero real part): 73 | $$ 74 | \frac{a}{c} + \frac{bc - ad}{c^2}ϵ. 75 | $$ 76 | Thus we have $s = \frac{a}{c}$ and $t = \frac{bc - ad}{c^2}$. 77 | 78 | 79 | 80 | 81 | **END** 82 | 83 | **Problem 3(b)** A _field_ is a commutative ring such that $0 ≠ 1$ and all nonzero elements have a multiplicative inverse, i.e., 84 | there exists $a^{-1}$ such that $a a^{-1} = 1$. Can we use the previous part to define $a^{-1} := 1/a$ to make $𝔻$ a field? Why or why not? 85 | 86 | **SOLUTION** 87 | 88 | An example that doesn't work is $z = 0 + ϵ$ where the formula is undefined, i.e, it would give: 89 | $$ 90 | z^{-1} = ∞ + ∞ϵ 91 | $$ 92 | 93 | **END** 94 | 95 | 96 | 97 | **Problem 4** Use dual numbers to compute the derivative of the following functions at $x = 0.1$: 98 | $$ 99 | \exp(\exp x \cos x + \sin x), \prod_{k=1}^3 \left({x \over k}-1\right),\hbox{ and } f^{\rm s}_2(x) = {1 + {x - 1 \over 2 + {x-1 \over 2}}} 100 | $$ 101 | 102 | **SOLUTION** 103 | 104 | 105 | We now compute the derivatives of the three functions by evaluating for $x = 0.1 + ϵ$. For the first function we have: 106 | $$ 107 | \begin{align*} 108 | &\exp(\exp(0.1 + ϵ)\cos(0.1+ϵ) + \sin(0.1+ϵ)) \\ 109 | &=\exp((\exp(0.1) + ϵ\exp(0.1))(\cos(0.1)-\sin(0.1)ϵ) + \sin(0.1)+\cos(0.1)ϵ) \\ 110 | &= \exp(\exp(0.1)\cos(0.1)+ \sin(0.1) + (\exp(0.1)(\cos(0.1)-\sin(0.1))+\cos(0.1))ϵ) \\ 111 | &= \exp(\exp(0.1)\cos(0.1)+ \sin(0.1))\\ 112 | &\qquad + \exp(\exp(0.1)\cos(0.1)+ \sin(0.1))\exp(0.1)(\cos(0.1)-\sin(0.1))+\cos(0.1))ϵ 113 | \end{align*} 114 | $$ 115 | therefore the derivative is the dual part 116 | $$ 117 | \exp(\exp(0.1)\cos(0.1)+ \sin(0.1))(\exp(0.1)(\cos(0.1)-\sin(0.1))+\cos(0.1)) 118 | $$ 119 | 120 | For the second function we have: 121 | $$ 122 | \begin{align*} 123 | \left(0.1+ϵ-1\right) \left({0.1 + ϵ \over 2}-1\right)\left({0.1 + ϵ \over 3}-1\right) 124 | &=\left(-0.9+ϵ\right) \left(-0.95 + ϵ/2\right)\left(-29/30 + ϵ/3\right) \\ 125 | &=\left(171/200 -1.4ϵ\right)\left(-29/30 + ϵ/3\right) \\ 126 | &= -1653/2000 + 983ϵ/600 127 | \end{align*} 128 | $$ 129 | Thus the derivative is $983/600$. 130 | 131 | For the third function we have: 132 | $$ 133 | \begin{align*} 134 | {1 + {0.1+ϵ - 1 \over 2 + {0.1+ϵ-1 \over 2}}} &= {1 + {-0.9+ϵ \over 1.55 + ϵ/2}}\\ 135 | &= 1 -18/31 + 2ϵ/1.55^2 136 | \end{align*} 137 | $$ 138 | Thus the derivative is $2/1.55^2$. 139 | 140 | **END** 141 | -------------------------------------------------------------------------------- /src/sheets/sheet3s.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 3** 2 | 3 | **Problem 1** With 8-bit unsigned integers, what is the result for the following computations: 4 | $$ 5 | 127 ⊕_{256} 200,\quad 2 ⊗_{256} 127,\quad 2 ⊗_{256} 128, \quad 0 ⊖_{256} 1 6 | $$ 7 | 8 | **SOLUTION** 9 | $$ 10 | 127 ⊕_{256} 200 = 327\ ({\rm mod} 256) = 71 11 | $$ 12 | $$ 13 | 2 ⊗_{256} 127 = 254 14 | $$ 15 | $$ 16 | 2 ⊗_{256} 128 = 256 \ ({\rm mod} 256) = 0 17 | $$ 18 | $$ 19 | 0 ⊖_{256} 1 = -1 \ ({\rm mod} 256) = 255. 20 | $$ 21 | 22 | 23 | 24 | **END** 25 | 26 | **Problem 2(a)** With 8-bit signed integers, what are the bits for the following: $10, 120, -10$. 27 | 28 | **SOLUTION** 29 | We can find the binary digits by repeatedly subtracting the largest power of 2 less than a number 30 | until we reach 0, e.g. $10 - 2^3 - 2 = 0$ 31 | implies $10 = (1010)_2$. 32 | Thus the bits are: `00001010`. 33 | Similarly, 34 | $$ 35 | 120 = 2^6 + 2^5 + 2^4 + 2^3 = (1111000)_2 36 | $$ 37 | Thus the bits are `01111000`. 38 | For negative numbers we perform the same trick but adding $2^p$ to make it positive, e.g., 39 | $$ 40 | -10 = 2^8 - 10 ({\rm mod 2^8}) = 246 = 2^7 + 2^6 + 2^5 + 2^4 + 2^2 + 2 = (11110110)_2 41 | $$ 42 | This the bits are: `11110110`. 43 | **END** 44 | 45 | **Problem 2(b)** With 8-bit signed integers, what is the result for the following computations: 46 | $$ 47 | 127 ⊕_{256}^{\rm s} 200,\quad 2 ⊗_{256}^{\rm s} 127, \quad 2 ⊗_{256}^{\rm s} 128, \quad 0 ⊖_{256}^{\rm s} 1 48 | $$ 49 | 50 | **SOLUTION** 51 | The first and 3rd part are somewhat trick questions since $200$ and $128$ aren't representable using 52 | 8-bit integers, but if we interpret the problem as modulo arithimetic we have: 53 | 54 | $$ 55 | 127 ⊕_{256}^{\rm s} 200 = 327\ ({\rm mod}^{\rm s} 256) = 71, 56 | $$ 57 | $$ 58 | 2 ⊗_{256}^{\rm s} 127 = 254\ ({\rm mod}^{\rm s} 256) = -2, 59 | $$ 60 | 61 | $$ 62 | 2 ⊗_{256}^{\rm s} 128 = 256 \ ({\rm mod}^{\rm s} 256) = 0, 63 | $$ 64 | 65 | $$ 66 | 0 ⊖_{256}^{\rm s} 1 = -1 \ ({\rm mod} 256)^{\rm s} = -1. 67 | $$ 68 | 69 | **END** 70 | 71 | 72 | **Problem 3** What is $π$ to 5 binary places? Hint: recall that $π ≈ 3.14$. 73 | 74 | **SOLUTION** 75 | We subtract off powers of two until we get 5 places. Eg we have 76 | $$ 77 | π = 3.14… = 2 + 1.14… = 2 + 1 + 0.14… = 2 + 1 + 1/8 + 0.016… = 2 + 1 + 1/8 + 1/64 + 0.000… 78 | $$ 79 | Thus we have $π = (11.001001…)_2$. The question is slightly ambiguous whether we want to round 80 | to 5 digits so either `11.00100` or `11.00101` would be acceptable. 81 | **END** 82 | 83 | **Problem 4** What are the single precision $F_{32} = F_{127,8,23}$ floating point representations for the following: 84 | $$ 85 | 2, \quad 31, \quad 32, \quad 23/4, \quad (23/4)\times 2^{100} 86 | $$ 87 | 88 | **SOLUTION** 89 | Recall that we have `σ,Q,S = 127,8,23`. Thus we write 90 | $$ 91 | 2 = 2^{128-127} * (1.00000000000000000000000)_2 92 | $$ 93 | The exponent bits are those of 94 | $$ 95 | 128 = 2^7 = (10000000)_2 96 | $$ 97 | Hence we get the bits 98 | ``` 99 | 0 10000000 00000000000000000000000 100 | ``` 101 | We write 102 | $$ 103 | 31 = (11111)_2 = 2^{131-127} * (1.1111)_2 104 | $$ 105 | And note that $131 = (10000011)_2$ Hence we have the bits 106 | ``` 107 | 0 10000011 11110000000000000000000 108 | ``` 109 | 110 | On the other hand, 111 | $$ 112 | 32 = (100000)_2 = 2^{132-127} 113 | $$ 114 | and $132 = (10000100)_2$ hence we have the bits 115 | ``` 116 | 0 10000100 00000000000000000000000 117 | ``` 118 | Note that 119 | $$ 120 | 23/4 = 2^{-2} * (10111)_2 = 2^{129-127} * (1.0111)_2 121 | $$ 122 | and $129 = (10000001)_2$ hence we get: 123 | ``` 124 | 0 10000001 01110000000000000000000 125 | ``` 126 | Finally, 127 | $$ 128 | 23/4 * 2^{100} = 2^{229-127} * (1.0111)_2 129 | $$ 130 | and $229 = (11100101)_2$ giving us: 131 | ``` 132 | 0 11100101 01110000000000000000000 133 | ``` 134 | **END** 135 | 136 | 137 | **Problem 5** Let $m(y) = \min\{x \in F_{32} : x > y \}$ be the smallest single precision number 138 | greater than $y$. What is $m(2) - 2$ and $m(1024) - 1024$? 139 | 140 | **SOLUTION** 141 | The next float after $2$ is $2 * (1 + 2^{-23})$ hence we get $m(2) - 2 = 2^{-22}$: 142 | ```julia 143 | nextfloat(2f0) - 2, 2^(-22) 144 | ``` 145 | similarly, for $1024 = 2^{10}$ we find that the difference $m(1024) - 1024$ is $2^{10-23} = 2^{-13}$: 146 | ```julia 147 | nextfloat(1024f0) - 1024, 2^(-13) 148 | ``` 149 | **END** 150 | -------------------------------------------------------------------------------- /src/sheets/sheet5s.jmd: -------------------------------------------------------------------------------- 1 | **Numerical Analysis MATH50003 (2023–24) Problem Sheet 5** 2 | 3 | 4 | 5 | **Problem 1(a)** Suppose $|ϵ_k| ≤ ϵ$ and $n ϵ < 1$. Use induction to show that 6 | $$ 7 | ∏_{k=1}^n (1+ϵ_k) = 1+θ_n 8 | $$ 9 | for some constant $θ_n$ satisfying 10 | $$ 11 | |θ_n| ≤ \underbrace{n ϵ \over 1-nϵ}_{E_{n,ϵ}} 12 | $$ 13 | 14 | **SOLUTION** 15 | 16 | $$ 17 | ∏_{k=1}^{n+1} (1+ϵ_k) = ∏_{k=1}^n (1+ϵ_k) (1+ϵ_{n+1}) = (1+θ_n)(1+ϵ_{n+1}) = 1 + \underbrace{θ_n + ϵ_{n+1} + θ_nϵ_{n+1}}_{θ_{n+1}} 18 | $$ 19 | where 20 | $$ 21 | \begin{align*} 22 | |θ_{n+1}| &≤ {n ϵ \over 1-nϵ}(1+ϵ) + ϵ \\ 23 | &={n ϵ + n ϵ^2 \over 1-(n+1)ϵ} \underbrace{{1-(n+1)ϵ \over 1-nϵ}}_{≤ 1} + {ϵ-(n+1)ϵ^2 \over 1-(n+1)ϵ} \\ 24 | &≤ {(n+1) - ϵ \over 1-(n+1)ϵ} ϵ ≤ {(n+1) ϵ \over 1-(n+1)ϵ} = E_{n+1,ϵ}. 25 | \end{align*} 26 | $$ 27 | 28 | **END** 29 | 30 | 31 | **Problem 1(b)** Show for an idealised floating point vector $𝐱 ∈ F_{∞,S}^n$ 32 | that 33 | $$ 34 | x_1 ⊕ ⋯ ⊕ x_n = x_1 + ⋯ + x_n + σ_n 35 | $$ 36 | where 37 | $$ 38 | |σ_n| ≤ \| 𝐱 \|_1 E_{n-1,ϵ_{\rm m}/2}, 39 | $$ 40 | assuming $n ϵ_{\rm m} < 2$ and where 41 | $$ 42 | \|𝐱\|_1 := ∑_{k=1}^n |x_k|. 43 | $$ 44 | Hint: use the previous part to first write 45 | $$ 46 | x_1 ⊕ ⋯ ⊕ x_n = x_1(1+θ_{n-1}) + ∑_{j=2}^n x_j (1 + θ_{n-j+1}). 47 | $$ 48 | 49 | 50 | **SOLUTION** 51 | 52 | Using Problem 2.1 we write: 53 | $$ 54 | \meeq{ 55 | (⋯((x_1 + x_2)(1+δ_1) + x_3)(1+δ_2)⋯+x_n)(1+δ_{n-1}) 56 | = x_1 ∏_{k=1}^{n-1} (1+δ_k) + ∑_{j=2}^n x_j ∏_{k=j-1}^{n-1} (1+δ_j) \ccr 57 | = x_1(1+θ_{n-1}) + ∑_{j=2}^n x_j (1 + θ_{n-j+1}) 58 | } 59 | $$ 60 | where we have for $j = 2,…,n$ 61 | $$ 62 | |θ_{n-j+1}| ≤ E_{n-j+1,ϵ_{\rm m}/2} ≤ E_{n-1,ϵ_{\rm m}/2}. 63 | $$ 64 | Thus we have 65 | $$ 66 | ∑_{j=1}^n x_j (1 + θ_{n-j+1})= ∑_{j=1}^n x_j + \underbrace{∑_{j=1}^n x_j θ_{n-j+1}}_{σ_n} 67 | $$ 68 | where 69 | $$ 70 | |σ_n| ≤ ∑_{j=1}^n |x_j θ_{n-j+1}| ≤ \sup_j |θ_{n-j+1}| ∑_{j=1}^n |x_j| ≤ \| 𝐱\|_1 E_{n-1,ϵ_{\rm m}/2}. 71 | $$ 72 | 73 | 74 | **END** 75 | 76 | **Problem 1(c)** For $A ∈ F_{∞,S}^{n × n}$ and $𝐱 ∈ F_{∞,S}^n$ consider the error 77 | in approximating matrix multiplication with idealised floating point: for 78 | $$ 79 | A 𝐱 = \begin{pmatrix} 80 | ⨁_{j=1}^n A_{1,j} ⊗ x_j\\ 81 | ⋮ \\ 82 | ⨁_{j=1}^n A_{1,j} ⊗ x_j 83 | \end{pmatrix} + δ 84 | $$ 85 | show that 86 | $$ 87 | \| δ \|_∞ ≤ 2 \|A\|_∞ \| 𝐱 \|_∞ E_{n,ϵ_{\rm m}/2} 88 | $$ 89 | where $n ϵ_{\rm m} < 2$ and the matrix norm is $\|A \|_∞ := \max_k ∑_{j=1}^n |a_{kj}|$. 90 | 91 | **SOLUTION** 92 | We have for the $k$=th row 93 | $$ 94 | ⨁_{j=1}^n A_{k,j} ⊗ x_j = ⨁_{j=1}^n A_{k,j} x_j (1+δ_j) = 95 | ∑_{j=1}^n A_{k,j} x_j (1+δ_j) + σ_{k,n} 96 | $$ 97 | where we know $|σ_n| ≤ M_k E_{n-1,ϵ_{\rm m}/2}$, where from 1(b) we have 98 | $$ 99 | M_k = Σ_{j=1}^n |A_{k,j}x_j (1+δ_j)| = Σ_{j=1}^n |A_{k,j}| |x_j| (1+|δ_j|) ≤ 2 \max |x_j| Σ_{j=1}^n |A_{k,j}| 100 | ≤ 2 \|𝐱\|_∞ \|A \|_∞ 101 | $$ 102 | Similarly, we also have 103 | $$ 104 | |∑_{j=1}^n A_{k,j} x_j δ_j| ≤ \|𝐱\|_∞ \|A \|_∞ ϵ_{\rm m}/2 105 | $$ 106 | and so the result follows from 107 | $$ 108 | ϵ_{\rm m}/2 + 2E_{n-1,ϵ_{\rm m}/2} ≤ {ϵ_{\rm m}/2 + ϵ_{\rm m} (n-1) \over 1-(n-1)ϵ_{\rm m}/2} ≤ {ϵ_{\rm m} n \over 1-nϵ_{\rm m}/2} = 2E_{n,ϵ_{\rm m}/2}. 109 | $$ 110 | **END** 111 | 112 | 113 | 114 | --------- 115 | 116 | 117 | **Problem 2** Derive Backward Euler: use the left-sided divided difference approximation 118 | $$ 119 | u'(x) ≈ {u(x) - u(x-h) \over h} 120 | $$ 121 | to reduce the first order ODE 122 | $$ 123 | \meeq{ 124 | u(a) = c, \qquad u'(x) + ω(x) u(x) = f(x) 125 | } 126 | $$ 127 | to a lower triangular system by discretising on the grid $x_j = a + j h$ for $h = (b-a)/n$. Hint: only impose the ODE on the 128 | gridpoints $x_1,…,x_n$ so that the divided difference does not depend on behaviour at $x_{-1}$. 129 | 130 | 131 | **SOLUTION** 132 | 133 | We go through all 4 steps (this is to help you understand what to do. In an exam I will still give full credit if you get the right result, 134 | even if you don't write down all 4 steps): 135 | 136 | (Step 1) Since we need to avoid going off the left in step 2 we start the ODE discretisation at $x_1$: 137 | $$ 138 | \Vectt[u(x_0), u'(x_1) + ω(x_1)u(x_1), ⋮, u'(x_n) + ω(x_n)u(x_n)] = \underbrace{\Vectt[c, f(x_1),⋮,f(x_n)]}_{𝐛} 139 | $$ 140 | 141 | (Step 2) Replace with divided differences: 142 | $$ 143 | \Vectt[u(x_0), (u(x_1)-u(x_0))/h + ω(x_1)u(x_1), ⋮, (u(x_n)-u(x_{n-1}))/h + ω(x_n)u(x_n)] ≈ 𝐛 144 | $$ 145 | 146 | (Step 3) Replace with discrete system with equality: 147 | $$ 148 | \Vectt[u_0, (u_1-u_0)/h + ω(x_1)u_1, ⋮, (u_n-u_{n-1}))/h + ω(x_n)u_n] = 𝐛 149 | $$ 150 | 151 | (Step 4) Write as linear system: 152 | $$ 153 | \begin{bmatrix} 154 | 1 \\ 155 | -1/h & 1/h + ω(x_1) \\ 156 | & ⋱ & ⋱ \\ 157 | && -1/h & 1/h + ω(x_n) 158 | \end{bmatrix} \Vectt[u_0,⋮,u_n] = 𝐛 159 | $$ 160 | 161 | 162 | **END** 163 | 164 | 165 | **Problem 3** Reduce a Schrödinger equation to a tridiagonal linear system by discretising on the grid $x_j = a + j h$ for $h = (b-a)/n$: 166 | $$ 167 | \meeq{ 168 | u(a) = c,\qquad u''(x) + V(x) u(x) = f(x), \qquad u(b) = d. 169 | } 170 | $$ 171 | 172 | **SOLUTION** 173 | 174 | (Step 1) 175 | $$ 176 | \Vectt[u(x_0), u''(x_1) + V(x_1) u(x_1), ⋮, u'(x_{n-1}) + V(x_{n-1}) u(x_{n-1}), u(x_n)] = \underbrace{\Vectt[c, f(x_1),⋮,f(x_{n-1}), d]}_{𝐛} 177 | $$ 178 | 179 | (Step 2) Replace with divided differences: 180 | $$ 181 | \Vectt[u(x_0), (u(x_0)-2u(x_1)+u(x_2))/h^2 + V(x_1)u(x_1), ⋮, (u(x_{n-2} - 2u(x_{n-1})+u(x_n))/h^2 + V(x_{n-1})u(x_{n-1}), u(x_n)] ≈ 𝐛 182 | $$ 183 | 184 | (Step 3) Replace with discrete system with equality: 185 | $$ 186 | \Vectt[u_0, (u_0-2u_1+u_2)/h^2 + V(x_1)u_1, ⋮, (u_{n-2}-2u_{n-1}+u_n))/h^2+ V(x_{n-1})u_{n-1},u_n] = 𝐛 187 | $$ 188 | 189 | (Step 4) Write as a tridiagonal linear system: 190 | $$ 191 | \begin{bmatrix} 192 | 1 \\ 193 | 1/h^2 & V(x_1)-2/h^2 & 1/h^2 \\ 194 | & ⋱ & ⋱ & ⋱ \\ 195 | &&1/h^2 & V(x_{n-1})-2/h^2 & 1/h^2 \\ 196 | && &&1 197 | \end{bmatrix} \Vectt[u_0,⋮,u_n] = 𝐛 198 | $$ 199 | 200 | 201 | **END** -------------------------------------------------------------------------------- /src/sheets/template.tpl: -------------------------------------------------------------------------------- 1 | \documentclass[12pt,a4paper]{article} 2 | 3 | \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry} 4 | \usepackage{lmodern} 5 | \usepackage{amssymb,amsmath} 6 | \usepackage{bm} 7 | \usepackage{graphicx} 8 | \usepackage{microtype} 9 | \usepackage{hyperref} 10 | \usepackage[usenames,dvipsnames]{xcolor} 11 | {{#:tex_deps}} 12 | {{{ :tex_deps }}} 13 | {{/:tex_deps}} 14 | \setlength{\parindent}{0pt} 15 | \setlength{\parskip}{1.2ex} 16 | 17 | 18 | 19 | 20 | \hypersetup 21 | { pdfauthor = { {{{:author}}} }, 22 | pdftitle={ {{{:title}}} }, 23 | colorlinks=TRUE, 24 | linkcolor=black, 25 | citecolor=blue, 26 | urlcolor=blue 27 | } 28 | 29 | {{#:title}} 30 | \title{ {{{ :title }}} } 31 | {{/:title}} 32 | 33 | {{#:author}} 34 | \author{ {{{ :author }}} } 35 | {{/:author}} 36 | 37 | {{#:date}} 38 | \date{ {{{ :date }}} } 39 | {{/:date}} 40 | 41 | {{ :highlight }} 42 | 43 | \def\endash{–} 44 | \def\bbD{ {\mathbb D} } 45 | \def\bbZ{ {\mathbb Z} } 46 | \def\bbR{ {\mathbb R} } 47 | \def\bbC{ {\mathbb C} } 48 | 49 | \def\x{ {\vc x} } 50 | \def\a{ {\vc a} } 51 | \def\b{ {\vc b} } 52 | \def\e{ {\vc e} } 53 | \def\f{ {\vc f} } 54 | \def\u{ {\vc u} } 55 | \def\v{ {\vc v} } 56 | \def\y{ {\vc y} } 57 | \def\z{ {\vc z} } 58 | \def\w{ {\vc w} } 59 | 60 | \def\bt{ {\tilde b} } 61 | \def\ct{ {\tilde c} } 62 | \def\Ut{ {\tilde U} } 63 | \def\Qt{ {\tilde Q} } 64 | \def\Rt{ {\tilde R} } 65 | \def\Xt{ {\tilde X} } 66 | \def\acos{ {\rm acos}\, } 67 | 68 | \def\red#1{ {\color{red} #1} } 69 | \def\blue#1{ {\color{blue} #1} } 70 | \def\green#1{ {\color{ForestGreen} #1} } 71 | \def\magenta#1{ {\color{magenta} #1} } 72 | 73 | \input{somacros} 74 | 75 | \begin{document} 76 | 77 | {{#:title}}\maketitle{{/:title}} 78 | 79 | {{{ :body }}} 80 | 81 | \end{document} -------------------------------------------------------------------------------- /src/sheets/unused.jl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Imperial-MATH50003/MATH50003NumericalAnalysis2023-24/668e99ee4f6b912a908206ef7d00b54a2f5032ba/src/sheets/unused.jl -------------------------------------------------------------------------------- /src/slides/slides1.jl: -------------------------------------------------------------------------------- 1 | using Plots, IntervalSets 2 | 3 | f = x -> exp(x)cos(x^2) 4 | 5 | n = 7 6 | x = range(0,1,n+1) 7 | 8 | 9 | ### 10 | # Right 11 | ### 12 | 13 | p = plot(legend=false, title="(Right-sided) Rectangular Rule") 14 | for k = 1:n 15 | plot!([x[k:k+1]; x[k+1:-1:k]; x[k]], [0,0,f(x[k+1]),f(x[k+1]),0], color=:darkred, fill=(0,0.5,:red)) 16 | end; p 17 | plot!(range(0,1,1000), f; linewidth=2, color=:blue) 18 | savefig("slides/figures/slides1_rightrect.pdf") 19 | 20 | ### 21 | # Left 22 | ### 23 | 24 | p = plot(legend=false, title="(Left-sided) Rectangular Rule") 25 | for k = 1:n 26 | plot!([x[k:k+1]; x[k+1:-1:k]; x[k]], [0,0,f(x[k]),f(x[k]),0], color=:darkred, fill=(0,0.5,:red)) 27 | end; p 28 | plot!(range(0,1,1000), f; linewidth=2, color=:blue) 29 | savefig("slides/figures/slides1_leftrect.pdf") 30 | 31 | ### 32 | # Trapezium 33 | ### 34 | 35 | p = plot(legend=false, title="Trapezium Rule") 36 | for k = 1:n 37 | plot!([x[k:k+1]; x[k+1:-1:k]; x[k]], [0,0,f(x[k+1]),f(x[k]),0], color=:darkred, fill=(0,0.5,:red)) 38 | end; p 39 | plot!(range(0,1,1000), f; linewidth=2, color=:blue) 40 | savefig("slides/figures/slides1_trap.pdf") 41 | 42 | -------------------------------------------------------------------------------- /src/slides/slides2.jl: -------------------------------------------------------------------------------- 1 | using Plots 2 | 3 | f = x -> exp(x)cos(x^2) 4 | 5 | n = 7 6 | x = range(0,1,n+1) 7 | 8 | 9 | ### 10 | # Right 11 | ### 12 | 13 | x = 0.5 14 | for h = (0.3, 0.1, 0.01, 0.0000000000000001) 15 | p = plot(title="(Right-sided) Divided Difference") 16 | plot!(range(0,1,1000), f; linewidth=2, color=:blue, label="f") 17 | scatter!([x,x+h], f.([x,x+h]); color=:darkred, label=nothing) 18 | # (f(x+h)-f(x))/h * (t-x) + f(x) 19 | plot!([0,1], [(f(x+h)-f(x))/h * (-x) + f(x), (f(x+h)-f(x))/h * (1-x) + f(x)], color=:darkred, linewidth=2, label="h = $h") 20 | p 21 | savefig("slides/figures/slides2_rightdiff_h=$h.pdf") 22 | end 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/slides/slides4.jl: -------------------------------------------------------------------------------- 1 | using Plots 2 | using ForwardDiff: derivative 3 | 4 | f = x -> exp(x)cos(8x^2) 5 | 6 | x = range(0,1,100) 7 | plot(x, f.(x); linewidth=2, legend=false) 8 | 9 | r = [sqrt(π/2)/(2sqrt(2)),sqrt(-π/2+2π)/(2sqrt(2)),sqrt(π/2 + 2π)/(2sqrt(2))] 10 | scatter!(r, zero(r)) 11 | savefig("slides/figures/slides4_funct.pdf") 12 | 13 | -------------------------------------------------------------------------------- /src/slides/slides_classicalops.jl: -------------------------------------------------------------------------------- 1 | using Plots, LaTeXStrings 2 | p = plot() 3 | for k = 1:5 4 | plot!(ChebyshevT()[:,k]; label = latexstring("T_{$(k-1)}"), linewidth=2) 5 | end; p 6 | savefig("slides/figures/chebyshevt.pdf") 7 | 8 | 9 | p = plot() 10 | for k = 1:5 11 | plot!(ChebyshevU()[:,k]; label = latexstring("U_{$(k-1)}"), linewidth=2) 12 | end; p 13 | savefig("slides/figures/chebyshevu.pdf") 14 | 15 | 16 | p = plot() 17 | for k = 1:5 18 | plot!(Legendre()[:,k]; label = latexstring("P_{$(k-1)}"), linewidth=2) 19 | end; p 20 | savefig("slides/figures/legendrep.pdf") -------------------------------------------------------------------------------- /src/slides/slides_fourier.jl: -------------------------------------------------------------------------------- 1 | using FFTW, Plots, Test 2 | 3 | 4 | f = θ -> exp(θ/(2π)) 5 | using LaTeXStrings 6 | 7 | for n = [5, 11, 101] 8 | θ = range(0, 2π, n+1)[1:end-1] 9 | 10 | f̂ = fft(f.(θ))/n 11 | 12 | @test n*ifft(f̂) ≈ f.(θ) 13 | 14 | 15 | M = 500 16 | N = 2M+1 17 | m = n ÷ 2 18 | 19 | 20 | g = range(0, 2π, N+1) 21 | plot(g, real.(f.(g)); label=L"f", linewidth=2, title="Real part, n = $n") 22 | v = N*ifft([f̂[1:m+1]; zeros(N - n); f̂[m+2:end]]); v = [v; v[1]]; plot!(g, real.(v); label=L"Re(f_{-m:m})", linewidth=2) 23 | scatter!(θ, real.(f.(θ)); label=nothing) 24 | savefig("slides/figures/realfft_m_exp_$n.pdf") 25 | end 26 | 27 | 28 | using ClassicalOrthogonalPolynomials 29 | for n = [5, 11, 101] 30 | T_n =chebyshevt(0..1)[:,1:n] 31 | f_n = expand(T_n, exp) 32 | g = range(0, 1, N+1) 33 | plot(g, exp.(g), linewidth=2, title="n = $n", label=L"f") 34 | plot!(g, f_n[g], linewidth=2, label=L"f_n") 35 | 36 | x = ClassicalOrthogonalPolynomials.grid(T_n) 37 | scatter!(x, exp.(x); label=nothing) 38 | savefig("slides/figures/cheb_exp_$n.pdf") 39 | end -------------------------------------------------------------------------------- /src/slides/slides_gaussquad.jl: -------------------------------------------------------------------------------- 1 | using Plots, FastGaussQuadrature, LinearAlgebra 2 | 3 | f = x -> 1/(25(x-1/2)^2+1) 4 | function trapeziumrule(f, n) 5 | ret = f(-1)/2 6 | for j = 1:n-1 # j skips first and lest point 7 | ret = ret + f(-1+2j/n) 8 | end 9 | ret = ret + f(1)/2 10 | 2ret/n 11 | end 12 | rectrule(n) = range(-1,1; length=n+1)[1:end-1], fill(2/n,n) 13 | traprule(m) = (n = m-1; (range(-1,1; length=n+1), [1/(2n); fill(1/n,n-1); 1/(2n)]*2)) 14 | 15 | ex = (1/5)*(atan(5/2) + atan(15/2)) 16 | ns = 2:100 17 | plot(ns, [((x,w) = rectrule(n); abs(dot(w,f.(x)) - ex)) for n = ns]; yscale=:log10, linewidth=2, label="Left-Rectangular", legend=:bottomleft, xlabel="number of points", ylabel="Error", yticks=10.0 .^ (-16:0)) 18 | plot!(ns, [((x,w) = traprule(n); abs(dot(w,f.(x)) - ex)) for n = ns]; yscale=:log10, linewidth=2, label="Trapezium") 19 | nanabs(x) = x == 0 ? NaN : abs(x) 20 | plot!([((x,w) = gausslegendre(n); nanabs(dot(w,f.(x)) - ex)) for n = ns]; yscale=:log10, linewidth=2, label="Gauss") 21 | 22 | savefig("slides/figures/gausserror.pdf") 23 | 24 | -------------------------------------------------------------------------------- /src/slides/slides_ops.jl: -------------------------------------------------------------------------------- 1 | using FFTW, Plots 2 | f = θ -> 2/(2-exp(im*θ)) 3 | 4 | for n = [5, 10] 5 | θ = range(0, 2π, n+1)[1:end-1] 6 | 7 | f̂ = fft(f.(θ))/n 8 | 9 | @test n*ifft(f̂) ≈ f.(θ) 10 | 11 | 12 | m = 1000 13 | g = range(0, 2π, m+1) 14 | plot(g, real.(f.(g)); label="Re(f)", linewidth=2, title="Real part, n = $n") 15 | v = m*ifft([f̂; zeros(m-length(f̂))]); v = [v; v[1]]; plot!(g, real.(v); label="Re(fₙ)", linewidth=2) 16 | scatter!(θ, real.(f.(θ)); label=nothing) 17 | savefig("slides/figures/realfft_analytic_$n.pdf") 18 | 19 | plot(g, imag.(f.(g)); label="Im(f)", linewidth=2, title="Imag part, n = $n") 20 | v = m*ifft([f̂; zeros(m-length(f̂))]); v = [v; v[1]]; plot!(g, imag.(v); label="Im(fₙ)", linewidth=2) 21 | scatter!(θ, imag.(f.(θ)); label=nothing) 22 | savefig("slides/figures/imagfft_analytic_$n.pdf") 23 | end 24 | 25 | f = θ -> 2/(25cos(θ-1)^2 + 1) 26 | 27 | for n = [5, 10, 100] 28 | θ = range(0, 2π, n+1)[1:end-1] 29 | 30 | f̂ = fft(f.(θ))/n 31 | 32 | @test n*ifft(f̂) ≈ f.(θ) 33 | 34 | 35 | m = 1000 36 | g = range(0, 2π, m+1) 37 | plot(g, real.(f.(g)); label="Re(f)", linewidth=2, title="Real part, n = $n") 38 | v = m*ifft([f̂; zeros(m-length(f̂))]); v = [v; v[1]]; plot!(g, real.(v); label="Re(fₙ)", linewidth=2) 39 | scatter!(θ, real.(f.(θ)); label=nothing) 40 | savefig("slides/figures/realfft_runge_$n.pdf") 41 | 42 | plot(g, imag.(f.(g)); label="Im(f)", linewidth=2, title="Imag part, n = $n") 43 | v = m*ifft([f̂; zeros(m-length(f̂))]); v = [v; v[1]]; plot!(g, imag.(v); label="Im(fₙ)", linewidth=2) 44 | scatter!(θ, imag.(f.(θ)); label=nothing) 45 | savefig("slides/figures/imagfft_runge_$n.pdf") 46 | end 47 | 48 | using LaTeXStrings 49 | 50 | for n = [5, 11, 101] 51 | θ = range(0, 2π, n+1)[1:end-1] 52 | 53 | f̂ = fft(f.(θ))/n 54 | 55 | @test n*ifft(f̂) ≈ f.(θ) 56 | 57 | 58 | M = 500 59 | N = 2M+1 60 | m = n ÷ 2 61 | 62 | 63 | g = range(0, 2π, N+1) 64 | plot(g, real.(f.(g)); label=L"Re(f)", linewidth=2, title="Real part, n = $n") 65 | v = N*ifft([f̂[1:m+1]; zeros(N - n); f̂[m+2:end]]); v = [v; v[1]]; plot!(g, real.(v); label=L"Re(f_{-m:m})", linewidth=2) 66 | scatter!(θ, real.(f.(θ)); label=nothing) 67 | savefig("slides/figures/realfft_m_runge_$n.pdf") 68 | 69 | plot(g, imag.(f.(g)); label="Im(f)", linewidth=2, title="Imag part, n = $n", ylims=(-5E-16,5E-16)) 70 | v = N*ifft([f̂[1:m+1]; zeros(N - n); f̂[m+2:end]]); v = [v; v[1]]; plot!(g, imag.(v); label="Im(fₙ)", linewidth=2) 71 | scatter!(θ, imag.(f.(θ)); label=nothing) 72 | savefig("slides/figures/imagfft_m_runge_$n.pdf") 73 | end -------------------------------------------------------------------------------- /src/slides/slides_regress.jl: -------------------------------------------------------------------------------- 1 | using Plots 2 | 3 | function lagrange(𝐱, k, x) 4 | ret = one(x) 5 | n = length(𝐱) 6 | for j = 1:n 7 | if j ≠ k 8 | ret *= (x-𝐱[j])/(𝐱[k]-𝐱[j]) 9 | end 10 | end 11 | ret 12 | end 13 | 14 | n = 4 15 | 𝐱 = range(0, 1; length=n) # evenly spaced points (BAD for interpolation) 16 | g = range(0, 1; length=1000) 17 | plot(g, lagrange.(Ref(𝐱), 1, g); label="ℓ₁"); scatter!(𝐱, lagrange.(Ref(𝐱), 1, 𝐱); label=nothing, color=:blue) 18 | savefig("slides/figures/slides_regress_l1.pdf") 19 | 20 | k = 2; plot!(g, lagrange.(Ref(𝐱), k, g); label="ℓ₂", color=:red); scatter!(𝐱, lagrange.(Ref(𝐱), k, 𝐱); label=nothing, color=:red) 21 | k = 3; plot!(g, lagrange.(Ref(𝐱), k, g); label="ℓ₃", color=:green); scatter!(𝐱, lagrange.(Ref(𝐱), k, 𝐱); label=nothing, color=:green) 22 | k = 4; plot!(g, lagrange.(Ref(𝐱), k, g); label="ℓ₄", color=:orange); scatter!(𝐱, lagrange.(Ref(𝐱), k, 𝐱); label=nothing, color=:orange) 23 | 24 | savefig("slides/figures/slides_regress_l.pdf") --------------------------------------------------------------------------------