4. Differentiating a solved model
A residual derivative is only the beginning
Section titled “A residual derivative is only the beginning”A source change first changes the balance equation, then the solution must move to restore balance. Differentiating only the equation’s explicit parameter terms misses that second step.
Start with one scalar balance,
At fixed , the partial derivative is . But the solved state is , whose derivative is . The two derivatives answer different questions and can have different units.
For a discrete model with state vector and parameters , write . Differentiating the whole relation gives
For one parameter direction , solve the tangent equation
An invertible state Jacobian makes this local response well defined. The tangent equation is linear even when the original model is nonlinear. This chain-rule construction underlies the tangent and adjoint methods discussed in the dolfin-adjoint project’s mathematical introduction.
The output can depend directly on a parameter
Section titled “The output can depend directly on a parameter”Let the published output be . Its change is
For Poisson finite elements, the state solve can use only free interior unknowns, while the complete output also contains prescribed boundary values. Changing boundary parameter changes those boundary entries directly. The term retains this dependence.
For our constant boundary offset, the complete analytic field rises by one unit everywhere when rises by one unit. The boundary sensitivity is therefore one on the boundary as well as in the interior. A derivative array that silently drops the boundary contribution would answer a different question.
Forward actions and reverse actions
Section titled “Forward actions and reverse actions”Let . A Jacobian-vector product, or JVP, computes : choose a parameter change and ask how the output changes.
A vector-Jacobian product, or VJP, computes : choose output weights and ask how the scalar changes with each parameter. The notation uses real column vectors, so the transpose makes the shapes explicit.
For fixed , let . Instead of forming the full state derivative, solve an adjoint equation and contract with the parameter derivative:
A forward solve follows one input direction across many outputs. An adjoint solve follows one scalar objective across many inputs. In both cases the physical model and observation rule determine the derivative being requested.
Read the existing script
Section titled “Read the existing script”The Poisson run script
uses the public eqiora.diff API:
tangent = program.jvp(direction).tangent.numpy()gradient = program.vjp(weights).input_cotangent.numpy()Its output is the complete Q1 field. The weights integrate that field over the
unit square, with half weights on edges and quarter weights at corners. The
result is an area mean because this square has area one. A simple unweighted
mean of vertex values would be a different objective.
Our continuum solution independently predicts
Here . The script prints this gradient at , , beside the computed gradient. Mesh refinement connects the discrete prediction to the continuum calculation. The numerical integration and differentiation both refer to the chosen discrete objective.
Check the direction independently
Section titled “Check the direction independently”Re-solve at nearby points and form a centred difference:
Compare this with the JVP. For a sufficiently smooth response, the leading truncation error is proportional to . At very small , cancellation and solve errors can dominate. The script prints differences for several step sizes, so you can see the useful range rather than assume the smallest step is best. The dolfin-adjoint gradient-check discussion explains the related Taylor-remainder test.
Also compare the two scalar products and . Agreement checks that the forward and reverse actions pair consistently. A separately evaluated finite difference and the continuum calculation ask additional questions: two mutually consistent derivative routines could still share a mistake.
Exercises
Section titled “Exercises”- Apply the tangent equation to on the positive branch. What happens to its sensitivity as approaches zero?
- Derive the area-mean gradient with respect to by hand and explain its sign.
- Run the script with 12 and 24 cells per direction. Compare changes in the continuum-gradient error with changes in the JVP/VJP pairing difference.
- If the objective is , what output weight vector should the VJP receive at the current point?
- Add a penalty . Which gradient term comes directly from the penalty rather than the solved state?
Check your reasoning: exercise 4 uses ; exercise 5 adds .
Reading
Section titled “Reading”The dolfin-adjoint project, Differentiating functionals, mathematical documentation, sections “The tangent linear approach” and “The adjoint approach.” Derivation. The same project’s Verification, section on Taylor remainder convergence. Gradient checks. These references explain the mathematics; the executable calls above use Eqiora.
Previous: Sensitivity and identifiability · Book map · Next: Noise, scaling, and design