I have a MATLAB code (see below) that employs 'fsolve' from the optimization toolbox for a root finding problem.The bottleneck is that, within the objective function calculation, there is a computationally expensive function (denoted as 'expensive_function' in the below code, and is located in the matlabpath). The variable 'x_sol' holds solution returned by 'fsolve'.The issue is that, within the main code, I need access to the 'other_results' struct computed by the expensive function for further calculations. It is wasteful to recompute the 'other_results' struct using the solution value (such as by using the following line of code within 'main' )other_results = expensive_function(x_sol,param);Is there a way to get access to this struct without re-evaluating the expensive_function? PS: I have been cautioned throughout my career against using global/persistent variables, and I have thus far stayed clear of them. I don't know if this is the only recourse to solving this issue, or there exists a better/clever way ?%% main codex_sol = fsolve(@(x) compute_residual(x,param) , x0);% code below needs to use various values from the other_results struct computed in the obj_fun  functionline with some computation on 'other_results' resultline with some other computation using 'other_results' struct.......and so on.________________________________________________________function residual = compute_residual(x, param)     other_results = expensive_function(x,param);  % other_results is a struct returned by a very computationally expensive function     residual = abs(other_results.a * other_results.b - x)   % the abs(diff) end
Obtaining extra output argument(s) from the objective function used by fsolve in MATLAB
optimization;matlab;nonlinear equations;iterative method;roots
As I said in my comment, I don't think you can necessarily count on the value ofx in the last call to your function being the value that is returned as the solution.But, that being said, here is a way to do what you want. The solution in the code I show below relies on two components. First you define a MATLAB class that derives from handle. This makes it a so-called handle class so that when it is passed to a function as an argument, it is passed by reference, and therefore its contents can be modified by that function. Second, an anonymous function definition is used to pass an instance of this class into your fsolve function so you can store your solution there. The example code below shows this explicitly.The class definition isclassdef SolutionSaver < handle  properties    x, expensiveResult;  endendAnd the test case that uses this class isfunction solutionSaveTest   ss = SolutionSaver;   func = @(x) calcF(x, ss);   x = fsolve(func, 1);   fprintf('x=%18.10e, ss.x=%18.10e\n', x, ss.x);   ss.expensiveResultendfunction f=calcF(x, ss)  e=expensive_calc(x);  f = e - 5;  ss.x = x;  ss.expensiveResult = e;endfunction e=expensive_calc(x)  e = x^2;endI ran this example in Octave and, in this case, the saved x was equal to the returned x. But, in general, it is probably worth testing that fact before using the saved solution data.