Before this lesson:lesson 02 (the directional
derivative), lesson 03 (critical points and second-order
conditions), lesson 10 (integration by parts).
For section 3 it helps to know positive definite matrices (linear algebra lesson 07)
and condition numbers (linear algebra lesson 13).
Time: about 80 minutes, plus the notebook.
Many laws of physics say that some integral is as small as possible, or at least stationary. An
elastic body at rest minimizes its potential energy. The Poisson equation is the condition for the
minimum of the Dirichlet energy. Newton's law and the wave equation make an "action" stationary
(Hamilton's principle). The calculus of variations gives the derivative of a number J[u] with
respect to a whole function u, and the rule that turns "J is stationary" into a differential
equation.
For ML for science this gives two kinds of physics losses for a network uθ:
an energy (Ritz) loss minimizes the integral itself (the Deep Ritz method);
a residual (PINN) loss penalizes the squared residual of the differential equation
(physics-informed neural networks).
They need different derivatives, treat boundary conditions differently and fail in different
ways. The same "gradient of a functional" also gives maximum-entropy distributions, the mean-field
update of variational inference, gradient flows, and the adjoint equations of weather data
assimilation (4D-Var) and seismic full-waveform inversion.
A functional maps a function to a number. Two examples on [a,b]:
the length of the graph of u:J[u]=∫ab1+u′(x)2dx;
the Dirichlet energy with a load f:J[u]=∫ab(21u′(x)2−f(x)u(x))dx.
For an elastic string, 21u′2 is the stored energy of the stretch and −fu is the work of the load.
The general form is J[u]=∫abL(x,u,u′)dx. The function L(x,u,p) is the Lagrangian;
p stands for the slope u′.
A functional on a grid is a function of many variables #
Put u on a grid to see J as an ordinary function. Take xi=a+ih for i=0,…,N, with
h=(b−a)/N, and values Ui≈u(xi). For the Dirichlet energy a simple discretization is
The last step uses the second difference (lesson 07):
(Ui+1−2Ui+Ui−1)/h2≈u′′(xi). Three things to see here.
The factor h.Ui controls a piece of length h of the integral, so its partial derivative
is h times a density. Divided by h, the gradient becomes a function of x: the functional
derivativeδJ/δu=−u′′−f.
Where d/dx comes from.Ui sits in two neighbouring slopes, so the gradient contains the
difference of ∂L/∂p=p across the cell. That difference, divided by h, is
−dxd∂L/∂p. This is integration by parts on a grid.
The equation. Setting the gradient to zero gives −u′′=f, the Poisson equation, with the
fixed boundary values. The minimum of a functional is the solution of a differential equation.
Interactive figure: loading…
Change N in both views. For J[u]=∫01(21u′2−u)dx and a fixed function u, the
raw gradient∂Jh/∂Ui gets smaller as the grid gets finer. Switch to
"(1/h)∂Jh/∂Ui": the dots now lie on the curve δJ/δu=−u′′−1
(black). For the minimizer u=x(1−x)/2 the gradient is zero at every grid point.
In the continuous version, we perturb u in a directionη. The direction is itself a
function, and it must be zero where u is fixed (here at both ends), so that u+ϵη still has
the right boundary values. Then φ(ϵ)=J[u+ϵη] is an ordinary function of one number
ϵ. If u is a minimizer, φ has its minimum at ϵ=0, so φ′(0)=0. This is the
directional derivative ∇f(x)⊤v of lesson 02, with a
function η in place of the vector v.
Interactive figure: loading…
Drag the violet handle. It moves a smooth bump η (dashed) along the curve and changes ϵ.
The functional is J[u]=∫01u′2dx with u(0)=0 and u(1)=1. The chart shows
φ(ϵ)=J[u+ϵη], a parabola, and its tangent at ϵ=0. For
u=x2 the slope at 0 is not zero, so u=x2 is not a minimizer. Switch to u=x: the slope is
0 for every bump you try.
Quick check
In the figure, J[u]=∫01u′2dx with u(0)=0,u(1)=1. For which curve is the slope of φ(ϵ)=J[u+ϵη] at ϵ=0 zero for every bump η?
Why.
The slope is 2∫u′η′dx=−2∫u′′ηdx (integration by parts; η is zero at the ends). It is zero for every η exactly when u′′=0, that is, for the straight line u=x. For u=x2,u′′=2, and a positive bump gives the slope −4∫ηdx<0.
Quick check
On a grid with spacing h, how is the partial derivative ∂Jh/∂Ui of the discretized functional related to the functional derivative δJ/δu?
Why.
Ui controls a piece of length h of the integral, so its partial derivative is h times the density δJ/δu. In the example, ∂Jh/∂Ui=−h[(Ui+1−2Ui+Ui−1)/h2+fi]≈h(−u′′−f). Forgetting this h is a common error when you compare gradients on different grids.
Setup.L(x,u,p) is a twice continuously differentiable (C2) function of three real
variables. The functional is J[u]=∫abL(x,u(x),u′(x))dx. The admissible functions are
u∈C2[a,b] with u(a)=α and u(b)=β. We write Lu=∂L/∂u,Lp=∂L/∂p,Lpp=∂2L/∂p2, and so on, always evaluated along (x,u(x),u′(x)).
First variation. For η∈C2[a,b] with η(a)=η(b)=0, the function u+ϵη is
admissible for every ϵ. The first variation is
δJ[u;η]=dϵdJ[u+ϵη]ϵ=0=∫ab(Luη+Lpη′)dx.
To get the integral, differentiate under the integral sign and use the chain rule:
dϵdL(x,u+ϵη,u′+ϵη′)=Luη+Lpη′.
Functional derivative. If a function δJ/δu satisfies
δJ[u;η]=∫abδuδJ(x)η(x)dx for every such η, it is the functional derivative
of J at u. It is the gradient of J for the L2 inner product ⟨g,η⟩=∫gηdx, in the
same way as ∇f satisfies Df(x)v=∇f(x)⊤v. On a grid of spacing h:∂Jh/∂Ui≈hδuδJ(xi).
Lemma (fundamental lemma of the calculus of variations). Let g be continuous on [a,b], and
let ∫abgηdx=0 for every smooth η that is zero outside some closed interval inside (a,b).
Then g(x)=0 for every x∈[a,b].
The idea: if g were positive somewhere, a bump η placed exactly there would make ∫gη>0.
Proof of the fundamental lemma
Suppose g(x0)=0 for some x0∈(a,b). Replacing g by −g if needed, g(x0)>0.
By continuity, g>0 on a closed interval [x0−δ,x0+δ]⊂(a,b) for some δ>0.
Take the smooth bump η(x)=exp(−1/(δ2−(x−x0)2)) inside this interval and η=0
outside. It is smooth, positive inside and zero outside.
Then gη>0 inside the interval and gη=0 outside, so ∫gηdx>0. This contradicts the
assumption.
So g=0 on (a,b), and by continuity also at a and b.■
Theorem (Euler–Lagrange equation). Let L∈C2, and let the admissible u∈C2 be a local
minimizer of J:J[u]≤J[v] for every admissible v with max∣v−u∣+max∣v′−u′∣ small
enough. Then
Lu−dxdLp=0on [a,b].
The proof uses integration by parts (lesson 10) in
this form:
∫abLpη′dx=[Lpη]ab−∫ab(dxdLp)ηdx.
Proof.
Fix η∈C2 with η(a)=η(b)=0. For small ∣ϵ∣,u+ϵη is admissible and close to
u, so φ(ϵ)=J[u+ϵη] has a local minimum at ϵ=0. Hence φ′(0)=0.
φ′(0)=∫ab(Luη+Lpη′)dx. (Differentiation under the integral is allowed: L is C2
and [a,b] is a closed bounded interval.)
Integrate the second term by parts (the formula above). The boundary term [Lpη]ab is 0
because η(a)=η(b)=0.(dxdLp is continuous because L∈C2 and u∈C2.)
So ∫ab(Lu−dxdLp)ηdx=0 for every such η.
g=Lu−dxdLp is continuous, so the fundamental lemma gives g=0.■
Step 4 also shows that
δuδJ=Lu−dxdLp.
The derivative dxd is a total derivative along the curve: Lp depends on x directly and
through u(x) and u′(x). By the chain rule,
dxdLp(x,u,u′)=Lpx+Lpuu′+Lppu′′.
So the Euler–Lagrange equation Lu−Lpx−Lpuu′−Lppu′′=0 is a second-order ODE when
Lpp=0. It comes with two boundary conditions. Example: for L=21xu′2 we have Lp=xu′,
so dxdLp=u′+xu′′, not only xu′′.
Now fix only u(a)=α and leave u(b) free. Then the directions η must have η(a)=0, but
η(b) can be anything. Step 3 of the proof now keeps one boundary term, at x=b:
0=Lp(b,u(b),u′(b))η(b)+∫ab(Lu−dxdLp)ηdx.
First take all η with η(b)=0. As before, the Euler–Lagrange equation holds, so the
integral is 0 for every η.
Then take an η with η(b)=1. The equation above becomes Lp(b,u(b),u′(b))=0.
This is the natural boundary condition. You do not impose it; the minimization produces it.
For L=21u′2+… it is u′(b)=0, a Neumann condition. Prescribed values such as
u(a)=α are called essential (Dirichlet) conditions.
Quick check
Minimize J[u]=∫01(21u′2−fu)dx with u(0)=0 fixed and u(1) free. Which condition holds at x=1?
Why.
The natural boundary condition is Lp=0 at the free end, and here Lp=u′. So u′(1)=0. It is not imposed; it comes out of the minimization, because the directions η may have η(1)=0.
The Euler–Lagrange equation is a necessary condition: maxima and saddle points of J satisfy it
too. A sufficient condition:
Theorem. If L(x,u,p) is convex in (u,p) for every x, then every admissible solution of the
Euler–Lagrange equation is a global minimizer.
Proof. Let v be admissible and η=v−u, so η(a)=η(b)=0. Convexity says that L lies
above its tangent plane: L(x,v,v′)≥L(x,u,u′)+Luη+Lpη′ at every x. Integrate:
Without convexity, the second variationφ′′(0) decides, like the Hessian in
lesson 03. A necessary condition for a minimum is Lpp≥0
along u (the Legendre condition).
For u:Ω⊂Rd→R and J[u]=∫ΩL(x,u,∇u)dx, write p=∇u∈Rd and let
∇pL be the vector of partial derivatives of L in p. The same steps work with the divergence
theorem in place of integration by parts:
∫Ω∇pL⋅∇ηdx=∫∂Ωη(∇pL⋅n)dS−∫Ω(∇⋅∇pL)ηdx,
with n the outward unit normal. The Euler–Lagrange equation is
Lu−∇⋅(∇pL)=0in Ω,
and on a part of the boundary ∂Ω where u is free, the natural condition is ∇pL⋅n=0.
Shortest path. Arc length: L=1+p2.Lu=0, so the equation says dxdLp=0,
that is, Lp=u′/1+u′2=C, a constant. The map p↦p/1+p2 is strictly
increasing, so u′ is constant: a straight line. L is convex in p(Lpp=(1+p2)−3/2>0)
and does not depend on u, so the line is the global minimizer.
Dirichlet energy and the Poisson equation.E[u]=∫Ω(21∣∇u∣2−fu)dx with
u=0 on ∂Ω. Here Lu=−f and ∇pL=p=∇u, so the equation is −f−∇⋅∇u=0:
−Δu=f.
L is convex in (u,p), so the solution is the minimizer (Dirichlet's principle). Where no
boundary value is imposed, the natural condition is ∂u/∂n=0. In 1-D: −u′′=f.
Hamilton's principle: stationary, not minimal
In mechanics the integral runs over time. The actionS[q]=∫(21mq˙2−V(q))dt has
Lq=−V′(q) and Lq˙=mq˙, so the Euler–Lagrange equation is Newton's law
mq¨=−V′(q). For a 1-D elastic medium with density ρ and stiffness μ,S[u]=∬(21ρut2−21μux2)dxdt gives the wave equation ρutt=μuxx
(the elastic wave equation of seismology is the 3-D version). This L is not convex in (ut,ux):
the motion makes the action stationary, not minimal.
The Dirichlet condition is either a penalty β∫∂Ωuθ2dS, or it is built into the network,
uθ(x)=d(x)Nθ(x) with a function d that is 0 on ∂Ω. The classical Ritz method uses
uc=∑kckφk with fixed basis functions φk; the finite element method is this method
with piecewise polynomials.
Upper bound. If the trial functions are admissible (essential conditions exact) and the integral
is exact, they form a subset of the admissible set. So minθE[uθ]≥minuE[u]: the Ritz
value approaches the true minimum from above. For the Dirichlet energy the gap is exactly an error:
E[uθ]−E[u∗]=21∫Ω∣∇(uθ−u∗)∣2dx, because δE[u∗;⋅]=0 and E is quadratic.
PINN (strong-form residual) loss. With collocation points xj∈Ω and boundary points
yk∈∂Ω:
PDEs that are Euler–Lagrange equations of a functional (symmetric problems: Poisson, elasticity)
any PDE, also non-variational ones (advection, Navier–Stokes)
Derivatives of uθ
half the order: first derivatives for Poisson
full order: second derivatives for Poisson
Loss at the solution
the minimum energy, not 0
0; the residual is an error indicator
Boundary conditions
essential ones must be imposed; natural ones come free
every condition needs a loss term (or must be built in)
Regularity
weak solutions; kinks and corner singularities are fine
needs a smooth uθ (strong solution)
Conditioning
like A: condition number κ
like A⊤A: condition number κ2
Why the residual loss is worse conditioned. Discretize −u′′ on N=100 interior points to get
the matrix A=tridiag(−1,2,−1)/h2. It is symmetric positive definite.
The energy 21U⊤AU−F⊤U has gradient AU−F and Hessian A, with
κ(A)≈4.1⋅103.
The residual loss 21∥AU−F∥2 has gradient A⊤(AU−F) and Hessian A⊤A. Its eigenvalues
are the squares of those of A, so κ(A⊤A)=κ(A)2≈1.7⋅107.
Plain gradient descent needs a number of steps proportional to κ
(linear algebra lesson 14 shows how CG reduces this to κ).
Both losses have the same minimizer U∗=A−1F. Only the speed differs. For neural networks the
picture is less simple, but Krishnapriyan et al. (see "Watch and read") show that the differential
operator in the loss makes PINN training hard.
Interactive figure: loading…
Move k to the right. Plain gradient descent on the energy loss and on the
residual loss of the same discrete Poisson problem, each with the step 1/λmax of its
Hessian. Both start at U=0 and have the same solution (dashed). The right chart shows the error
against the number of steps. Change N:κ(A) grows like N2 and κ(A⊤A) like N4.
Two more facts. A linear problem Au=f is the Euler–Lagrange equation of
21⟨u,Au⟩−⟨f,u⟩ only if A is symmetric (self-adjoint) in L2; an
advection term b⋅∇u is not, so such problems have no energy of this form. And a ReLU network
is piecewise linear, so Δuθ=0 almost everywhere: a PINN for a second-order PDE needs smooth
activations (tanh, SiLU, sine). Deep Ritz needs only first derivatives, but E and Yu still use a
smoother activation, max(x3,0), for accuracy.
Quick check
You train a network uθ for −u′′=f on (0,1) with u(0)=0 and the free-end condition u′(1)=0. Which statement is right?
Why.
The energy ∫(21u′2−fu)dx contains only u′, and its minimization produces u′(1)=0 by itself (natural condition). Only the essential condition u(0)=0 must be imposed. The PINN residual −uθ′′−f needs second derivatives, and every boundary condition needs its own term. A loss value of 0 at the solution says nothing about conditioning; the PINN loss has condition number about κ2.
Since ∂Jh/∂Ui≈hδJ/δu(xi), a gradient-descent step U←U−τ∇Jh is
Ui←Ui−τhδuδJ(xi). This is an explicit Euler step of length τh for the
gradient flow
ut=−δuδJ.
For the Dirichlet energy δJ/δu=−u′′−f, so this flow is the heat equationut=u′′+f.
Gradient descent smooths u the way heat spreads. Explicit Euler for the heat equation is stable
only for time steps up to h2/2, so τh≤h2/2, that is τ≤h/2. The number of steps to
reach a fixed time grows like 1/h2. This is the conditioning problem again: plain gradient
descent on a fine grid is slow.
Interactive figure: loading…
Press Play. Each step is one gradient-descent step on the discretized Dirichlet energy, with
step size τ=r⋅h/2. The curve becomes smooth: the fast wiggles
disappear first, the slow bump last. Now set r slightly above 1, press Reset and Play: the
fastest mode grows at every step. This is the stability limit τ=h/2.
Answers to section 1.
(1) L=p2 gives Lu=0 and Lp=2u′, so the equation is 2u′′=0. With the boundary values,
u=x, and the minimum is ∫011dx=1. No other admissible u does better: by the
Cauchy–Schwarz inequality, 1=(∫01u′dx)2≤∫01u′2dx.
(2) Yes: the Dirichlet energy ∫(21u′2−fu)dx. Its Euler–Lagrange equation is −u′′=f,
and it needs only u′. This is the Ritz loss.
Minimize J[u]=∫01(21u′2−u)dx with u(0)=0 and u(1)free.
Step 1: Euler–Lagrange.L=21p2−u, so Lu=−1 and Lp=p. The equation
Lu−dxdLp=−1−u′′=0 gives u′′=−1. This is the Poisson equation −u′′=f with f=1.
Step 2: boundary conditions. Essential: u(0)=0. Natural, at the free end: Lp=u′(1)=0.
Step 3: solve. Integrate twice: u=−x2/2+Ax+B. From u(0)=0:B=0. From
u′(1)=−1+A=0:A=1. So u∗(x)=x−x2/2.
Step 4: value.u∗′=1−x. The two parts are ∫0121(1−x)2dx=61 and
∫01(x−x2/2)dx=21−61=31. So J[u∗]=61−31=−61.L is convex in (u,p),
so this is the global minimum.
Step 5: Ritz with one trial function, u=cx. It satisfies u(0)=0, but not u′(1)=0.J(c)=∫01(21c2−cx)dx=c2/2−c/2. The minimum is at c=21, with
J=81−41=−81=−0.125. This is above −61≈−0.1667, as the upper bound says. The
slope at x=1 is 21, not 0: the natural condition holds only approximately.
Step 6: Ritz with two trial functions, u=c1x+c2x2. Then u′=c1+2c2x and
J(c1,c2)=21c12+c1c2+32c22−21c1−31c2.
Setting both partial derivatives to zero gives c1+c2=21 and c1+34c2=31.
Subtract: 31c2=−61, so c2=−21 and c1=1. This is exactly u∗, with J=−61,
and u′(1)=0 holds although nobody imposed it. The trial space contains the solution, so Ritz
finds it.
Step 7: the PINN loss for the same problem is
M1j∑(uθ′′(xj)+1)2+λ0uθ(0)2+λ1uθ′(1)2.
The condition u′(1)=0 needs its own term and its own weight λ1. The Ritz loss contains it
already; only u(0)=0 must be imposed, for example with uθ(x)=xNθ(x).
Quick check
In step 5, Ritz with u=cx gives J=−0.125, while the true minimum is −1/6≈−0.1667. What does the gap −0.125−(−1/6)≈0.042 measure?
Why.
For the Dirichlet energy, E[u]−E[u∗]=21∫(u′−u∗′)2dx for every admissible u. Here 21∫01(21−(1−x))2dx=21∫01(x−21)2dx=241≈0.042. The trial function cx satisfies u(0)=0 exactly, so the Ritz value is always above the minimum.
NumPy and SciPy check the worked example: an Euler–Lagrange residual by finite differences (with the
total derivative), the Ritz coefficients, the natural boundary condition on a grid, and the two
condition numbers.
importnumpyasnpfromscipyimportintegrate# (a) Euler-Lagrange residual L_u - d/dx L_p, with the total derivative d/dx by central differencesdefel_residual(Lu,Lp,u,du,x,h=1e-4):dLp=(Lp(x+h,u(x+h),du(x+h))-Lp(x-h,u(x-h),du(x-h)))/(2*h)returnLu(x,u(x),du(x))-dLpLu=lambdax,u,p:0.0*x# L = x p^2 / 2: L_u = 0, L_p = x pLp=lambdax,u,p:x*pxs=np.array([1.5,2.0,2.5])print(el_residual(Lu,Lp,np.log,lambdax:1/x,xs).round(6))# [0. 0. 0.]: u = log x solves itprint(el_residual(Lu,Lp,lambdax:x,lambdax:1+0*x,xs).round(6))# [-1. -1. -1.]: u = x does not# (b) worked example: J[u] = int_0^1 (u'^2/2 - u) dx, u(0) = 0, u(1) freeJ=lambdau,du:integrate.quad(lambdax:0.5*du(x)**2-u(x),0,1)[0]print(round(J(lambdax:x-x**2/2,lambdax:1-x),6))# -0.166667# Ritz with phi_1 = x, phi_2 = x^2: J(c) = c^T K c / 2 - b^T c, K_jk = int phi_j' phi_k', b_j = int phi_jphi=[lambdax:x,lambdax:x**2]dphi=[lambdax:1.0,lambdax:2*x]K=np.array([[integrate.quad(lambdax:dphi[j](x)*dphi[k](x),0,1)[0]forkinrange(2)]forjinrange(2)])b=np.array([integrate.quad(phi[j],0,1)[0]forjinrange(2)])c1=b[0]/K[0,0]print(c1,round(-0.5*c1*b[0],6))# 0.5 -0.125 (one trial function)c=np.linalg.solve(K,b)print(c,round(-0.5*c@b,6))# [ 1. -0.5] -0.166667 (two: exactly u*)# (c) discrete energy with a free right end: unknowns U_1..U_N, U_0 = 0, trapezoid weights for f = 1N=100;h=1/NA=(np.diag(np.full(N,2.0))-np.eye(N,k=1)-np.eye(N,k=-1))/hA[-1,-1]=1/h# the last unknown sits in one cell onlyF=np.full(N,h);F[-1]=h/2U=np.linalg.solve(A,F)# gradient of J_h = 0x=np.arange(1,N+1)*hprint(np.abs(U-(x-x**2/2)).max().round(6),round((U[-1]-U[-2])/h,4))# 0.0 0.005 (= h/2 -> 0)# (d) conditioning: energy loss (Hessian A) vs residual loss (Hessian A^T A), N = 100n=100;hh=1/(n+1)A1=(2*np.eye(n)-np.eye(n,k=1)-np.eye(n,k=-1))/hh**2print(f"{np.linalg.cond(A1):.0f}{np.linalg.cond(A1.T@A1):.3g}")# 4134 1.71e+07
In (a), the term −u′ in −dxd(xu′)=−u′−xu′′ is the Lpx part of the total derivative.
A code that uses only Lppu′′ would report a wrong residual. In (c) nobody imposed u′(1)=0;
the discrete slope at the free end is h/2, which goes to 0 with h.
Physics-informed ML. PINNs, Deep Ritz, energy-based losses for elasticity, and weak-form
("variational") PINNs. Weak-form PINNs test the residual against a finite set of functions η
after an integration by parts; for a problem with an energy, this is the first variation set to
zero. Finite elements use the same weak form with piecewise polynomials. See the
ML for science track.
Seismic and weather models. The elastic wave equation follows from Hamilton's principle.
Full-waveform inversion and 4D-Var minimize a misfit functional over an Earth model or an initial
state. Their adjoint equations come from the first variation of a Lagrangian with a multiplier.
Regularization of inverse problems and images. Tikhonov smoothing 21∫∣∇m∣2 adds −Δm
to the Euler–Lagrange equation and smooths everything. Total variation ∫∣∇m∣ adds
−∇⋅(∇m/∣∇m∣) and keeps sharp edges (faults, fronts).
Probability and information theory. Maximum-entropy distributions (the Gaussian for a given
mean and variance) come from setting the functional derivative of the entropy −∫plogp, plus
Lagrange multipliers for the constraints, to zero. The mean-field variational inference update
qj∝exp(E−j[logp(x,z)]) comes from the functional derivative of the ELBO in qj.
Gradient flows. Besides the heat equation (section 3), the Fokker–Planck equation of Langevin
dynamics is the gradient flow of the KL divergence in the Wasserstein metric (not derived here).
A partial instead of a total derivative.dxdLp includes Lpx,Lpuu′ and Lppu′′.
For L=21xu′2:dxd(xu′)=u′+xu′′, not xu′′.
Wrong variations.η must be zero exactly where u is prescribed. Imposing both u(b)=β and the
natural condition Lp(b)=0 asks for too much: the problem usually has no solution.
"Euler–Lagrange means minimum." It is necessary only. Check convexity, or the second
variation. Actions in mechanics are stationary, not minimal.
Reading the Ritz loss as an error. Its optimum is the minimum energy (−61 in the worked
example), not 0. Measure the error against a reference solution, or look at the residual.
Boundary conditions in Ritz. Essential conditions must be imposed. A penalty weight that is too
small leaves them violated; one that is too large makes the loss ill-conditioned. Natural
conditions need no term.
Fixed quadrature points. The network can make the integrand small at those points only.
Resample the points at every step (as in Deep Ritz) or use enough of them.
ReLU networks in a PINN for a second-order PDE.Δuθ=0 almost everywhere.
Forgetting the 1/h.∂Jh/∂Ui≈hδJ/δu(xi). Comparisons with the functional
derivative, step sizes and convergence tests must use (1/h)∂Jh/∂U.
Give the Euler–Lagrange equation for L=21u′2+21k2u2−f(x)u.AnswerLu=k2u−f and Lp=u′, so k2u−f−u′′=0, that is, −u′′+k2u=f (a screened Poisson equation).
Minimize J[u]=∫01(u′2+u2)dx with u(0)=0 and u(1)=1. Give the minimizer and J,
and compare with u=x.AnswerLu=2u and Lp=2u′, so 2u−2u′′=0, that is, u′′=u. With the boundary values, u=sinhx/sinh1. Integration by parts with u′′=u gives J=∫(u′2+uu′′)dx=[uu′]01=u(1)u′(1)=cosh1/sinh1=coth1≈1.3130. For u=x:J=1+31≈1.3333, which is larger, as it must be (L is convex).
L=21xu′2 on [1,e] with u(1)=0 and u(e)=1. Give the Euler–Lagrange equation and
its solution.AnswerLu=0 and Lp=xu′, so −dxd(xu′)=0, that is, −u′−xu′′=0. Then xu′=C,u=Clogx+D, and the boundary values give u=logx.
J[u]=∫01(21u′2−fu)dx with no boundary conditions at all. Which conditions does
a minimizer satisfy, and what must f satisfy for a minimizer to exist?Answer−u′′=f with the natural conditions u′(0)=u′(1)=0 at both ends. Integrating the equation, ∫01fdx=−[u′]01=0. If ∫f=0, the constants u=c give J=−c∫f, which goes to −∞ for a suitable sign of c, so there is no minimizer. If ∫f=0, the minimizer is unique up to an added constant.
The 1-D Laplacian matrix on a grid has κ(A)≈4.1⋅103. Plain gradient descent on the
energy loss needs about k steps. About how many does it need on the residual loss
21∥AU−F∥2, and why?AnswerAbout κ(A) times more, so roughly 4⋅103k. The residual loss has Hessian A⊤A with κ(A⊤A)=κ(A)2≈1.7⋅107, and the number of gradient-descent steps grows like the condition number.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
What is a functional? Give one example.
A map from a function to a number, for example the Dirichlet energy J[u]=∫ab(21u′2−fu)dx or the arc length ∫ab1+u′2dx.
Define the first variation δJ[u;η].
dϵdJ[u+ϵη]ϵ=0=∫ab(Luη+Lpη′)dx, for directions η that are zero where u is prescribed.
State the fundamental lemma of the calculus of variations.
If g is continuous and ∫abgηdx=0 for every smooth η that is zero near the ends, then g=0 on [a,b].
Write the Euler–Lagrange equation for J[u]=∫abL(x,u,u′)dx.
∂u∂L−dxd∂u′∂L=0, with dxd a total derivative along (x,u(x),u′(x)).
What condition does a minimizer satisfy at a free end x=b?
The natural boundary condition Lp(b,u(b),u′(b))=0; for L=21u′2+… this is u′(b)=0.
Which PDE is the Euler–Lagrange equation of the Dirichlet energy ∫Ω(21∣∇u∣2−fu)dx?
The Poisson equation −Δu=f (in 1-D: −u′′=f).
On a grid with spacing h, how is ∂Jh/∂Ui related to δJ/δu?
∂Jh/∂Ui≈hδuδJ(xi): each value Ui controls a piece of length h of the integral.
For −u′′=f, which derivatives of the network does a Ritz loss need, and which does a PINN loss need?
Ritz (energy) needs only uθ′; PINN (residual) needs uθ′′.
Why is the residual loss 21∥AU−F∥2 worse conditioned than the energy 21U⊤AU−F⊤U?
Its Hessian is A⊤A, with κ(A⊤A)=κ(A)2; the energy has Hessian A, with κ(A).
Gradient descent on the discretized Dirichlet energy is a time step of which equation?
The heat equation ut=u′′+f, the gradient flow ut=−δJ/δu; the explicit step is stable only for τ≤h/2.
"Derive the Euler–Lagrange equation." Take the first variation, integrate by parts with
η(a)=η(b)=0, and apply the fundamental lemma. Add the natural boundary condition at a free end.
"Prove that the shortest curve between two points is a straight line." Lp=u′/1+u′2 is
constant, so u′ is constant; convexity of L makes the line the global minimum.
"You must solve a Poisson equation with a neural network. Which loss do you use?" Ritz: first
derivatives only, Neumann conditions for free, but it needs an energy and imposed Dirichlet
conditions. PINN: any PDE, but second derivatives, a term for every condition, and worse
conditioning.
"Why do PINNs often fail to train?" The differential operator in the loss makes it ill-conditioned,
the loss terms need weights, and networks fit low frequencies first. Remedies include curriculum
training, causal (time-ordered) weighting, and boundary conditions built into the network.
"What is the functional derivative of ∫plogpdx? Find the maximum-entropy density with a
given mean and variance." logp+1. With Lagrange multipliers for ∫p=1, the mean and the
variance, logp is a quadratic in x: the density is a Gaussian.
The notebook for this lesson runs in Colab. It has four exercises, each with a check cell
and a folded solution:
Write euler_lagrange_residual with a finite-difference total derivative and use it to test
candidate solutions, including L=21xu′2.
Write discrete_functional_gradient for the Dirichlet energy on a grid and compare
(1/h)∂Jh/∂U with δJ/δu for several N.
Solve the 1-D Poisson problem by minimizing the discrete energy and the discrete residual, with
gradient descent and with conjugate gradients, and count the iterations.
Ritz with polynomial trial functions x,x2,…,xK for a problem with a free end: watch the
natural condition u′(1)=0 appear without being imposed.
Functionals, the variation u+ϵη, and a step-by-step derivation of the Euler–Lagrange equation with integration by parts.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
A visual introduction: functionals, variations as small bumps, and the shortest path between two points.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The Ritz method on a beam: pick trial functions, write the potential energy, and minimize over the coefficients.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
How a PINN puts the PDE residual and the boundary conditions into the loss, and where it struggles.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Peter Olver, The Calculus of Variations, free
lecture notes: §3 (the Euler–Lagrange equation; the fundamental lemma is Lemmas 3.3–3.4), §4
(boundary conditions, including natural boundary conditions), §5 (the second variation), §6
(several variables; Example 6.1 is the Dirichlet integral).
E and Yu, The Deep Ritz Method: §2 (trial functions and Monte
Carlo quadrature inside SGD) and §3.1 (the Poisson equation with a boundary penalty).