title: Matlab fmincon Not Yielding Global Maximum/Minimum
body: <p>I am new to matlab, apologies if the question is silly. I am using the <code>fmincon</code>  function to derive the elements of a <img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=m%20%5Ctimes%20n" alt="m x n"> matrix (X) which will maximize the following objective function subject to the non-negativity constraint and:</p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=sum(x_%7Bij%7D)%20%3C%20100" alt="sum(x_{ij}) &lt; 100"></p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=%5C%7B%20%7Bf(x_%7B1%2C1%7D)%20%2B%20(f(x_%7B1%2C2%7D)%20%2B%20...%20%2B%20f(x_%7B1%2Cn%7D))%7D%20%5C%7D%20%2B%20%5Cbeta%20*%20%5C%7B%20%7Bf(x_%7B2%2C1%7D)%2B(f(x_%7B2%2C2%7D)%2B...%2Bf(x_%7B2%2Cn%7D))%7D%20%5C%7D%20%2B%20...%20%2B%20%20%5Cbeta%20%5E%7B(m-1)%7D%20*%20%5C%7Bf(x_%7Bm%2C1%7D)%2B(f(x_%7Bm%2C2%7D)%2B...%2Bf(x_%7Bm%2Cn%7D))%20%5C%7D" alt="{f(x(1,1))+(f(x(1,2))+...+f(x(1,n))} + Beta*{f(x(2,1))+(f(x(2,2))+...+f(x(2,n))}+...+(Beta^(m-1))*{f(x(m,1))+(f(x(m,2))+...+f(x(m,n))}"></p>
  
  <p>where</p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=f(x_%7Bij%7D)%3D%5Czeta%20*%20%5B((%5Calpha*p_%7Bf%7D*x_%7Bij%7D%5E%7B%5Cdelta%7D)-(p_%7Bw%7D*x_%7Bij%7D))%5E%7B%5Cgamma%7D%5D" alt="f(xij)=zeta*[((alpha*pf*(xij^delta))-(pw*xij))^gamma]"></p>
  
  <p>In the code I used to do this <code>fmincon</code> was used, as the objective function is non-linear:</p>
  
  <pre><code>function optim = optim(m,n)
  A= ones(1,m*n);
  b = 100;
  z = zeros(m,n);
  in = inf(m,n);
  X = ones(m,n);
  
  [x, bestval] = fmincon(@myfun2,X,A,b,[],[],z,in,[])
  
    function f = myfun2(x)
    Alpha = 5;
    %kappa = 5;
    zeta = 5;
    beta = 0.90909;
    delta = 0.4;
    gamma = 0.4;
    pf = 1;
    pw = 1;
  
    for i= 1:m
       f=0;
       sum(i)=0;
          for j=1:n
          sum(i) = sum(i) +((beta^(i-1))*(-1)*(zeta)*(((Alpha*pf.*((x(i,j)).^delta))- 
                   (pw.*x(i,j)))^gamma));
  
           end
       f = f+sum(i)
     end
  
   end
  end
  </code></pre>
  
  <p>When the code was run for a 5x5 matrix (optim(5,5)), the resulting solution was
  x =</p>
  
  <pre><code>1.5439    1.5439    1.5439    1.5439    1.5439
  1.5439    1.5439    1.5439    1.5439    1.5439
  1.5439    1.5439    1.5439    1.5439    1.5439
  1.5439    1.5439    1.5439    1.5439    1.5439
  3.1748    3.1748    3.1748    3.1748    3.1748
  
  
  bestval =
  
  -31.8780 
  </code></pre>
  
  <p>But this is not a global minimum - as the marginal conditions specify at the global minimum we would have (for 1st row):</p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=f%27(x_%7B1%2C1%7D)%3D...%3Df%27(x_%7B1%2Cn%7D)" alt="f&#39;(x11)=...=f&#39;(x1n)"></p>
  
  <p>And so on for each row. Also we would have for each column:</p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=f%27(x_%7B1%2C1%7D)%3D%5Cbeta*f%27(x_%7B2%2C1%7D)%3D...%3D%5Cbeta%5E%7B(m-1)%7D*f%27(x_%7Bm%2C1%7D)" alt="f&#39;(x11)=beta*f&#39;(x21)=...=(beta^m-1)*f&#39;(xm1)"></p>
  
  <p>For the first column and so on. None of these conditions are satisfied by the resulting matrix. I have looked at the the related questions in stack overflow and also the documentation and I have no inkling of how to get better results. Is there a problem with the code? Can the code be tweaked to get better results?</p>
  
  <p>Can I make the marginal conditions the stopping conditions and how could i go about doing that. or Can I use the Jacobian in some way? Any help would be appreciated. Thank You.</p>