'fmincon' and how to declare parameters without 'global'

4 views (last 30 days)
Hi All,
I am trying not to use "global" to declare shared parameters as someone suggested that it causes problems. But I have not been able to include the parameters in the function properly. I am using "fmincon". I've posted the error and warning message I am getting below the code. The code is simple, it just has too many constants and the model is multiplicative, makes it look complex. I was wondering if I am correctly declaring the parameters by including them in the function.
close all
clear all
clc
T=100;
a=0.6;
a1=0.16;
a2=0.24;
tauw=0.5;
tauc=1;
Thetac=0.02;
Thetaw=0.02;
gammac=0.02;
gammaw=0.01;
lambda=0.1;
sigma=3;
phic=50;
phiw=110;
nu=1;
eta=0.6;
%Initial values
Ac0=10;
Aw0=89;
Nc0=1;
Nw0=1;
Sw0=570
Sc0=430
Ec=zeros(T,1);Ew=zeros(T,1);
Yc=zeros(T,1); Yw=zeros(T,1);
Y=zeros(T,1);
xc=zeros(T,1);xw=zeros(T,1);
Ac=zeros(T+1,1); Aw=zeros(T+1,1);
Ac(1,1)=Ac0;Aw(1,1)=Aw0;
Pc=zeros(T,1); Pw=zeros(T,1);
pc=zeros(T,1); pw=zeros(T,1);
cc=zeros(T,1); cw=zeros(T,1);
Nc=zeros(T+1,1);Nw=zeros(T+1,1);
Nc(1,1)=Nc0; Nw(1,1)=Nw0;
Sc=zeros(T+1,1); Sw=zeros(T+1,1);
Sc(1,1)=Sc0; Sw(1,1)=Sw0;
Lw=zeros(T,1);Lc=zeros(T,1);
Xw=zeros(T,1);Xc=zeros(T,1);
Pic=zeros(T,1);Piw=zeros(T,1);
for t=1:T
A=[Ac(t),Aw(t)];
Xc(t)=Sc(t)/(Ac(t)^lambda);
cc(t)=phic*(1+1/(Sc(t)+Xc(t))^gammac);
cw(t)=phiw*(1+1/(Sw(t)+Xw(t))^gammaw);
c=[cc(t),cw(t)];
x0=[Nc(t),Nw(t)];
UB=[10^12*ones(T,1); 10^12*ones(T,1)]; % upper bound for the optimization
LB=[zeros(T,1); zeros(T,1)]; % lower boud for the optimization
options=optimset('Algorithm','active-set','TolFun',1e-11,'TolX',1e-9);
out1=fmincon(@productivity_vL1,x0,[],[],[(1+tauw)*Aw(t),-(1+tauc)*Ac(t)],0,LB,UB,[],A,eta,nu, a,c, a1, a2, sigma,tauc,tauw,Thetac,Thetaw,options);
Nc(t+1)=out1(1);
Nw(t+1)=out1(2);
Ac(t+1)=(1+tauc*Thetac)*Ac(t);
Aw(t+1)=(1+tauw*Thetaw)*Aw(t);
Pc(t)=((Aw(t)/Ac(t))^(1-a1)*(cc(t)/cw(t))^a2)/(1+((Aw(t)/Ac(t))^(1-a1)*(cc(t)/cw(t))^a2)^(1-sigma))^(1/(1-sigma));
Pw(t)=(1-Pc(t)^(1-sigma))^(1/(1-sigma));
Lc(t,1)=Pc(t)^(1-sigma);
Lw(t,1)=1-Lc(t);
Ec(t)=(a2/cc(t)*Pc(t)^(1/(1-a1))*Lc(t)^((1-a)/(1-a1))*Ac(t+1))^((1-a1)/(1-a1*(1+a2))*(1-a2));
Ew(t)=(a2/cw(t)*Pw(t)^(1/(1-a1))*Lw(t)^((1-a)/(1-a1))*Aw(t+1))^((1-a1)/(1-a1*(1+a2))*(1-a2));
Sc(t+1)=Sc(t)+Xc(t)-Ec(t)/Ac(t)^lambda;
Sw(t+1)=Sw(t)+Xw(t)-Ew(t);
FUNCTION
function F=productivity_vL1(x,A,eta,nu, a, a1, a2, c, sigma,tauc,tauw,Thetac,Thetaw)
Nc=x(1);
Nw=x(2);
Thetac=nu*(Nc/((1+tauc)*A(1)))^eta;
Thetaw=nu*(Nw/((1+tauw)*A(2)))^eta;
Ac1=(1+tauc*Thetac)*A(1);
Aw1=(1+tauw*Thetaw)*A(2);
p1=1+1/((1-a)*(1-sigma))+(a2*(1+(1-a)*(1-sigma)))/((1-a)*(1-sigma)*(1-a1*(1+a2)));
Lc=((Nc^(1-eta)/(nu*eta*a1*(1-a1)))^((1-a1)/(1-a))*(a2/c(1)*Ac1)^(-a2*(1-a1)/((1-a)*(1-a1*(1+a2)))))^(1/p1);
Lw=((Nw^(1-eta)/(nu*eta*a1*(1-a1)))^((1-a1)/(1-a))*(a2/c(2)*Aw1)^(-a2*(1-a1)/((1-a)*(1-a1*(1+a2)))))^(1/p1);
F=Lc+Lw-1;
The error message I am getting is below
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures
Displays Warning, for details.
> In createOptionFeedback at 33
In prepareOptionsForSolver at 31
In fmincon at 210
In paper2_vL1 at 85
Warning: Length of lower bounds is > length(x); ignoring extra bounds.
> In checkbounds at 27
In fmincon at 317
In paper2_vL1 at 85
Warning: Length of upper bounds is > length(x); ignoring extra bounds.
> In checkbounds at 41
In fmincon at 317
In paper2_vL1 at 85
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As
Structures Displays Warning, for details.
> In fmincon at 363
In paper2_vL1 at 85
Attempted to access A(2); index out of bounds because numel(A)=1.
Error in productivity_vL1 (line 11)
Thetaw=nu*(Nw/((1+tauw)*A(2)))^eta;
Error in fmincon (line 564)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in paper2_vL1 (line 85)
out1=fmincon(@productivity_vL1,x0,[],[],[(1+tauw)*Aw(t),-(1+tauc)*Ac(t)],0,LB,UB,[],A,eta,nu, a,c, a1, a2, sigma,tauc,tauw,Thetac,Thetaw,options);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.

Answers (2)

dpb
dpb on 4 Jan 2015
Per the doc's, the function for fmincon is a function that accepts a vector x and returns a scalar f. You've got a whole lot of other stuff in there as well; that doesn't meet the specification and so fmincon is thereafter all confused...
function F=productivity_vL1(x)
is all you can have as the function that is the one whose handle is passed to fmincon
As referenced in the links in the doc, you have the choices of either using an anonymous function which includes the other constants within it explicitly, a secondary file that uses a nested function and which calls fmincon from the visible function which accepts all the other variables or the global solution.
I don't have the time at the present to try to decipher just who's who in the zoo in your function to try to recast it, but the link to current doc for the issue is at
along with links to some more complex cases as guides.
It is unfortunate that TMW didn't include a user-definable array or structure arrangement in the user interface to make this an easier process than it is, but they chose not to so we have this to deal with...

mrrox
mrrox on 5 Jan 2015
Thank you very much guys!

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!