title: How to solve the "Array indices must be positive integers or logical values" when using optimization toolbox?
body: <p>I am trying to implement this optimization problem through optimization toolbox:</p>
  <blockquote>
  <p><a href="https://i.stack.imgur.com/MAr9U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MAr9U.png" alt="enter image description here" /></a></p>
  </blockquote>
  <p>where <code>N = 60</code> ﬁles and <code>K = 130</code> users when <code>1/K ≤ M ≤ t∗N/K</code>, for which <code>t∗ =3</code>.
  so I wrote this code:</p>
  <pre><code> clear all; 
      close all; 
      clc;
      N=60;
      t=3;
      K=130;
      for M=0:0.1:1.4
          r=zeros(size(1:M));
          f=le(1/K,M);
          c=le(M,3*N/K);
          if f || c
          R2 = @(s) -(s-(s./ floor(N./s)).*M);
          LB = 1;
          UB = min(N, K);
          options = optimoptions('fmincon','Algorithm','interior-point'); % run interior-point algorithm
          [sopt, ropt] = fmincon(R2,1,[],[],[],[],LB,UB,[],options); 
          r(M) = -ropt;
          end 
      end 
      plot(0:1.4,r(M),'r-','LineWidth',2);
      xlabel('Cache Capacity (M)');
      ylabel('Delivery Rate (R)');
  </code></pre>
  <p>However the output should be as shown in the graph the one of cut-set bound</p>
  <blockquote>
  <p><a href="https://i.stack.imgur.com/zBJ8Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zBJ8Z.png" alt="enter image description here" /></a></p>
  </blockquote>
  <h1>The error is as follows:</h1>
  <blockquote>
  <p>Array indices must be positive integers or logical values.</p>
  <p>Error in try (line 17)
  <code>r(M) = -ropt</code>;</p>
  </blockquote>