Info

此问题已关闭。 请重新打开它进行编辑或回答。

Using the intermediate values in a search algorithm.

1 次查看(过去 30 天)
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

回答(1 个)

David Ding
David Ding 2017-9-27
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

此问题已关闭。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by