Info

Using the intermediate values in a search algorithm.

1 view (last 30 days)
Gleb Gertsman on 21 Sep 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
I was wondering whether there is a way to store the value of the x's inside the objective function to be used in the next iteration. I am using fmincon to minimize some function but I want it to do the following:
- Start from x0, calculate the objective function using x0, calculate the gradient, produce x1.
- Recalculate the objective function using x1 and x0 in the process, calculate the gradient, produce x2.
- Recalculate the objective function using x2 and x1 in the process ....
To be more precise, I need one of the intermediate calculations in each step to be used in the next step, but if it's not possible, then I can also reproduce it given that the value of x's from the previous iteration are kept and feeded back into the objective function.
Is there any "smart" way of doing that without rewriting all the optimization procedures?
Thanks

David Ding on 27 Sep 2017
Hi Gleb,
One crude way of storing the values of the inputs inside a function without feeding back the value into the function is to store the value inside the base workspace. That way, when the function returns, the intermediate values are still present and accessible in the base workspace. If you wish to do this, you may use the " assignin " function. For example:
function y = multAdd(x, a, b)
% returns y = ax + b
u = a*x;
assignin('base', 'u', u); % stores the intermediate result u in the base workspace
y = u + b;
end
Thanks,
David