Advanced Octave A24 — Derivatives and multivariate sensitivity
Advanced GNU Octave with GX-TXT · Module 04
A jacobiana collects the partial derivatives of the outputs compared to the inputs. It is a useful tool to study the local sensitivity of a model and to propagate small variations or uncertainties under appropriate hypotheses.
Objectives and prerequisites
After the functions and numerical calculation of the introductory course and the callbacks of the A23 lesson, we calculate a numerical jacobian through the Bridge and compare it with an analytical formula. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
Consider the synthetic mathematical model f(x,y) = [x²+y; x*y] in (1,2). The analytical jacobian is [2*x, 1; y, x], then at the chosen point is [2,1; 2,1].
Record the model and compare
Import jacobiana_callback.m, record it in the Session and run it in the Runner.
h = gxtxt_callback_register(@(z) [z(1)^2+z(2); z(1)*z(2)]);
unwind_protect
r = gxtxt_api_call('NUMERIC_API_v1','foundation', ...
{'calculus','jacobian',h,[1;2],[]});
J = r.value_real;
expected = [2,1;2,1];
fprintf('Numerical Jacobian:\n'); disp(J);
fprintf('Maximum error against analytical Jacobian: %.3e\n',max(abs(J(:)-expected(:))));
unwind_protect_cleanup
gxtxt_callback_release(h);
end_unwind_protect
The callback receives the input carrier and returns the vector of the two outputs. The foundation operation of the calculus family calculates the numerical jacobiana; each row corresponds to an output and each column to an input. The cleaning section releases the callback.
In the test, the printed matrix is [2,1; 2,1] and the maximum difference from the analytical formula is zero to the accuracy displayed. This result concerns the function, point and method used: it does not guarantee null error for other models.
Sensitivity and limits
A numerical derivative depends on the scale of the variables and the passage of finite differences; too large steps can introduce approximation error, while too small steps can amplify the rounding. For a physical model it assigns units to inputs and outputs: each element of the jacobiana has the units of the output divided for those of the entrance.
The jacobiana describes the local behavior near the chosen point. For wide variations or non-linear models, check the model directly away from that point and do not automatically attribute global validity to linear approximation.
Exercise
- What derivative is the element in the first row and second column?
- What controls the comparison with
expected? - Why can a jacobiana at one point not describe very large variations well?
Worked solution
- The derivative of the first exit compared to the second entrance, here equal to one.
- The numerical difference from the analytical jacobian known at that point.
- Because it is a local approximation and the model can be non-linear.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories