Access to variables of function in which calling function is nested
显示 更早的评论
I have this function:
function[measure1,minNumSensors1,feasible] ...
= measureAlg1FUN(numOfSensorstoTurnOn,sigmav,sigma_theta,...
sigmax,Dthres,Pmax,sourcePos,FCPos,sensorPos)
which contains a call to fmincon:
[P,fval]=fmincon(@objfun,P0,[],[],[],[],lb,ub,@confun,options);
When calling measureAlg1FUN, I get an error that the variables within this function are not accessible. These are needed for example in the constraint function defined as:
function[c,ceq]=confun(P)
a1=evalin('base','RvAlg');
a2=evalin('base','sigmax');
a3=evalin('base','sigma_theta');
a4=evalin('base','R_thetaAlg');
a5=evalin('base','RxAlg');
a6=evalin('base','Dthres');
Dp=diag(sqrt(P)/a2);
c=a3^2-a4*Dp*(Dp*a5*Dp+a1)^(-1)*Dp*a4'-a6;
ceq=[];
end
You can see that I have input the required variables using evalin from the base workspace. Of course, these variables do not exist within the base workspace, but within the workspace of function measureAlg1FUN. Changing the evalin argument from 'base' to 'caller', with the intent of having it access the variables in the caller function measureAlg1FUN, does not work either. I think this is because the caller is actually fmincon.
What can I do? Any help would be appreciated!
采纳的回答
更多回答(1 个)
Steven Lord
2016-4-4
1 个投票
If you're not modifying those variables (and you shouldn't be, as it could confuse the solver) you should pass them into the constraint function as additional parameters. The documentation for FMINCON links to a page describing several techniques by which you can do this.
类别
在 帮助中心 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!