<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174238</link>
    <title>MATLAB Central Newsreader - Constrained Optimization -fmincon</title>
    <description>Feed for thread: Constrained Optimization -fmincon</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 13 Aug 2008 01:04:02 -0400</pubDate>
      <title>Constrained Optimization -fmincon</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174238#448821</link>
      <author>Adriana </author>
      <description>Hi, I'm stuck in an Constrained Optimization problem.. I'm&lt;br&gt;
using the fmincon function with matrices...  and it's&lt;br&gt;
appearing this error:&lt;br&gt;
&lt;br&gt;
??? Error using ==&amp;gt; optimfcnchk at 287&lt;br&gt;
NONLCON must be a function.&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; fmincon at 302&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;[confcn, msg] =&lt;br&gt;
optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,gradconstflag,false,1);&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; Algorithm_1 at 92&lt;br&gt;
[x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);&lt;br&gt;
&lt;br&gt;
This is my code:&lt;br&gt;
&lt;br&gt;
global CA CB Z LAMBD miu&lt;br&gt;
global A F P x&lt;br&gt;
&lt;br&gt;
% Enter value of variables&lt;br&gt;
CA=input('Enter the value of C1:');&lt;br&gt;
CB=input('Enter the value of C2:');&lt;br&gt;
Z=input('Enter the value of Z:');&lt;br&gt;
n=input('Enter the number of states,n:');&lt;br&gt;
LAMBD=input('Enter the value of Lambda:');&lt;br&gt;
&lt;br&gt;
x0=LAMBD;&lt;br&gt;
lb=0.1;&lt;br&gt;
ub=0.9;&lt;br&gt;
CC=0.0001;&lt;br&gt;
&lt;br&gt;
disp('Enter &quot;1&quot;, if you want to work with a Tridiagonal&lt;br&gt;
Matrix:');&lt;br&gt;
disp('Enter &quot;2&quot;, if you want to work with a Hessenberg&lt;br&gt;
Matrix:');&lt;br&gt;
&lt;br&gt;
ToH=input('\n\n');&lt;br&gt;
%Creating a matrix A, and F and P vectors&lt;br&gt;
syms miu&lt;br&gt;
&lt;br&gt;
if ToH==1 % It is chosen a Tridiagonal matrix&lt;br&gt;
A=sym(zeros(n,n));&lt;br&gt;
A(1,1)=1-LAMBD;&lt;br&gt;
A(2,1)=LAMBD;&lt;br&gt;
A(n,n)=1-miu;&lt;br&gt;
A(n-1,n)=miu;&lt;br&gt;
for i=2:n-1&lt;br&gt;
for j=1:n&lt;br&gt;
if i==j&lt;br&gt;
A(j,i)=1-LAMBD-miu;&lt;br&gt;
elseif j==i-1&lt;br&gt;
A(j,i)=miu;&lt;br&gt;
elseif j==i+1&lt;br&gt;
A(j,i)=LAMBD;&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
elseif ToH==2   %It is chosen a Hessenberg matrix&lt;br&gt;
A=sym(zeros(n,n));&lt;br&gt;
for i=1:n&lt;br&gt;
for j=1:n&lt;br&gt;
if (i==j &amp;&amp; j~=n &amp;&amp; j~=1)&lt;br&gt;
A(j,i)=1-LAMBD-(j-1)*miu;&lt;br&gt;
elseif (i==n &amp;&amp; j==n)&lt;br&gt;
A(j,i)=1-(n-1)*miu;&lt;br&gt;
elseif (i==1 &amp;&amp; j==1)&lt;br&gt;
A(j,i)=1-LAMBD;&lt;br&gt;
elseif j==i+1&lt;br&gt;
A(j,i)=LAMBD;&lt;br&gt;
elseif j&amp;lt;i&lt;br&gt;
A(j,i)=miu;&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
else   %If &quot;1&quot; or &quot;2&quot; it's not the option chosen&lt;br&gt;
disp('***********NOT VALID OPTION***********');&lt;br&gt;
return&lt;br&gt;
end&lt;br&gt;
AA=vpa(A);&lt;br&gt;
P=zeros(n,1);&lt;br&gt;
F=zeros(1,n);&lt;br&gt;
for i=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;F(i)=i-1;&lt;br&gt;
if i==1&lt;br&gt;
P(i,1)=1;&lt;br&gt;
else P(i,1)=0;&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
A;&lt;br&gt;
P;&lt;br&gt;
F;&lt;br&gt;
&lt;br&gt;
a=1;    % Storage Variable&lt;br&gt;
t1=clock;   %Initial vector for calculating the time&lt;br&gt;
optimizacion&lt;br&gt;
err=CC+1;   % Assignment of error to start the optimization&lt;br&gt;
&lt;br&gt;
while (err&amp;gt;CC)   % Minimizing&lt;br&gt;
&lt;br&gt;
[x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);&lt;br&gt;
clc&lt;br&gt;
&lt;br&gt;
vxg(a,1)=x;   % storage the xmin&lt;br&gt;
vxg(a,2)=feval; % storage the g(x)min&lt;br&gt;
&lt;br&gt;
x=miu;&lt;br&gt;
miu=vxg(a,1);&lt;br&gt;
&lt;br&gt;
A1=eval(A);   % It's evaluated matrix A with the value of x&lt;br&gt;
obtained&lt;br&gt;
P=A1*P;   % Matrix A is multiplied by P vector&lt;br&gt;
&lt;br&gt;
miu=sym('miu');&lt;br&gt;
if a&amp;gt;=2   % if it's optimizated two or more times&lt;br&gt;
err=abs(vxg(a,1)-vxg(a-1,1)); % Error calculation&lt;br&gt;
if err&amp;lt;0.0000000001   %If the error is very small&lt;br&gt;
err=1; %it's assigned as 1&lt;br&gt;
end&lt;br&gt;
else err=CC+1;   % it's optimizated only one time&lt;br&gt;
end&lt;br&gt;
a=a+1;&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
t2=clock;   % Vector 2 to calculate the optimization&lt;br&gt;
tp=t2-t1;   % Calculating time of optimization&lt;br&gt;
time=tp(1,4)*3600+tp(1,5)*60+tp(1,6);&lt;br&gt;
&lt;br&gt;
Funn.m&lt;br&gt;
&lt;br&gt;
%Objective Function&lt;br&gt;
function g=funn(x,CA,CB,F,P)&lt;br&gt;
global M miu &lt;br&gt;
x=miu;&lt;br&gt;
M=((AA).^Z);&lt;br&gt;
g=(CA*x+CB*(P*(F*M)));&lt;br&gt;
&lt;br&gt;
nonlin1.m&lt;br&gt;
% Nonlinear Constraint&lt;br&gt;
function [c,ceq]=nonlin1(x,LAMBD)&lt;br&gt;
x=miu;&lt;br&gt;
c=(LAMBD/x)-1;&lt;br&gt;
ceq=[];&lt;br&gt;
&lt;br&gt;
I appreciated if somebody could help me..I'm a newer user in&lt;br&gt;
Matlab...&lt;br&gt;
&lt;br&gt;
Thanks..&lt;br&gt;
&lt;br&gt;
Adriana</description>
    </item>
    <item>
      <pubDate>Wed, 13 Aug 2008 03:20:18 -0400</pubDate>
      <title>Re: Constrained Optimization -fmincon</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174238#448829</link>
      <author>vedenev</author>
      <description>Strange thing in your code for function:&lt;br&gt;
x=miu;&lt;br&gt;
&lt;br&gt;
whay you set x input argument to some constant value mui?&lt;br&gt;
&lt;br&gt;
------------------------------------&lt;br&gt;
Maxim Vedenev, custom matlab coder&lt;br&gt;
&lt;a href=&quot;http://simulations.narod.ru/&quot;&gt;http://simulations.narod.ru/&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Wed, 13 Aug 2008 14:03:38 -0400</pubDate>
      <title>Re: Constrained Optimization -fmincon</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/174238#448907</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Adriana &quot; &amp;lt;cocaalvarado@hotmail.com&amp;gt; wrote in message &lt;br&gt;
news:g7tbu2$3ah$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hi, I'm stuck in an Constrained Optimization problem.. I'm&lt;br&gt;
&amp;gt; using the fmincon function with matrices...  and it's&lt;br&gt;
&amp;gt; appearing this error:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ??? Error using ==&amp;gt; optimfcnchk at 287&lt;br&gt;
&amp;gt; NONLCON must be a function.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Error in ==&amp;gt; fmincon at 302&lt;br&gt;
&amp;gt;   [confcn, msg] =&lt;br&gt;
&amp;gt; optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,gradconstflag,false,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Error in ==&amp;gt; Algorithm_1 at 92&lt;br&gt;
&amp;gt; [x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);&lt;br&gt;
&lt;br&gt;
Looking at the calling signature for FMINCON:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fmincon.html?BB=1&quot;&gt;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fmincon.html?BB=1&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
the longest is:&lt;br&gt;
&lt;br&gt;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)&lt;br&gt;
&lt;br&gt;
You have two too many empty matrices in your calling syntax.  lb is being &lt;br&gt;
treated as the nonlcon input, and it's not a function handle so the helper &lt;br&gt;
function that validates the input arguments correctly errors.&lt;br&gt;
&lt;br&gt;
&amp;gt; This is my code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; global CA CB Z LAMBD miu&lt;br&gt;
&amp;gt; global A F P x&lt;br&gt;
&lt;br&gt;
You shouldn't use global variables.  They can cause problems that are &lt;br&gt;
difficult to debug, and you run the risk of another function that you call &lt;br&gt;
changing the values of the global variables without you realizing it.  Use &lt;br&gt;
the techniques described in the documentation pages linked in question 4.13 &lt;br&gt;
in the newsgroup FAQ instead:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; if ToH==1 % It is chosen a Tridiagonal matrix&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
You might want to look at the DIAG function to help you avoid using so many &lt;br&gt;
loops to define your A matrix.&lt;br&gt;
&lt;br&gt;
&amp;gt; elseif ToH==2   %It is chosen a Hessenberg matrix&lt;br&gt;
&lt;br&gt;
DIAG may be of use here as well.&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; t2=clock;   % Vector 2 to calculate the optimization&lt;br&gt;
&amp;gt; tp=t2-t1;   % Calculating time of optimization&lt;br&gt;
&amp;gt; time=tp(1,4)*3600+tp(1,5)*60+tp(1,6);&lt;br&gt;
&lt;br&gt;
You can use the ETIME function, or TIC/TOC, or CPUTIME to calculation the &lt;br&gt;
duration of your optimization instead of manually computing it as you're &lt;br&gt;
doing.&lt;br&gt;
&lt;br&gt;
&amp;gt; Funn.m&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; %Objective Function&lt;br&gt;
&amp;gt; function g=funn(x,CA,CB,F,P)&lt;br&gt;
&amp;gt; global M miu&lt;br&gt;
&amp;gt; x=miu;&lt;br&gt;
&lt;br&gt;
As Maxim said, your function accepts the x input argument that FMINCON is &lt;br&gt;
trying to optimize and promptly overwrite it with the contents of a global &lt;br&gt;
variable.  This means that even if you correct your FMINCON call's syntax, &lt;br&gt;
this will not work.&lt;br&gt;
&lt;br&gt;
&amp;gt; M=((AA).^Z);&lt;br&gt;
&lt;br&gt;
You never define either AA or Z in this function, and you have not declared &lt;br&gt;
them global, so this line of code will error.  If you want to use a global &lt;br&gt;
variable, you need to define it as global in _each and every function in &lt;br&gt;
which you want to use it._&lt;br&gt;
&lt;br&gt;
&amp;gt; g=(CA*x+CB*(P*(F*M)));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; nonlin1.m&lt;br&gt;
&amp;gt; % Nonlinear Constraint&lt;br&gt;
&amp;gt; function [c,ceq]=nonlin1(x,LAMBD)&lt;br&gt;
&amp;gt; x=miu;&lt;br&gt;
&lt;br&gt;
The same problems as above occur here -- you never define miu, and even if &lt;br&gt;
you did declare it as global you're overwriting the value at which FMINCON &lt;br&gt;
is evaluating your function with the value of that global variable.  It &lt;br&gt;
looks like you're trying to manage the variable over which you're optimizing &lt;br&gt;
manually using global variables.  You don't need to do that, let FMINCON &lt;br&gt;
handle it.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
  </channel>
</rss>

