How to assign an equation built by string to parameter c in nonlinear inequality constraint function for fmincon?

1 次查看(过去 30 天)
Using R2020a, Prerelease. For fmincon optimization function with inequality nonlinear constraints. The symplified version of the constraint function may look as follow:
function [c,ceq] = myconfun(x)
c(1) = x(3)^2-(12.0000-x(1))^2-(0.0000-x(2))^2; % x_min
c(2) = (12.0000-x(1))^2+(0.0000-x(2))^2-x(4)^2; % x_max
ceq = [];
end
and the code above works fine. In order to integrate any other constants (instead of 12 and 0) into c(1) and c(2), we can use string, then the code is modified as follow:
function [c,ceq] = myconfun(x)
global MYDATA
P = round(double(MYDATA),4);
% the first inequality constraint function (x_min)
formatSpec1 = 'x(3)^2-(%.4f-x(1))^2-(%.4f-x(2))^2';
% the second inequality constraint function (x_max)
formatSpec2 = '(%.4f-x(1))^2+(%.4f-x(2))^2-x(4)^2';
% syms str1 str2
% to keep simple. It is supposed to be a loop here of course
eqt1 = str2sym(string(sprintf(formatSpec1,P(1,1),P(1,2))));
eqt2 = str2sym(string(sprintf(formatSpec2,P(1,1),P(1,2))));
c(1) = eqt1;
c(2) = eqt2;
ceq = [];
end
The second version is supposed to be exactly the same as the first one. But MATLAB doesn't accept it and is warning with the message:
Error using fmincon (line 710)
FMINCON requires all values returned by functions to be of data type double.
Error in mz_test (line 10)
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
I have also tried other options such as assigning, sym, symfun, str2double but there was the same message all the times. Please explain how to achieve a required format? And what sort of format is it actually (of c(1) and c(2)) ??? Thank you in advanced for prompt respond! (Tu, 18.02.2020)

采纳的回答

Jon
Jon 2020-2-19
编辑:Jon 2020-2-19
I don't think the approach you are following is worth pursuing further.
This explains how you can pass additional parameters, to your constraint function.
  3 个评论
Jon
Jon 2020-2-19
编辑:Jon 2020-2-19
I'm sorry, I don't understand your comment. Please explain exactly what you are trying to do. In general you must define a function, such as your myconfun, which given a trial value of x, and some parameter values, which you pass as explained in the link, gives you values of each constraint variable in the vector c which is returned by myconfun.
I would suggest following the "nested" function approach described in the link.
Note that the example in the link is for fminunc and so doens't have a constraint function.
For fmincon your case you would nest (include within the end statement of your of your overall function )your myconfun so that it would have access to the parameter values that you want it to use.
Petr Ch
Petr Ch 2020-2-19
Thank you for the tips! I agree that this is too confusing approach. In fact, this problem can be solved without any constraints. I will accept your first answer:)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by