using fmincon to optimize function including simultaneous equations

1 次查看(过去 30 天)
My optmization goal is equation (14) to maximize U cell
The code has 3 m.file(s)
1)
function [x,fval]=main
% 4 X's
x0=[0.207;0.00003288;0.592;0.004];
lb=[0.1656;0.000026304;0.4736;0.0032];
ub=[0.2484;0.000039456;0.7104;0.0048];
%linear constrain
A=[];B=[];
Aeq=[];Beq=[];
[x,fval]=fmincon(@objfunc,x0,A,B,Aeq,Beq,lb,ub,@confunc);
end
2)
function f=objfunc(x)
F=96485.4;a=0.051;b=0.663;R=8.3144;T=303;Va=0.000055;Vc=0.000055;XIn=0;
AcIn=1.56;OIn=0.3125;Am=0.0005;Fx=10;Yac=0.05;Kdec=0.000833;Ca=400;
Cc=500;Qa=0.0000225;Qc=0.0111;
q1=x(1)*exp((a*F*q3)/(R*T))*(q5*q7/(x(3)+q5)); %eq1
q2=-x(2)*(q6/(x(4)*q6))*exp(((b-1)*F*q4)/(R*T)); %eq2
0=Qa*(AcIn-q5)-(Am*q1); %eq3
0=(Qa*((XIn-q7)/Fx))+(Am*Yac*q1)-(Va*Kdec*q7); %eq4
0=Qc(OIn-q6)+q2*Am; %eq5
0=(3600*q8)-(8*F*q1);%eq6
0=(3600*q8)-(4*F*q2);%eq7 %
U0=0.77;dm=0.0001778;dcell=0.022;kaq=5;km=17;
f=U0-q3+q4-(((dm/km)+(dcell/kaq))*q8);
end
3)
function [c,ceq]=confunc(x)
F=96485.4;a=0.051;b=0.663;R=8.3144;T=303;Va=0.000055;Vc=0.000055;XIn=0;
AcIn=1.56;OIn=0.3125;Am=0.0005;Fx=10;Yac=0.05;Kdec=0.000833;Ca=400;
Cc=500;Qa=0.0000225;Qc=0.0111;
ceq=[Qa*(AcIn-q5)-(Am*q1);(Qa*((XIn-q7)/Fx))+(Am*Yac*q1)-(Va*Kdec*q7);Qc(OIn-q6)+q2*Am;(3600*q8)-(8*F*q1);(3600*q8)-(4*F*q2)];
c=[];
end
Can anyone enlighten me on what I might made wrong
  2 个评论
John D'Errico
John D'Errico 2020-3-5
Note that you cannot use fmincon to force the parameters to be integers. You seem to have indicated that in your problem statement, but it is not clear.
Husam Mahmoud
Husam Mahmoud 2020-3-5
Thanks for your reply, The parameter which are definded as intergres are the constatns used in solving the equations. However, x(1-4) are arrays and q(1-4) are functions of x(1-4).
>> main
Error: File: objfunc.m Line: 8 Column: 2
Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality,
use '=='.
Error in fmincon (line 546)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in main (line 9)
[x,fval]=fmincon(@objfunc,x0,A,B,Aeq,Beq,lb,ub,@confunc);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

请先登录,再进行评论。

回答(2 个)

Matt J
Matt J 2020-3-5
编辑:Matt J 2020-3-5
These lines are illegal in Matlab. You cannot put a literal number like 0 on the left hand side of an '=', except when working with symbolic equations, which would not be appropriate here.
0=Qa*(AcIn-q5)-(Am*q1); %eq3
0=(Qa*((XIn-q7)/Fx))+(Am*Yac*q1)-(Va*Kdec*q7); %eq4
0=Qc(OIn-q6)+q2*Am; %eq5
0=(3600*q8)-(8*F*q1);%eq6
0=(3600*q8)-(4*F*q2);%eq7 %
If these are meant to represent equations that your solution must satisfy, they should go in confunc as additional equality constraints.
  1 个评论
Husam Mahmoud
Husam Mahmoud 2020-3-5
编辑:Husam Mahmoud 2020-3-5
Thanks for your answer!, I agree, i have them already in confunc. Following your advise i removed them from the obj function, yet i still get this error.
>> main
Undefined function or variable 'q3'.
Error in objfunc (line 6)
q1=x(1)*exp((a*F*q3)/(R*T))*(q5*q7/(x(3)+q5)); %eq1
Error in fmincon (line 546)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in main (line 9)
[x,fval]=fmincon(@objfunc,x0,A,B,Aeq,Beq,lb,ub,@confunc);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-3-5
You do not have simultaneous variables. You have a system of differential equations that are functions of time, but you are attempting to optimize as if the system was timeless.
You appear to working on chemical reactions. It might hypothetically make sense to optimize steady state, and it might hypothetically make sense to optimize over a limited time, but either way you would need to give time for the system to evolve with an ode solver, which you are not doing at all.
  3 个评论
Walter Roberson
Walter Roberson 2020-3-5
Is the idea that when you reach the steady state, the function stops changing, so its derivative becomes 0, so the left side of the differential equations become 0?
If so then you should probably use fsolve() on the system of expressions.
Husam Mahmoud
Husam Mahmoud 2020-3-5
编辑:Husam Mahmoud 2020-3-5
Thank you for your reply again, Yes it becomes zero. However, this would be true if i am willing to solve this without optimizing any of the variables, correct me if i am wrong.
All of these equations are function of q1, and q1 is function of my objective varibles x(1),x(2),x(3),x(4).
its an endless loop where q3,q4,q5,q6,q7,q8 are needed to find q1 and q2 yet they are needed to find q3-q8.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by