필터 지우기
필터 지우기

Controlling X variable when using Fmincon

조회 수: 2 (최근 30일)
Edward Umpfenbach
Edward Umpfenbach 2011년 9월 19일
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

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 9월 19일
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 2011년 9월 19일
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.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by