[more details] [source]
Check out an interesting lecture here
PDE = equation involving partial derivatives (see also an ODE).
An examples of a PDE:
where $u(t,x,y)$ is an unknown function (e.g., the temperature) that depends on time $t$ and space $x,y$.
Let us model stretching a guitar string:
Consider a system of $N$ springs and $N+1$ masses:
Total energy: $$ E = \frac{k h}{2} + \frac{k h^2}{2} \sum_{i=0}^{N-1} (y_{i+1}-y_i)^2/(h^2) $$
Internal force on $j$-th mass: $$ f_j = -\frac{\partial E}{\partial y_j} = k \big( (y_{j+1}-y_j) - (y_{j}-y_{j-1}) \big) = k (y_{j+1}-2 y_j + y_{j-1}) $$
Equilibrium (force balance): $$ F_j = -f_j = k (-y_{j+1}+2 y_j - y_{j-1}) $$
We know that if $y_j = u(j h)$, where $u$ is a smooth function then $$ 0 = h^{-2} (y_{j+1}-2 y_j + y_{j-1}) \approx u''(hj). $$ Hence if also $F_j = h^{-2} F(j h)$ then we have $$ \frac{k}{2} u'' = F $$ as a model.
Note: The way we scaled $F$ as $h\to 0$ is not right. To make it cleaner, we should have assumed that $k$ depends on $h$ as $c h^{-2}$.
Force balance: $$ h^{-2} (u_{i+1,j} - 2 u_{i,j} + u_{i-1,j} + u_{i,j+1} - 2 u_{i,j} + u_{i,j-1}) = -F_{i,j} $$ which approximates $$ \Delta u = -F, $$ where $\Delta = \frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2}$
This equation models a membrane.
To solve a PDE we need:
Neumann (also known as free): leaving their (vertical) position free. This corresponds to setting $\frac{\partial u}{\partial n} = 0$ on $\Gamma$.
In general, one may have a non-zero RHS (right-hand side), e.g., $u|_{\partial\Omega} = g$, where $g$ is a known function.
Choose $h$---grid step size
Grid is points "that are multiples" of $h$ (also called a mesh). We denote it as $${\mathbb R}^2_h := \{(x_1,x_2) : x_1/h\in{\mathbb R}, x_2/h\in{\mathbb R}\}$$
Discrete domain: $$ \Omega_h := \Omega \cap {\mathbb R}^2_h $$
Discrete boundary: $$ \Gamma_h := \Gamma \cap {\mathbb R}^2_h $$
Recall that for $g=g(x)$, $$ \frac{d^2 f}{dx^2} = \frac{g(x-h)-2g(x)+g(x+h)}{h^2} + O(h^2) $$
Exercise: prove it by Taylor series: $$ g(x+\xi) = g(x) + \xi g'(x) + \frac{\xi^2}{2} g''(x) + \frac{\xi^3}{6} g'''(x) + O(\xi^4) $$ (refresh the Big-O notation if you need)
Hence $$ \Delta u = \Delta_h u + O(h^2),\qquad\text{where} $$ $$ \Delta_h u := \frac{u(x-h,y)+u(x+h,y)+u(x,y+h)+u(x,y-h)-4u(x,y)}{h^2}. $$
Thus, the discretized equation is $$ \frac{u(x-h,y)+u(x+h,y)+u(x,y+h)+u(x,y-h)-4u(x,y)}{h^2} = f(x,y) \qquad (x,y)\in\Omega_h $$
Discrete boundary conditions are easy: $$ u(x,y) = 0 \qquad (x,y)\in\Gamma_h $$
This is a system of linear equations SLE on $u(x,y)$ for $(x,y)\in\Omega_h\cap\Gamma_h$.
Exercise: Let $h=1/n$ how many unknowns and equations are there?
If we collect the unknowns in a vector, we will have a linear system \[ \frac{1}{h^2} \begin{pmatrix} -4 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 1 &-4 & 1 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 1 &-4 & 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 &-4 & 1 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 1 &-4 & 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 1 &-4 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 &-4 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 1 &-4 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 &-4 \\ \end{pmatrix} \begin{pmatrix} u{1,1} \ u{1,2} \ u{1,3} \ u{2,1} \ u{2,2} \ u{2,3} \ u{3,1} \ u{3,2} \ u_{3,3}
\begin{pmatrix} f_{1,1} \\ f_{1,2} \\ f_{1,3} \\ f_{2,1} \\ f_{2,2} \\ f_{2,3} \\ f_{3,1} \\ f_{3,2} \\ f_{3,3} \end{pmatrix} \]
Exercise: guess how $f_{i,j}$ is defined
The matrix has a block form $$ \begin{pmatrix} A & I & O \\ I & A & I \\ O & I & A \end{pmatrix} , $$ where $I$ is the 3x3 identity matrix, $O$ is the zero matrix, and $$A = \begin{pmatrix} -4 & 1 & 0 \\ 1 &-4 & 1 \\ 0 & 1 &-4 \end{pmatrix} $$
In general, the matrix contains $(n-1)\times(n-1)$ blocks (most of the blocks are zero), each block is an $(n-1)\times(n-1)$ matrix: $$ \frac{1}{h^2} \begin{pmatrix} A & I & O & \cdots & O & O\\ I & A & I & \cdots & O & O\\ O & I & A & \cdots & O & O\\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\ O & O & O & \cdots & A & I \\ O & O & O & \cdots & I & A \\ \end{pmatrix} , $$ And the $(n-1)\times (n-1)$ matrix $A$ has a tridiagonal structure with $-4$ on the diagonal and $1$ above and below the diagonal.
TODO : write a python code that solves and plots the solution for $f\equiv 1$. Would be good to use the Kronecker product
In [7]:
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
import matplotlib.cm as cm
%matplotlib inline
#Problem size:
N = 15
# Making A without Kronecker product:
def lap2d(N):
import pyamg
return sp.sparse.csr_matrix(pyamg.gallery.laplacian.poisson((N,N)))
A = lap2d(N)
# Making A with kronecker product
# Making 1D operator:
B = np.diag(2*np.ones(N)) + np.diag((-1)*np.ones(N-1),k=-1)+ np.diag((-1)*np.ones(N-1),k = 1)
Id = np.diag(np.ones(N));
# Making 2D operator:
A = np.kron(Id,B) + np.kron(B,Id)
# Fig. of A
plt.spy(A,markersize=20/N)
# Right hand side
f = np.ones(N**2)
# Solution
x = sp.sparse.linalg.spsolve(A,f)
Sq_x = x.reshape((N, N))
#print Sq_x
#Fig. of solution
fig, ax = plt.subplots()
im = ax.imshow(Sq_x, cmap=cm.RdBu, vmin=abs(Sq_x).min(), vmax=abs(Sq_x).max(), extent=[0, 1, 0, 1])
im.set_interpolation('bilinear')
cb = fig.colorbar(im, ax=ax)
In [ ]:
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()
In [ ]: