Solving simultaneous equations with fmincon (Error:Not enough input arguments / Supplied objective function must return a scalar value.)

2 views (last 30 days)
Hi guys, I'm having a problem with solving a system of nonlinear equations using symbolic variables and fmincon. I tried to follow the guidance provided in this thread but I'm still not able to make it work.
This is my code (I have presented a simplified system of equations for the sake of simplicity.):
x=sym('x',[1 2]);
eqn_test=[x(1)+x(2)^2;x(2)-x(1)-6];
c_con=[];
ceq_con=eqn_test;
nonlcon = matlabFunction(c_con,ceq_con,[],[],'vars',x);
obj=@(x)0;
[soln1,error1] = fmincon(obj,[0 0],[],[],[],[],[-5 -5],[5 5],nonlcon,[]);
% Not enough input arguments.
% Error in
% symengine>@(x1,x2)deal(zeros(0,0),[x1+x2.^2;-x1+x2-6.0],zeros(0,0),zeros(0,0))
% Error in fmincon (line 633)
% [ctmp,ceqtmp] =
% feval(confcn{3},X,varargin{:});
% Caused by:
% Failure in initial nonlinear constraint
% function evaluation. FMINCON cannot continue.
eqn_test_fun=matlabFunction(ceq_con);
fun2 = @(f) eqn_test_fun(f(1),f(2));
[soln2,error2] = fmincon(fun2,[0 0],[],[],[],[],[-5 -5],[5 5],[],[]);
% Error using fmincon (line 619) Supplied objective function must return a scalar
% value.
I read that I should add a sum to my objective function but I don't know how to make it work. The code below returns the wrong solutions.
eqn_test_fun=matlabFunction(ceq_con);
fun2 = @(f) sum(eqn_test_fun(f(1),f(2)));
[soln3,error3] = fmincon(fun2,[0 0],[],[],[],[],[-5 -5],[5 5],[],[]);
% Local minimum found that satisfies the constraints.
%
% Optimization completed because the objective function is non-decreasing in
% feasible directions, to within the default value of the optimality tolerance,
% and constraints are satisfied to within the default value of the constraint tolerance.
%
% <stopping criteria details>
% soln3 =
%
% 0.0000 -0.5000
%
%
% error3 =
%
% -6.2500
Would you please help me correct my error? Thank you!
Edit: I tried adapting some code from the MATLAB documentation to work with symbolic variables (which I need because my equations are changing with every iteration, so I cannot write an equation to an m-file), but I have the same error:
x=sym('x',[2 1]);
F(1) = (x(1)+1)*(10-x(1))*(1+x(2)^2)/(1+x(2)^2+x(2));
F(2) = (x(2)+2)*(20-x(2))*(1+x(1)^2)/(1+x(1)^2+x(1));
fbnd=matlabFunction(F);
c_con = []; % No nonlinear inequality
ceq_con = fbnd(x(1),x(2)); % fsolve objective is fmincon nonlinear equality constraints
fminconstr = matlabFunction(c_con,ceq_con,[],[],'vars',x);
lb = [0,0]; % Lower bound constraint
rng default % Reproducible initial point
x0 = 100*randn(2,1);
opts = optimoptions(@fmincon,'Algorithm','interior-point','Display','off');
x = fmincon(@(x)0,x0,[],[],[],[],lb,[],fminconstr,opts)
% Not enough input arguments.
%
% Error in
% symengine>@(x1,x2)deal(zeros(0,0),[-((x2.^2+1.0).*(x1+1.0).*(x1-1.0e1))./(x2+x2.^2+1.0),-((x1.^2+1.0).*(x2+2.0).*(x2-2.0e1))./(x1+x1.^2+1.0)],zeros(0,0),zeros(0,0))
%
% Error in fmincon (line 633)
% [ctmp,ceqtmp] =
% feval(confcn{3},X,varargin{:});
%
% Caused by:
% Failure in initial nonlinear constraint
% function evaluation. FMINCON cannot continue.

Answers (1)

Alan Weiss
Alan Weiss on 6 Oct 2019
Edited: Alan Weiss on 6 Oct 2019
I think that you are making life hard for yourself by using symbolic variables instead of using plain numeric variables and functions. For example,
fun = @(x)[x(1)+x(2)^2;x(2)-x(1)-6];
x = fsolve(fun,[-1;2])
You say that for your application you must use symbolic variables, but I doubt that is true. If you can write a brief explanation of what you are trying to do, and why you think you need symbolic variables, then maybe we can show you how to do what you want.
Alan Weiss
MATLAB mathematical toolbo documentation

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!