{"cells": [
{"cell_type": "markdown",
"source": [
"<center>\n",
"<h1>Teaching with Jupyter\n",
...
Text Cells are Markdown:
def with_syntax_highlighting(a=5):
return "cool"
...and $\LaTeX$ for mathematics via MathJax:
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
import time
for i in range(3):
print(i)
0 1 2
x = np.linspace(0, 10)
from scipy.special import jn
for n in range(5):
plt.plot(x, jn(n, x))
for n in range(3):
plt.plot(x, jn(n, x))
We want to solve the Poisson equation:
\begin{align} - \nabla^{2} u &= f \quad {\rm in} \ \Omega, \\ u &= 0 \quad {\rm on} \ \Gamma_{D}, \\ \nabla u \cdot n &= g \quad {\rm on} \ \Gamma_{N}. \end{align}First, we define our domain, $\Omega$, a unit square, and a function space:
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
mesh
Our boundary conditions:
def boundary(x):
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS
u0 = Constant(0.0)
bc = DirichletBC(V, u0, boundary)
and our source terms, $f$ and $g$:
\begin{align} f &= 10 e^\left( - \frac { (x - 0.5)^2 + (y - 0.5) ^ 2 } { 0.02 } \right) \\ g &= \sin(5 x) \end{align}with sys_pipes():
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
g = Expression("sin(5*x[0]) + %g" % eps, degree=4)
Calling DOLFIN just-in-time (JIT) compiler, this may take some time.
Which we can plug into the Poisson equation, where:
$$ a(u, v) = L(v) \quad \forall \ v \in V $$and
\begin{align} a(u, v) &= \int_{\Omega} \nabla u \cdot \nabla v \, {\rm d} x, \\ L(v) &= \int_{\Omega} f v \, {\rm d} x + \int_{\Gamma_{N}} g v \, {\rm d} s. \end{align}u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v))*dx
L = f*v*dx + g*v*ds
And finally compute our solution:
with sys_pipes():
u = Function(V)
solve(a == L, u, bc)
plot(u);
Solving linear variational problem.
help students explore principles on their own:
from ipywidgets import interact
from sympy import Symbol, Eq, factor
@interact
def factorit(n=5):
x = Symbol('x')
return Eq(x**n-1, factor(x**n-1))
...In Cartesian coordinates, the stream function is given by:
$$\psi\left(x,y\right) = \frac{\Gamma}{4\pi}\ln\left(x^2+y^2\right)$$interact(plot_cylinder_lift, gamma=(0, 10));
Interpolation is a method of constructing new data points within the range of a discrete set of known data points.
def interpolate(x, x0, x1, y0, y1):
"""return an interpolated value for y(x)
based on:
- y(x0) = y0
- y(x1) = y1
"""
def test_interpolate():
f = math.sin
x0, x1 = 1, 2
y0, y1 = f(x0), f(x1)
y = interpolate(x0, x0, x1, y0, y1)
assert y == x0
'...'
%%html
<center><iframe src="https://demohub.jupyter.org" width=800 height=600/></center>