Controlling X variable when using Fmincon

3 Ansichten (letzte 30 Tage)
Edward Umpfenbach
Edward Umpfenbach am 19 Sep. 2011
Hi,
I have a problem where I want to reference previous iterations of Fmincon to use in current function evaluations. For example, the function looks something like this in pseudocode:
[log_value] = function (params)
for i=1:1:num_days
temp_indicator_vector = indicator_vector{1,i};
for j=1:1:size(X) if indicator_vector(j) == 0 x(j) = x_old(j) end end
.... calculate log_value based on x
So, the question is how do I save the previous iteration of x as x_old so that I can use it in the current? I know I can write each iteration to the disk and read it back into Matlab, but I am hoping to do it in memory to keep the computation as fast as possible.
Any help would be appreciated,
Ed

Antworten (2)

Fangjun Jiang
Fangjun Jiang am 19 Sep. 2011
After the iteration, just use x_old=x;
Do you know that you can assign a matrix to another matrix, as long as they are in the same size? Like this:
x_old=1:10;
x=rand(1,10);
x_old=x;
Two other things,
  1. You have capital X and low case x. Don't mix them.
  2. size(X) will return two values for scalar, vector or 2-D matrix. The statement "for j=1:1:size(X)" will use the first value returned by size(X) function. But it will be better if you specify it like size(X,1) or size(X,2) depending on your need.#

Edward Umpfenbach
Edward Umpfenbach am 19 Sep. 2011
Thanks for your answer.
I am aware of points 1 and 2. What I wrote above was just quick pseudocode.
I know I will have code like what you wrote above. I am still unsure of where to put it. If it was a script, x and x_old are in the workspace and I just update them on each iteration. I am using a function (Fmincon) to call another function (my custom written function).
If I modify the code for Fmincon so that it has
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN, X_old] = fmincon(FUN,X,A,B,Aeq,Beq,LB,UB,NONLCON,options,varargin)
then it should return X and X_old, right? I just set X_old = X at line 1. Then X_old can be passed as a parameter to my written function?
Maybe I just need to try and see what happens. Just wanted to post first to see if anyone has advice since I generally don't mess with the functions distributed with Matlab.

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by