Overview 1 Recap 2 Partial differential equations Classification Finite difference methods Boundary conditions 3 Summary Quiz next week! Recap Many boundary-value odes can be discretised, made into matrix equations Three general methods for solving matrix equation Ax = b: I I I Direct elimination (Gauss–Jordan etc) Iteration (Gauss–Seidel etc) for diagonally dominant matrices Conjugate gradient type for sparse matrices Sparse matrices appear in many physical problems I I Huge savings in storage and computation Many numerical methods are tailor-made for sparse matrices Partial differential equations A very large number of physical problems are formulated as pdes: Schrödinger equation i~ i ∂Ψ h ~2 2 → = − ∇ + V (− x) Ψ ∂t 2m Poisson equation → ∇2 u = ρ(− x) Navier–Stokes equation ρ( → ∂− u 1 → → → → +− u∇·− u ) = −∇p + η∇2 − u + ( η + ζ)∇(∇ · − u) ∂t 3 Korteweg–de Vries equation ∂φ ∂ 3 φ ∂φ + 3 + 6φ =0 ∂t ∂x ∂x and many more!! Complications, simplifications ODE: One variable, one general theory, general methods PDE: Several mathematically very different cases! We have 3 (10?) space dimensions, but only one time dimension Equations must be 0th, 1st or 2nd order in time: ṗ = − and p = p(q, q̇, t) ∂H ∂q Classification Traditional classification Second-order pde, 2 variables: a ∂2u ∂2u ∂2u ∂u ∂u + 2b + c +d +e + fu + g = 0 ∂x 2 ∂x∂y ∂y 2 ∂x ∂y elliptic parabolic hyperbolic b 2 − ac > 0 Poisson b 2 − ac = 0 Diffusion b 2 − ac < 0 Wave Not very useful from computational view Classification Computational classification Time evolution (initial value problem) I I Mostly hyperbolic, parabolic Most important issue: stability Static solution (boundary value problem) I I Mostly elliptic Most important issue: efficiency A lot of pdes are a mix of parabolic, hyperbolic, elliptic! Methods Finite differences Finite elements Monte Carlo Spectral methods (later!) Variational ... Finite differences 1 Replace x and y with a discrete grid (i, j): xi = x0 + iδx , yj = y0 + jδy Function is given by values on lattice points: u(x, y ) → uij 2 3 Replace derivatives by finite differences — but how? Result can be written as matrix equation Au = b I I 4 u, b are N = nx × ny vectors A is a sparse N × N matrix Use direct/iteration/Fourier methods to solve for u In isotropic systems (with static solutions) we usually choose δx = δy = a but for time evolution we need δt δx Poisson equation ∂2Φ ∂2Φ + = ρ(x, y ) ∂x 2 ∂y 2 Discrete derivative: =⇒ ∂2Φ ∂x 2 → Φi+1,j − 2Φi,j + Φi−1,j a2 Φi+1,j + Φi−1,j + Φi,j−1 + Φi,j+1 − 4Φi,j = a2 ρij ≡ ρ̂ij To write as matrix equation, relabel coordinates: k = i + Nx j d d w d d We get AΦ = ρ̂ Amn = δm,n+1 + δm,n−1 + δm,n+Nx + δm,n−Nx − 4δmn A is sparse: most Amn = 0! Boundary conditions Three important classes of boundary conditions: Dirichlet Φ(x) = b(x) on boundary Neumann ∂n Φ(x) = b 0 (x) on boundary ∂n Φ is normal derivative, orthogonal to boundary Periodic Φ(x + L) = Φ(x) This only works for regular (rectangular) boundaries! Used mostly when we are interested in bulk behaviour → take large volume limit Topology changes: line → circle, rectangle → torus, . . . Implementation Dirichlet If one of the neighbours is on the boundary, it gets replaced by the boundary value bj in the finite difference: ∇2 Φ1,j → b0j + Φ2,j + Φ1,j−1 + Φ1,j+1 − 4Φ1,j Recipe: Modify finite diff on edges to include only ‘available’ directions Subtract (sum of) boundary values from source term Implementation Neumann Use the forward/backward derivative on the boundary: 0 b0j = (∂Φ)0j = Φ1j − Φ0j 0 in finite diff operator So we replace Φ0j → Φ1j − b0j Recipe: Modify finite diff operator on edges to replace ‘unavailable’ direction ‘Φ0 ’ by Φk Add (sum of) boundary values to source term Periodic If we are at the edge, the nearest neighbour is at opposite edge! So Φ0j → ΦLj ; ΦL+1,j → Φ1j etc in all finite differences Irregular boundaries We start by mapping our boundary onto the regular grid Number interior grid points and boundary points Find distances from interior points to boundary Taylor expand differential operators near boundary t tt b5 ........................................................ .................... ........ 13 14 ................. ..... .... . . . . .... . . . ..... . ... . . . . . . ... b....3......... ... . . 9 10 11 12 .... b2 ...... 8 . .. . . . . ... . .. . . ... ... .. .. . . . . . . . . .. .. ... b1 .... 4 5 6 7 ... .. .... .. ... . .. . . ... .... ... ..... ...... ... ....... .... ....... . . ..... . . . . . ... 2 3 ....... 1 .......... ........ ........... ........... .............. .................. ....................................................... t .......... θ b..4...................... tt t t tt t tt t t tt Repeat for all boundary points! Taylor expansion around Φ9 : 1 b4 ≈ Φ9 + θ∂y Φ9 + θ2 ∂y2 Φ9 2 1 Φ5 ≈ Φ9 − ∂y Φ9 + ∂y2 Φ9 2 θΦ + b − (1 + θ)Φ9 5 4 ∂y2 Φ9 ≈ θ(1 + θ) Some MatLab tricks reshape(X,M,N) reshape(X,M,N,P) rearranges X into a M×N matrix rearranges X into a M×N×P matrix etc. [X,Y] = meshgrid(x,y) numgrid(region,N) delsq(G) speye(N) creates creates creates creates del2(U) evaluates the laplacian of U 2d arrays X,Y from vectors x,y a numbered grid for various regions the 2-dim laplacian on such a grid the sparse N×N unit matrix Summary Classification of pdes: I I time evolution, initial value (mostly parabolic, hyperbolic) static solution, boundary value (mostly elliptic) Finite differences transform pdes into matrix equations I static solution problems may be solved directly Boundary conditions I I I Dirichlet: subtract boundary terms from source term Neumann: modify finite diff on boundary, add boundary term to source Periodic: wrap finite differences around boundary