Why won't fmincon work in this Rosen-Suzuki problem?
4 views (last 30 days)
Show older comments
Hello, I'm trying to solve a Rosen-Suzuki problem by using fmincon and I have written my code below:
clc
close all
clear all
%Rosen-Suzuki objective function to be optimized
fun = @(x)x(1)^2+x(2)^2+2*x(3)^2-x(4)^2-5*x(1)-5*x(2)-21*x(3)+7*x(4)+100;
%No linear constraints exist, so our linear inequality constraints are left
%blank:
A = [];
b = [];
Aeq = [];
beq = [];
%Defining our non-linear constraint:
n_C = @nC;
%Defining lower and upper bounds:
l_b = [-100 -100 -100 -100];
u_b = [100 100 100 100];
%Arbitrary initial starting point:
x0 = [1 1 1 1];
%Optimizing objective function using fmincon:
optim = fmincon(fun,x0,A,b,Aeq,beq,l_b,u_b,n_C)
%Function for nonlinear constraints:
function [c,ceq] = nC
%Since there are no nonlinear equality constraints:
ceq = [];
%Then we define our three nonlinear inequality constraints:
c(1) = x(1)^2+x(2)^2+x(3)^2+x(4)^2+x(1)-x(2)+x(3)-x(4)-100;
c(2) = x(1)^2+2*x(2)^2+x(3)^2+2*x(4)^2-x(1)-x(4)-10;
c(3) = 2*x(1)^2+x(2)^2+x(3)^2+2*x(1)-x(2)-x(4)-5;
end
However when I run it, I get the error:
Error using a6q1>nC
Too many input arguments.
Error in fmincon (line 654)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in a6q1 (line 26)
optim = fmincon(fun,x0,A,b,Aeq,beq,l_b,u_b,n_C)
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
I'm not sure what's causing it and any help would be greatly appreciated. Thank you for your assistance and time.
0 Comments
Answers (1)
Harshavardhan
on 30 May 2025
The error you are encountering is due to the way you have defined your nonlinear constraint function “nC”. MATLAB's “fmincon” expects the nonlinear constraint function to accept one input argument (the variable vector “x”) and return two outputs: the inequality constraints “c” and the equality constraints “ceq”.
To fix the issue, the code segment
function [c,ceq] = nC
should be replaced with
function [c,ceq] = nC(x)
For more information on “fmincon”, refer to the link below:
Hope this helps.
0 Comments
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!