Error: using fmincon and integral: Failure in initial objective function evaluation. FMINCON cannot continue

1 次查看(过去 30 天)
I am using fmincon and integral function simultaneously but I am getting an error. This is the error I am getting.
Unrecognized function or variable 'rhog'.
Error in optimization9>@(p)q(p,d,rhog) (line 39)
[s,fval] = fmincon(@(d,rhog)integral(@(p)q(p,d,rhog),2*pi,20*pi),d0,A,b,Aeq,beq,lb,ub,nonlcon,options); %minimize
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
Error in optimization9>@(d,rhog)integral(@(p)q(p,d,rhog),2*pi,20*pi) (line 39)
[s,fval] = fmincon(@(d,rhog)integral(@(p)q(p,d,rhog),2*pi,20*pi),d0,A,b,Aeq,beq,lb,ub,nonlcon,options); %minimize
Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in optimization9 (line 39)
[s,fval] = fmincon(@(d,rhog)integral(@(p)q(p,d,rhog),2*pi,20*pi),d0,A,b,Aeq,beq,lb,ub,nonlcon,options); %minimize
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
And this is my code. I am trying to optimize d and rhog. I am using matlabFunction to convert symbolic expression into function which has three variables p, d and rhog. I am numerically integrating wrt p then optimizing wrt d and rhog.
q = matlabFunction(answer,"Vars",[p,d,rhog]);
options = optimset('PlotFcns',@optimplotfval);
d0 = [0.5,30];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0,0];
ub = [1,2000];
nonlcon = [];
[s,fval] = fmincon(@(d,rhog)integral(@(p)q(p,d,rhog),2*pi,20*pi),d0,A,b,Aeq,beq,lb,ub,nonlcon,options);

采纳的回答

Star Strider
Star Strider 2023-11-14
编辑:Star Strider 2023-11-14
The ‘rhog’ variable must be defined in your workspace prior to using it as a function argument in ‘q’.
EDIT — (14 Nov 2023 at 17:20)
Please understand that ‘q’ is invisible to us.
q = matlabFunction(answer,"Vars",[p,d,rhog]);
options = optimset('PlotFcns',@optimplotfval);
d0 = [0.5,30];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0,0];
ub = [1,2000];
nonlcon = [];
% b(1) = d, b(2) = rhog
[s,fval] = fmincon(@(b)integral(@(p)q(p,b(1),b(2)),2*pi,20*pi),d0,A,b,Aeq,beq,lb,ub,nonlcon,options);
To optimise two (or more) different variables, create them as members of one parameter vextor, then optimise that parameter vector.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics and Optimization 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by