Thread Subject: Constrained Optimization -fmincon

Subject: Constrained Optimization -fmincon

From: Adriana

Date: 13 Aug, 2008 01:04:02

Message: 1 of 3

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);

This is my code:

global CA CB Z LAMBD miu
global A F P x

% Enter value of variables
CA=input('Enter the value of C1:');
CB=input('Enter the value of C2:');
Z=input('Enter the value of Z:');
n=input('Enter the number of states,n:');
LAMBD=input('Enter the value of Lambda:');

x0=LAMBD;
lb=0.1;
ub=0.9;
CC=0.0001;

disp('Enter "1", if you want to work with a Tridiagonal
Matrix:');
disp('Enter "2", if you want to work with a Hessenberg
Matrix:');

ToH=input('\n\n');
%Creating a matrix A, and F and P vectors
syms miu

if ToH==1 % It is chosen a Tridiagonal matrix
A=sym(zeros(n,n));
A(1,1)=1-LAMBD;
A(2,1)=LAMBD;
A(n,n)=1-miu;
A(n-1,n)=miu;
for i=2:n-1
for j=1:n
if i==j
A(j,i)=1-LAMBD-miu;
elseif j==i-1
A(j,i)=miu;
elseif j==i+1
A(j,i)=LAMBD;
end
end
end

elseif ToH==2 %It is chosen a Hessenberg matrix
A=sym(zeros(n,n));
for i=1:n
for j=1:n
if (i==j && j~=n && j~=1)
A(j,i)=1-LAMBD-(j-1)*miu;
elseif (i==n && j==n)
A(j,i)=1-(n-1)*miu;
elseif (i==1 && j==1)
A(j,i)=1-LAMBD;
elseif j==i+1
A(j,i)=LAMBD;
elseif j<i
A(j,i)=miu;
end
end
end

else %If "1" or "2" it's not the option chosen
disp('***********NOT VALID OPTION***********');
return
end
AA=vpa(A);
P=zeros(n,1);
F=zeros(1,n);
for i=1:n
    F(i)=i-1;
if i==1
P(i,1)=1;
else P(i,1)=0;
end
end

A;
P;
F;

a=1; % Storage Variable
t1=clock; %Initial vector for calculating the time
optimizacion
err=CC+1; % Assignment of error to start the optimization

while (err>CC) % Minimizing

[x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);
clc

vxg(a,1)=x; % storage the xmin
vxg(a,2)=feval; % storage the g(x)min

x=miu;
miu=vxg(a,1);

A1=eval(A); % It's evaluated matrix A with the value of x
obtained
P=A1*P; % Matrix A is multiplied by P vector

miu=sym('miu');
if a>=2 % if it's optimizated two or more times
err=abs(vxg(a,1)-vxg(a-1,1)); % Error calculation
if err<0.0000000001 %If the error is very small
err=1; %it's assigned as 1
end
else err=CC+1; % it's optimizated only one time
end
a=a+1;
end

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);

Funn.m

%Objective Function
function g=funn(x,CA,CB,F,P)
global M miu
x=miu;
M=((AA).^Z);
g=(CA*x+CB*(P*(F*M)));

nonlin1.m
% Nonlinear Constraint
function [c,ceq]=nonlin1(x,LAMBD)
x=miu;
c=(LAMBD/x)-1;
ceq=[];

I appreciated if somebody could help me..I'm a newer user in
Matlab...

Thanks..

Adriana

Subject: Constrained Optimization -fmincon

From: vedenev

Date: 13 Aug, 2008 03:20:18

Message: 2 of 3

Strange thing in your code for function:
x=miu;

whay you set x input argument to some constant value mui?

------------------------------------
Maxim Vedenev, custom matlab coder
http://simulations.narod.ru/

Subject: Constrained Optimization -fmincon

From: Steven Lord

Date: 13 Aug, 2008 14:03:38

Message: 3 of 3


"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


Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com