Effacer les filtres
Effacer les filtres

Index exceeds the number of array elements (1). Caused by: Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.

1 vue (au cours des 30 derniers jours)
My OBJECTIVE FUNCTION:
function f=objective2(h,x,F_r,T_r,UA_r,n)
options.Display='off';
%FlowStreams
x(1)=F_r(1);
x(2)=h;
x(6)=x(1)-x(2);
x(3)=x(2);
x(7)=x(6);
x(4)=x(3);
x(8)=x(7);
x(5)=x(4);
x(9)=x(8);
x(10)=x(5)+x(9);
x(11)=F_r(11);
x(12)=x(11);
x(16)=F_r(16);
x(17)=x(16);
x(18)=F_r(18);
x(19)=x(18);
x(20)=F_r(20);
x(21)=x(20);
x(13)=F_r(13);
x(14)=x(13);
x(15)=x(14);
%TempDependent
x(n+1)=T_r(1);
x(n+2)=T_r(2);
x(n+6)=T_r(6);
x(n+11)=T_r(11);
x(n+13)=T_r(13);
x(n+16)=T_r(16);
x(n+18)=T_r(18);
x(n+20)=T_r(20);
%Temp Independent
d1=fsolve(@(k)solve1(k,x,UA_r,n),[T_r(7),T_r(17)],options);
x(n+7)=d1(1);
x(n+17)=d1(2);
d2=fsolve(@(k)solve2(k,x,UA_r,n),[T_r(8),T_r(19)],options);
x(n+8)=d2(1);
x(n+19)=d2(2);
d3=fsolve(@(k)solve3(k,x,UA_r,n),[T_r(9),T_r(21)],options);
x(n+9)=d3(1);
x(n+21)=d3(2);
d4=fsolve(@(k)solve4(k,x,UA_r,n),[T_r(3),T_r(15),T_r(14),T_r(12),T_r(4),T_r(5)],options);
x(n+3)=d4(1);
x(n+15)=d4(2);
x(n+14)=d4(3);
x(n+12)=d4(4);
x(n+4)=d4(5);
x(n+5)=d4(6);
q = roots([0.000505*(x(5) + x(9)) 0.444*(x(5) + x(9)) -(0.444*(x(5)*x(n+5) + x(9)*x(n+9)) + 0.000505*(x(5)*x(n+5)^2 + x(9)*x(n+9)^2))]);
for k = 1:2
if q(k) > x(n+5) && q(k) < x(n+9)
x(n+10) = q(k);
break;
elseif q(k) > x(n+9) && q(k) < x(n+5)
x(n+10) = q(k);
break;
elseif k == 2
k=k+1;
end
end
f=-x(n+10);
end
Please Help

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Juil 2019
constraint2 will be called with a vector the same size as the vector passed to the objective function. Your x0 in your fmincon call is the scalar value 130, so the vector being passed to the objective function and to the constraint function will be a scalar.
You are confusing yourself by talking about x0 and x but also using @(h) defining your objective function. The x that is built inside your objective2 is not the x that will be passed to your constraint function. Perhaps you might need to build that x vector inside your constraint function as well.
  4 commentaires
BrokenDreams
BrokenDreams le 16 Juil 2019
I am sorry I have taken lot of your time. I have proceeded the same way you have mentioned. I am still getting an error message stating "Undefined variable h". I wrote the code with following things in mind:
1)Predict only h. Calculate the remaining x using the predicted h. That x has to follow the constraints written in the constraint2 function. x0 is the initial guess of h.
I am a noob.Thanks so much for your time.
CALLING FMINCON
x0=[130];
[y,fval]=fmincon(@(h)objective2(h,F_r,T_r,UA_r,n),x0,a,b,aeq,beq,lb,ub,@(x)constraint2(h,x,F_r,T_r,UA_r,n),options);
OBJECTIVE2 FUNCTION:
function f=objective2(h,F_r,T_r,UA_r,n)
x=myfun(h,F_r,T_r,n,UA_r);
f=-x(n+10);
end
CONSTRAINT2 FUNCTION:
function [c,ceq]=constraint2(h,x,F_r,T_r,UA_r,n)
ceq=[];
x=myfun(h,F_r,T_r,n,UA_r);
c(1)= x(n+6)-x(n+7);
c(2)= x(n+17)-x(n+16);
c(3)=10-x(n+16)+x(n+7);
c(4)=10-x(n+17)+x(n+6);
c(5)=x(n+7)-x(n+8);
c(6)=x(n+19)-x(n+18);
c(7)=10-x(n+18)+x(n+8);
c(8)=10-x(n+19)+x(n+7);
c(9)=x(n+8)-x(n+9);
c(10)=x(n+21)-x(n+20);
c(11)=10-x(n+20)+x(n+9);
c(12)=10-x(n+21)+x(n+8);
c(13)=x(n+2)-x(n+3);
c(14)=x(n+15)-x(n+14);
c(15)=10-x(n+14)+x(n+3);
c(16)=10-x(n+15)+x(n+2);
end
MYFUN FUNCTION:
function x =myfun(h,F_r,T_r,n,UA_r)
options.Display='off';
%FlowStreams
x(1)=F_r(1);
x(2)=h;
x(6)=x(1)-x(2);
x(3)=x(2);
x(7)=x(6);
x(4)=x(3);
x(8)=x(7);
x(5)=x(4);
x(9)=x(8);
x(10)=x(5)+x(9);
x(11)=F_r(11);
x(12)=x(11);
x(16)=F_r(16);
x(17)=x(16);
x(18)=F_r(18);
x(19)=x(18);
x(20)=F_r(20);
x(21)=x(20);
x(13)=F_r(13);
x(14)=x(13);
x(15)=x(14);
%TempDependent
x(n+1)=T_r(1);
x(n+2)=T_r(2);
x(n+6)=T_r(6);
x(n+11)=T_r(11);
x(n+13)=T_r(13);
x(n+16)=T_r(16);
x(n+18)=T_r(18);
x(n+20)=T_r(20);
%Temp Independent
d1=fsolve(@(k)solve1(k,x,UA_r,n),[T_r(7),T_r(17)],options);
x(n+7)=d1(1);
x(n+17)=d1(2);
d2=fsolve(@(k)solve2(k,x,UA_r,n),[T_r(8),T_r(19)],options);
x(n+8)=d2(1);
x(n+19)=d2(2);
d3=fsolve(@(k)solve3(k,x,UA_r,n),[T_r(9),T_r(21)],options);
x(n+9)=d3(1);
x(n+21)=d3(2);
d4=fsolve(@(k)solve4(k,x,UA_r,n),[T_r(3),T_r(15),T_r(14),T_r(12),T_r(4),T_r(5)],options);
x(n+3)=d4(1);
x(n+15)=d4(2);
x(n+14)=d4(3);
x(n+12)=d4(4);
x(n+4)=d4(5);
x(n+5)=d4(6);
q = roots([0.000505*(x(5) + x(9)) 0.444*(x(5) + x(9)) -(0.444*(x(5)*x(n+5) + x(9)*x(n+9)) + 0.000505*(x(5)*x(n+5)^2 + x(9)*x(n+9)^2))]);
for k = 1:2
if q(k) > x(n+5) && q(k) < x(n+9)
x(n+10) = q(k);
break;
elseif q(k) > x(n+9) && q(k) < x(n+5)
x(n+10) = q(k);
break;
elseif k == 2
k=k+1;
end
end
end
Walter Roberson
Walter Roberson le 16 Juil 2019
[y,fval]=fmincon(@(h)objective2(h,F_r,T_r,UA_r,n),x0,a,b,aeq,beq,lb,ub,@(h)constraint2(h,x,F_r,T_r,UA_r,n),options);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by