Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Using the intermediate values in a search algorithm.

2 ビュー (過去 30 日間)
Gleb Gertsman
Gleb Gertsman 2017 年 9 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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