I want to minimize a utility function which contain loops in it via using fmincon, but the command window shows error of "NONLCON must be a function". I wish someone could read the code following, point out the error and show the way to correct it. Thanks a lot.
function f=function_1_2(x)r0=.02;sigma=.04;gamma=5;delta=.4;mu0=.06;N=60;T=60;dt=T/N;R=40;A0=50;p=.16;b=.7;utility0=0;asset=A0*ones(1,N+1);L=A0;b0=b*ones(1,N+1);contri=p;S=0*ones(1,N+1);r=r0;mu=mu0;utility=utility0*ones(1,N+1);t=0:dt:T;for j=1:N S(1,j)=asset(1,j)-L; contri(1,j)=x(1)-x(2)*S(1,j)/R; asset(1,j+1)=asset(1,j)+((asset(1,j).*(r+x(3)*(mu-r)))+40*contri(1,j)-15*b0(1,j))*dt+x(2)*sigma*asset(1,j)*sqrt(dt).*randn(1); if j<R C(1,j)=ones(1,1)-(contri(1,j)-x(1)*S(1,j)/R); elseif j>=R & j<=N C(1,j)=b; end utility(1,j+1)=utility(1,j)+exp(-delta*j)*(C(1,j).^(1-gamma))/(1-gamma);endf=(-1)*mean(utility(1,N+1))
And the fmincon function is:
clearclc%%lower bound
lb=zeros(3,1);lb(2)=.02;%%upper bound
ub=inf(3,1);ub(2)=1;ub(3)=1;%%start point
x0=[0,0.02,0];%%optimize the utility function
[x,fval]=fmincon(@function_1_2,x0,[],[],[],[],[],[],lb,ub)
Best Answer