Write nonlinear constraint to fmincon

I am using fmincon to optimize a vectorized function (myfun) of five variables (x). This function has also some parameters (k, l, m, n) which I am passing to fmincon. What I am doing is shown below:
p=@(x)myfun(x, k, l, m, n);
[xmin, pmin]=fmincon(p, x0, A, b, Aeq, beq, lb, ub, nonlcon, options);
My question is how am I going to write the nonlinear equality which I have in my problem. I have written another function about the nonlinear equality in which x is the variables used in myfun and L is a parameter that I would like to pass inside the constraint. See below:
function [c,ceq]=pitch(x,L)
c=[];
ceq=x(3)*x(5)-L;
end
The problem is that I don't know how to write inside the fmincon the constraint. I have written this:
nonlcon=@(x)pitch(x,L)
which results in an error. I would appreciate your answers. Thank you.

 Accepted Answer

Thanks for the error, it appears that your objective function myfun takes 'x' as a 4x1 vector while your nonlinear constraint assumes that there is atleast x(5) in the line:
ceq=x(3)*x(5)-L;
that is atleast a 5x1 vector.
You might want to check that.

More Answers (4)

Attempted to access x(5); index out of bounds because numel(x)=4.
Error in power_fraction2 (line 7) Pf=(2*Rf*jmpp*x(2)^3*(x(3) - x(1))^2)/(3*x(3)*x(5)*x(3)*Vmpp*x(1));
Error in @(x)power_fraction2(x,jmpp,Vmpp,Rito,Rb,Rf,rc)
Error in fmincon (line 601) initVals.f = feval(funfcn{3},X,varargin{:});
Error in opt_power (line 48) [xmin, pmin]=fmincon(p, x0, A, b, Aeq, beq, lb, ub, nonlcon, options);
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Thank you Benji for the answer. This would make sense if I had not defined x(5) in the function that I would like to optimize. I am posting an equation included in myfun as an example.
Pf=(2*Rf*jmpp*x(2)^3*(x(3) - x(1))^2)/(3*x(3)*x(5)*x(3)*Vmpp*x(1));
So it is clearly stated in the function that vector x has 5 elements. What do you think?
I am sorry. Indeed I had a mistake in the boundaries. I had four elements instead of 5.
I would like one of the optimized variables to be an integer. Is there a way to achieve this with fmincon. In global optimization toolbox the Genetic Algorithm Solver can achieve that as far as I know. Am I correct or this can be done through fmincon?

3 Comments

Matt J
Matt J on 17 Jan 2013
Edited: Matt J on 17 Jan 2013
No, fmincon cannot do discrete optimization.
So Matt I have to use the Genetic Algorithm.
Giorgos, you are right this options is currently only available in Genetic Algorithm. Other optimization tools as yet dont support mixed integer programming.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!