Advanced Octave A23 — Optimization with callbacks
Advanced GNU Octave with GX-TXT · Module 04
A defined objective function in Octave can be evaluated by a public numerical operation of GX-TXT through a callback managed by the Bridge. The reference to the function is limited to the execution of the script.
Objectives and prerequisites
The introductory course has introduced scientific functions and calculation. Here we record an anonymous function, we pass it to a minimiser and we release the reference. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
The mathematical example f(x) = (x−2)²+1 has a minimum known f(2)=1; it does not describe experimental data. We use the [0,5] range to have independent control of the result.
Record and release
Import minimo_callback.m, record it in the Session and run it in the Runner.
% Advanced Octave course A23: optimization with a managed callback.
% SPDX-License-Identifier: GPL-3.0-or-later
gxtxt_api_require({'NUMERIC_API_v1@1'});
h = gxtxt_callback_register(@(x) (x-2).^2 + 1);
unwind_protect
result = gxtxt_api_call('NUMERIC_API_v1','minimize_1d',{h,[0,5]});
fprintf('Optimization result:\n'); disp(result);
unwind_protect_cleanup
gxtxt_callback_release(h);
end_unwind_protect
gxtxt_callback_register returns a matte handle: the script passes it to the provider, which recalls the function during the search. The handle is not a file or a function to serialize. unwind_protect_cleanup ensures that the release is requested even if the call fails.
The output shows x = 2, fval = 1, converged = 1 and six iterations. The values correspond to the minimum known of the parable. Diagnostic fields should however be interpreted according to the contract of the operation and applied tolerance.
Convergence and interpretation
A convergence flag indicates the outcome of the numerical criterion used, it does not prove by itself that a complex function has a single global minimum or that the model represents a real phenomenon. In more minimal problems, compare reasonable intervals or initial conditions, verify the value of the function and motivate the choice of the model.
The callback lives in the context of the single execution: do not retain its handle for a subsequent Job and do not attribute it a scientific meaning independent of the function it represents.
Exercise
- Why is the exact minimum of the test function in
x=2? - Which section releases the callback in case of error?
converged = 1always guarantees a global minimum for any function?
Worked solution
- The square is non-negative and is only worth zero when
x=2. - The
unwind_protect_cleanupsection. - No. It depends on the problem, range and method; the flag describes convergence according to the numerical criterion.
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