problem with nested function

I have a constrained minimization problem with objective function (in simplified form)
z=T*b1+p1*log(p1/(p1+p2+p3))+b2+p2*log(p2/(p1+p2+p3))+b3+p3*log(p3/(p1+p2+p3))
where
%T is dependent variable which will be iterated using formula
T=25-p1-p2-p3
b1=a1*2,%(a1 is a constant input)
b2=a2*3,%(a2 is a constant input)
b3=a3*4,%(a3 is a constant input)
and constraints
2*p1+3*p2=A1,%(A1 is a constant input)
3*p2+5*p3=A2,%(A2 is a constant input)
p1>=0,
p2>=0,
p3>=0,
so, I wrote on my script
function minimization
a1=input('a1=');
a2=input('a2=');
a3=input('a3=');
A1=input('A1=');
A2=input('A2=');
T=30%assumed T
while abs(T2-T)>0.1
T=0.5*(T2+T)
A=[];B=[];Aeq=[2,3,0;0,3,5];Beq=[A1;A2];wmin=[0.01;0.0;1;0.01];wmax=[];
optimal_p1p2p3=fmincon(@fozw,[0.1;0.1;0.1],A,B,Aeq,Beq,wmin,wmax);
function z=fozw(w);
p1=w(1);p2=w(2);p3=w(3);
z=b1+p1*log(p1/(p1+p2+p3))+b2+p2*log(p2/(p1+p2+p3))+b3+p3*log(p3/(p1+p2+p3));
end
T2=25-p1-p2-p3
end
end
Then, when I run the script. It appeared on command window
function is misplaced or it is nested improperly
What is the possible error on my script?.

 채택된 답변

Jan
Jan 2013년 5월 18일

0 개 추천

You cannot define functions or nested functions inside scripts. This is only allowed inside functions. Therefore insert the line "function YouFuncName" on top of the code, add needed inputs and outputs as usual.

댓글 수: 2

rio novendra
rio novendra 2013년 5월 18일
could you please help me to write it. I have tried it, but another problem aroused. Iteration was not performed.
rio novendra
rio novendra 2013년 5월 20일
thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by