optimization of nested loop with two variables
I have an optimization problem of minimizing a two-stage sampling variance subject to the expected variable cost as constraint, and here are my functions:
the objective function is:
function dblSum=dblSumFun(n_h,m_hi)
h=length(N_h);
dblSum=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(1/m_hi(i)-1/M_hi(i))*M_hi(i).^2*swh2(h);
iSum=iSum+iTerm;
end
hTerm = (1/n_h(h)-1/N_h(h))*N_h(h)^2*sbh2(h)+N_h(h)/n_h(h)*iSum;
dblSum=dblSum+hterm;
end
end
and the constraint is:
function [c,ceq]=nonlcon(n_h,m_hi)
h=length(N_h)
function dblSumc=dblSumFun(n_h,m_hi)
dblSumc=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(0.5*m_hi(i))
iSum=iSum+iTerm;
end
hTerm = 3.5-n_h(h)*0.7-n_h(h)/N_h(h)*iSum;
dblSumc=dblSumc+hterm;
end
end
ceq=dblSumc;% this will be satisfied if it eval to zero
c=[];
and the main coding for optimization is as follows
n_h0=zeros(length(N_h),1);%initial guess
m_hi0=zeros(26*n_h(h),1);
X0=[n_h0;m_hi0];
A=[];
B=[];
Aeq=[];
Beq=[];
options=optimoptions(‘fmincon’,’Algorithm’,’sqp’,’Display’,’iter-detailed’,…
‘MaxFunctionEvaluations’,100000,’MaxIterations’,2000,…
‘FunctionTolerance’,1e-10);
[n_h,m_hi,const]=fmincon(@(n_h,m_hi) obj_function(n_h,m_hi),X0,A,B,Aeq,Beq,LB,UB,@(n_h,m_hi) nonlcon(n_h,m_hi),options)
I wanted to obtain a vector of n_h for h=1,…,26; and a vector of m_hi for h=1,…,26 and i=1,…,n_h(h). Any help?I have an optimization problem of minimizing a two-stage sampling variance subject to the expected variable cost as constraint, and here are my functions:
the objective function is:
function dblSum=dblSumFun(n_h,m_hi)
h=length(N_h);
dblSum=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(1/m_hi(i)-1/M_hi(i))*M_hi(i).^2*swh2(h);
iSum=iSum+iTerm;
end
hTerm = (1/n_h(h)-1/N_h(h))*N_h(h)^2*sbh2(h)+N_h(h)/n_h(h)*iSum;
dblSum=dblSum+hterm;
end
end
and the constraint is:
function [c,ceq]=nonlcon(n_h,m_hi)
h=length(N_h)
function dblSumc=dblSumFun(n_h,m_hi)
dblSumc=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(0.5*m_hi(i))
iSum=iSum+iTerm;
end
hTerm = 3.5-n_h(h)*0.7-n_h(h)/N_h(h)*iSum;
dblSumc=dblSumc+hterm;
end
end
ceq=dblSumc;% this will be satisfied if it eval to zero
c=[];
and the main coding for optimization is as follows
n_h0=zeros(length(N_h),1);%initial guess
m_hi0=zeros(26*n_h(h),1);
X0=[n_h0;m_hi0];
A=[];
B=[];
Aeq=[];
Beq=[];
options=optimoptions(‘fmincon’,’Algorithm’,’sqp’,’Display’,’iter-detailed’,…
‘MaxFunctionEvaluations’,100000,’MaxIterations’,2000,…
‘FunctionTolerance’,1e-10);
[n_h,m_hi,const]=fmincon(@(n_h,m_hi) obj_function(n_h,m_hi),X0,A,B,Aeq,Beq,LB,UB,@(n_h,m_hi) nonlcon(n_h,m_hi),options)
I wanted to obtain a vector of n_h for h=1,…,26; and a vector of m_hi for h=1,…,26 and i=1,…,n_h(h). Any help? I have an optimization problem of minimizing a two-stage sampling variance subject to the expected variable cost as constraint, and here are my functions:
the objective function is:
function dblSum=dblSumFun(n_h,m_hi)
h=length(N_h);
dblSum=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(1/m_hi(i)-1/M_hi(i))*M_hi(i).^2*swh2(h);
iSum=iSum+iTerm;
end
hTerm = (1/n_h(h)-1/N_h(h))*N_h(h)^2*sbh2(h)+N_h(h)/n_h(h)*iSum;
dblSum=dblSum+hterm;
end
end
and the constraint is:
function [c,ceq]=nonlcon(n_h,m_hi)
h=length(N_h)
function dblSumc=dblSumFun(n_h,m_hi)
dblSumc=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(0.5*m_hi(i))
iSum=iSum+iTerm;
end
hTerm = 3.5-n_h(h)*0.7-n_h(h)/N_h(h)*iSum;
dblSumc=dblSumc+hterm;
end
end
ceq=dblSumc;% this will be satisfied if it eval to zero
c=[];
and the main coding for optimization is as follows
n_h0=zeros(length(N_h),1);%initial guess
m_hi0=zeros(26*n_h(h),1);
X0=[n_h0;m_hi0];
A=[];
B=[];
Aeq=[];
Beq=[];
options=optimoptions(‘fmincon’,’Algorithm’,’sqp’,’Display’,’iter-detailed’,…
‘MaxFunctionEvaluations’,100000,’MaxIterations’,2000,…
‘FunctionTolerance’,1e-10);
[n_h,m_hi,const]=fmincon(@(n_h,m_hi) obj_function(n_h,m_hi),X0,A,B,Aeq,Beq,LB,UB,@(n_h,m_hi) nonlcon(n_h,m_hi),options)
I wanted to obtain a vector of n_h for h=1,…,26; and a vector of m_hi for h=1,…,26 and i=1,…,n_h(h). Any help? nonlinear optimization nested sum vector MATLAB Answers — New Questions