Question of fmincon
조회 수: 4 (최근 30일)
이전 댓글 표시
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);
end
f=(-1)*mean(utility(1,N+1))
And the fmincon function is:
clear
clc
%%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)
댓글 수: 0
채택된 답변
Walter Roberson
2012년 3월 27일
The lower bound and upper bound should be the 7th and 8th arguments, but you have them as the 9th and 10th arguments.
Somewhere in there you have two too many []
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!