## how to use ``while'' in implementing an algorithm?

### Susan (view profile)

on 30 Apr 2019
Latest activity Commented on by Susan

### Susan (view profile)

on 2 May 2019
Hello guys,
I would like to implement the following algorithm in MATLAB. Could someone please kindly help me out?
I have an objective function (f) that I would like to maximize with respect to 4 variables (c, p, ptilda, v). At each step, I assume one of the variable is the optimization variable and three others are fixed and given.
When I find the c_opt, p_opt, ptilda_opt, and v_opt I calculate the f with these valuse and I continue till the difference between two consecutive f is less than a given epsilon.
I will start with the initial values, but at the second iteration I would like to use the c_opt, p_opt, ptilda_opt, and v_opt that I found in previous iteration as an initial values. Can someone kindly take a look at the following code and tell me what modifications are needed in order to get what I am looking for? Thanks in advance.
%%
c0 = zeros(IL, JL, K, M); %initial value for variable c
p0 = ones(JW , K); %initial value for variable p
ptilda0 = zeros(IL, M, JL, K);%initial value for variable ptilda
v0 = cell(IL, JL, JL, K, M); %initial value for variable v. Each cell contains a matrix of size Nt*Nr
iteration = 1;
epsilon = 1e-2;
while 1
%%%%%% First Step
c = sym('c', [IL, JL, K, M)];
objfun_c_symbExpression = f(c, p0, ptilda0, v0);
objfun_c_AnonymousFunction = matlabFunction(objfun_c_symbExpression, 'Vars', {c});
objfun_c = @(c) objfun_c_AnonymousFunction(c);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[c_opt,fval_c,flag_c] = fmincon(objfun_c,c0,A_c,b_c,[],[],lb_c,ub_c,[],opts); % I have defined all A_c, b_c, lb_c, ub_c
%%%% Second Step
p = sym('p', [JW , K]);
objfun_p_symbExpression = f(c_opt, p, ptilda0, v0);
objfun_p_AnonymousFunction = matlabFunction(objfun_p_symbExpression, 'Vars', {p});
objfun_p = @(p) objfun_p_AnonymousFunction(p);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[p_opt,fval_p,flag_p] = fmincon(objfun_p,p0,[],[],[],[],lb_p,ub_p,nonlcon_p,opts);
%%%% Third Step
ptilda = sym('ptilda', [IL, M, JL, K]);
objfun_ptilda_symbExpression = f(c_opt, p_opt, ptilda, v0);
objfun_ptilda_AnonymousFunction = matlabFunction(objfun_ptilda_symbExpression, 'Vars', {ptilda});
objfun_ptilda = @(ptilda) objfun_ptilda_AnonymousFunction(ptilda);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[ptilda_opt,fval_ptilda,flag_ptilda] = fmincon(objfun_ptilda,ptilda0,A_ptilda,b_ptilda,[],[],lb_ptilda,ub_ptilda,nonlcon_ptilda,opts);
%%%% Fourth Step (I have problem in implementing this part)
v = sym('v', ....);
objfun_v_symbExpression = f(c_opt, p_opt, ptilda_opt, v);
objfun_v_AnonymousFunction = matlabFunction(objfun_v_symbExpression, 'Vars', {v});
objfun_v = @(v) objfun_v_AnonymousFunction(v);
opts = optimset('Display','iter','Algorithm','sqp','MaxFunEval',inf,'MaxIter',Inf);
[v_opt,fval_v,flag_v] = fmincon(objfun_v,v0,A_v,b_v,[],[],lb_v,ub_v,nonlcon_v,opts);
%% Calculate the f with the computed varaibles
f_evolution = [f_evolution, f(c_opt, p_opt, ptilda_opt, v_opt)];
%% termination criterion
if size(f_evolution, 2)>2
err = abs(f_evolution(end) - f_evolution(end - 1) );
if err < epsilon
break,
end
end
iteration=iteration+1;
end

Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 2 May 2019
@Su san: do you have the optimization toolbox ?
Susan

### Susan (view profile)

on 2 May 2019
@Stephen Cobeldick Yes, I do.
Susan

### Susan (view profile)

on 2 May 2019
@dpb Thanks for offering the help.
Would you mind if I ask you what you mean by a subset of code? I didn't get you quit well. The code calls several functions to calculate a parameter of interest.
Thanks again