MATLAB: Fmincon error: Field assignment to a non-structure array object.

fmincon

Hi,I try to minimize a value function by using fmincon, but there is an error message that I can't figure it out. Below is the main file and several functions. Any idea/comments are highly appreciated! Below is my code.
%main file
global Params
Params.N=100;%%number of gridpoints for the asset state
Params.interptype='spli'; %Interpolation method (see interp1): linear is fast but requires more gridpoints
Params.w_min=0.0001;%%min asset holdings
Params.w_max=80;%%max asset holdings, should be large enough so it is never binding
Params.sigma=5.0; %%risk-aversion
Params.beta=0.96;%%discount factor
Params.R=1.025;%interest rate (1+r)
Params.z=[1;2];%shock indices
Params.e=[30;60];%incomes for different shocks z
Params.P=[0.8 0.2; 0.2 0.8];%%the transition probability matrix for the income shock: P(i,j) is the prob that next income is j given that current shocks is i.
Params.n_z=length(Params.z);
Params.h_grid=[0.42;5.06]; %two health shock.health=poor or health=good;
Params.h=[1;2]; %health shock index
Params.n_h=length(Params.h);
Params.tran_h=[0.95 0.05;0.2 0.8]; %matrix health transition matrix
Params.J=60;%Max age. For instance, we might assume that j=1 corresponds to real age 25. Then j=75 corresponds to real age 100.
Params.Jr=40;%retirement age
Params.w_grid=linspace(Params.w_min,Params.w_max,Params.N); % wealth grid
Params.options = optimset('Algorithm','active-set','DerivativeCheck','off','Display','off', ...
'FunValCheck','on','GradObj','on','MaxFunEvals',1e3,'TolFun',1e-10,'TolCon',1e-10);
%Construct the value and policy vectors. We define them as
ValVec=zeros(Params.N,Params.n_z,Params.n_h,Params.J);
Cons=zeros(size(ValVec)); %Consumption policy
Anext=zeros(size(ValVec));%bonds policy
Asnext=zeros(size(ValVec)); %stock policy
%Start the value function iteration
for j=Params.J:-1:1
disp(j);
age=j;
for i=1:Params.N
for k=1:Params.n_z
for h=1:Params.n_h
% ValVec=zeros(Params.N,Params.n_z,Params.n_h,Params.J);
w=Params.w_grid(i);
z=Params.z(k);
hh=Params.h(h);
state=[w z hh];
c_min=0.001;
c_max=state(1)+exp(Income(state,j));
if j==Params.J
policy=[0 0];
Cons(i,k,h,Params.J)=c_max;
Anext(i,k,h,Params.J)=policy(1);
Asnext(i,k,h,Params.J)=policy(2);
ValVec(i,k,h,Params.J)=(0.8*c_max+0.2*Params.h_grid(state(3)))^(1-Params.sigma)/(1-Params.sigma);
else
x0 = [1e-4 1e-4];
A = [-1 -1];
b = 0;
Aeq=[];
beq=[];
lb=[];
ub=[];
[policy,val]=fmincon(@value,x0,A,b,Aeq,beq,lb,ub,Params.options,state,j,ValVec(:,:,:,j+1));
Anext(i,k,h,j)=policy(1);
Asnext(i,k,h,j)=policy(2);
Cons(i,k,h,j)=state(1)+exp(Income(state,j))-policy(1)-policy(2);
ValVec(i,k,h,j)=-val;
end
end
end
end
end
%value function
function out=value(policy,state,age,ValVec)
global Params
c=state(1)+exp(Income(state,age))-policy(1)-policy(2);
z=state(2);
hh=state(3);
u=(0.8*c+0.2*Params.h_grid(state(3)))^(1-Params.sigma)/(1-Params.sigma);
valnext=Params.P(z,1)*Params.tran_h(hh,1)*interp1(Params.w_grid,ValVec(:,1,1,(age+1)),wnext(policy),Params.interptype)...
+Params.P(z,1)*Params.tran_h(hh,2)*interp1(Params.w_grid,ValVec(:,1,2,(age+1)),wnext(policy),Params.interptype)...
+Params.P(z,2)*Params.tran_h(hh,1)*interp1(Params.w_grid,ValVec(:,2,1,(age+1)),wnext(policy),Params.interptype)...
+Params.P(z,2)*Params.tran_h(hh,2)*interp1(Params.w_grid,ValVec(:,2,2,(age+1)),wnext(policy),Params.interptype);
val=u+Params.beta*valnext;
out=-val; %need to multiply by minus one because fmincon minimizes.
end
%income function
function out=Income(state,age)
global Params
if age>=15 && age<=25 %define age dummy
age1=1;
else
age1=0;
end
if age>=26 && age<=35
age2=1;
else
age2=0;
end
if age>=36 && age<=39
age3=1;
else
age3=0;
end
if state(3)==1
h_index=1;
else
h_index=0;
end
if age>=Params.Jr
out=3.5-1.2*h_index;
else
out=log(Params.e(state(2)))-(age1)*0.2-(age2)*0.3-(age3)*0.5-0.8*h_index;
end
end
%next period wealth function.
function out=wnext(policy)
global Params
out=policy(1)*Params.R+policy(2)*(1+rand); %Rs is iid.
end

Best Answer

The nonlcon parameter position is required before the options.
Also you are passing extra parameters which can confuse fmincon because of backwards compatibility with options that were never documented. Do not pass extra parameters that way : it has not been supported since MATLAB 6.1. parameterize the anonymous function instead.