Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Constrained Optimization -fmincon
Date: Wed, 13 Aug 2008 10:03:38 -0400
Organization: The MathWorks, Inc.
Lines: 103
Message-ID: <g7upjr$l9t$1@fred.mathworks.com>
References: <g7tbu2$3ah$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1218636219 21821 144.212.105.187 (13 Aug 2008 14:03:39 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 13 Aug 2008 14:03:39 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
Xref: news.mathworks.com comp.soft-sys.matlab:485263




"Adriana " <cocaalvarado@hotmail.com> wrote in message 
news:g7tbu2$3ah$1@fred.mathworks.com...
> Hi, I'm stuck in an Constrained Optimization problem.. I'm
> using the fmincon function with matrices...  and it's
> appearing this error:
>
> ??? Error using ==> optimfcnchk at 287
> NONLCON must be a function.
>
> Error in ==> fmincon at 302
>   [confcn, msg] =
> optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,gradconstflag,false,1);
>
> Error in ==> Algorithm_1 at 92
> [x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);

Looking at the calling signature for FMINCON:

http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fmincon.html?BB=1

the longest is:

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)

You have two too many empty matrices in your calling syntax.  lb is being 
treated as the nonlcon input, and it's not a function handle so the helper 
function that validates the input arguments correctly errors.

> This is my code:
>
> global CA CB Z LAMBD miu
> global A F P x

You shouldn't use global variables.  They can cause problems that are 
difficult to debug, and you run the risk of another function that you call 
changing the values of the global variables without you realizing it.  Use 
the techniques described in the documentation pages linked in question 4.13 
in the newsgroup FAQ instead:

http://matlabwiki.mathworks.com/MATLAB_FAQ

*snip*

> if ToH==1 % It is chosen a Tridiagonal matrix

*snip*

You might want to look at the DIAG function to help you avoid using so many 
loops to define your A matrix.

> elseif ToH==2   %It is chosen a Hessenberg matrix

DIAG may be of use here as well.

*snip*

> t2=clock;   % Vector 2 to calculate the optimization
> tp=t2-t1;   % Calculating time of optimization
> time=tp(1,4)*3600+tp(1,5)*60+tp(1,6);

You can use the ETIME function, or TIC/TOC, or CPUTIME to calculation the 
duration of your optimization instead of manually computing it as you're 
doing.

> Funn.m
>
> %Objective Function
> function g=funn(x,CA,CB,F,P)
> global M miu
> x=miu;

As Maxim said, your function accepts the x input argument that FMINCON is 
trying to optimize and promptly overwrite it with the contents of a global 
variable.  This means that even if you correct your FMINCON call's syntax, 
this will not work.

> M=((AA).^Z);

You never define either AA or Z in this function, and you have not declared 
them global, so this line of code will error.  If you want to use a global 
variable, you need to define it as global in _each and every function in 
which you want to use it._

> g=(CA*x+CB*(P*(F*M)));
>
> nonlin1.m
> % Nonlinear Constraint
> function [c,ceq]=nonlin1(x,LAMBD)
> x=miu;

The same problems as above occur here -- you never define miu, and even if 
you did declare it as global you're overwriting the value at which FMINCON 
is evaluating your function with the value of that global variable.  It 
looks like you're trying to manage the variable over which you're optimizing 
manually using global variables.  You don't need to do that, let FMINCON 
handle it.

-- 
Steve Lord
slord@mathworks.com