ENGGEN 131 Chap.6 Function Handles, ODEs and Efficiency
Function Handles, ODEs and Efficiency
This chapter shows how function handles carry callable behaviour into root finders, minimisers and ordinary differential equation solvers. It matches solver signatures, reads adaptive output arrays, reasons from derivative sign and tests equilibrium behaviour. Efficiency claims are treated as evidence problems that require equivalent outputs, controlled workloads, repeated timing and scaling checks.
What this chapter covers
- 01
Named and anonymous function handles
- 02
Solver input signatures and output shapes
- 03
Derivative sign and qualitative trajectories
- 04
Adaptive time points and paired state arrays
- 05
Correctness checks before timing comparisons
Worked example · free
Reason about an approach to equilibrium
- 1At y=2, the derivative is 4.
- 1At y=8, the derivative is 1.
- 1The state rises toward ten while the positive slope becomes smaller.
Key terms
- Function handle
- A MATLAB value referring to callable behaviour.
- Equilibrium
- A state where the derivative is zero and the model stops changing locally.
- Preallocation
- Reserving array storage before a loop fills its elements.
Function Handles, ODEs and Efficiency FAQ
Why are solver time points uneven?
Adaptive solvers choose step sizes based on numerical needs, so interpret state values against the returned time array rather than an assumed fixed interval. Rehearse this distinction with a fresh model and solver-contract trace before relying on program output.
How do I check an ODE plot?
Evaluate derivative signs at the initial state, around any equilibrium and on both sides of a target to predict direction and flattening. Test the claim on a boundary case where the model and solver-contract assumptions become visible.
What makes an efficiency comparison fair?
Use the same input and workload, verify equivalent outputs, time the intended operation several times and examine behaviour as input size increases. Write the expected model and solver-contract state first, then compare it carefully with execution.
Exam move
Begin by writing the mathematical model independently of MATLAB. Name the independent variable, state variables, parameters, initial conditions and units. Check that both sides of every differential equation have compatible dimensions. Identify equilibrium states by setting the derivative to zero. Evaluate the derivative below, at and above each equilibrium so its sign predicts local movement.
This qualitative phase-line sketch becomes a solver-independent reference and can expose a reversed sign or misplaced parameter before numerical output is trusted. Translate the model into a function-handle contract. Record argument order, expected state shape and returned derivative shape. If arrays may be supplied, decide which operators must be elementwise.
Evaluate the handle manually at the initial state and another convenient state, then call it directly with those values before passing it to a solver. A concise anonymous function can still capture the wrong workspace parameter, swap state and time, or return a shape incompatible with the solver. Before reading a plot, inspect the first returned time and state to verify the initial condition.
Compare early movement with the initial derivative, locate whether the curve approaches or moves away from predicted equilibria and check whether flattening corresponds to a decreasing derivative magnitude. Use units and rough timescale to judge plausibility. A smooth curve demonstrates only that points were plotted; it does not establish that the intended model, sign, parameters or solver arguments were used.
When comparing numerical implementations, establish equivalence first. Choose identical inputs and a stated accuracy criterion, then calculate an exact equality result or a maximum discrepancy as appropriate. Keep solver tolerances or evaluation points comparable. Separate setup, file reading and plotting from the code region whose efficiency is being studied. Suppress display and perform a warm-up when relevant.
A faster result outside the permitted error tolerance is a different computation, not an optimisation. Gather timing evidence over repeated runs and several problem sizes. Report the median or another robust summary rather than selecting the fastest observation. Record the hardware-independent workload, including input size and number of evaluations, so the claim can be interpreted.
Look for scaling trends rather than overinterpreting a tiny difference at one size. Fixed overhead may dominate small problems, while allocation or repeated growth may dominate large ones. State uncertainty when timing differences are comparable to run-to-run variation. Finish revision by auditing one complete model: units, equilibrium, derivative signs, handle evaluation, initial condition, trajectory and numerical agreement.
Then design a fair timing comparison between two equivalent ways to compute one stage. Keep a log separating modelling defects, interface defects, numerical discrepancies and measurement flaws. In an exam response, present reasoning in that order. Mastery means predicting the qualitative answer before integration and supporting an efficiency claim with correctness, controlled workload, repetition and scaling evidence.
For synthesis, choose a one-state model with a known equilibrium and write its units, initial condition and expected direction on either side of equilibrium. Evaluate the function handle at three states, then inspect the first solver output and compare the curve with those sign predictions. Implement an equivalent direct calculation in two ways and define a numerical tolerance before timing either.
Measure the same workload repeatedly at several sizes, excluding plotting and setup, and report agreement plus median time. If the faster version misses the tolerance or uses fewer evaluations, reject the optimisation claim.
This workflow joins model validation, function-handle interfaces, qualitative ODE reasoning, numerical comparison and performance evidence, ensuring that a smooth plot or isolated timing cannot substitute for a correct and controlled argument.