title: Matlab optimization fmincon actual function value from obj fn different from final value
body: <p>I have a Matlab optimization function, where the iterative function value evaluated last by the objective function is very different than the final function value. This is incorrect, see below the code for my question.</p>
  
  <p><em>globalvars.m</em></p>
  
  <pre><code>global sc_element_global;
  global B_j_global;
  % other global variables
  </code></pre>
  
  <p><em>sim_launcher.m</em></p>
  
  <pre><code>function sim_loop
      %loop
      sc_array={0.1,0.5,0.8};
      for sc_element_idx=1:length(sc_array)
          % Set 'x' as constant array 50
          sc_element=sc_array{sc_element_idx};
          find_network_productivity(sc_element,x)
      end
  end
  </code></pre>
  
  <p><em>find_network_productivity.m</em></p>
  
  <pre><code>%caller function to fmincon
  function [a, obj_f, x0, optimized_exit_flag]=find_network_productivity(sc_element,x)
  globalvars;    
          % Set global sc_element
          sc_element_global=sc_element;
  
          % Setting lb, ub, x0
          options = optimoptions(@fmincon,'Display','iter-detailed','DerivativeCheck','off','Diagnostics','on','Algorithm','sqp');
          [a, obj_f, optimized_exit_flag]=fmincon('objfunnwprod',x0,[],[],[],[],lb,ub,'confunnwprod',options);
          obj_f=-obj_f; % Because we want to find the maximum, not the minimum
          if optimized_exit_flag==-2
              optimized_exit_flag
          end
  
          obj_f   % print the final function value obj_f
  end
  </code></pre>
  
  <p><em>objfunnwprod.m</em></p>
  
  <pre><code>%OBJ FN
  function [f]=objfunnwprod(x)
  globalvars;
      f=B_j_global*x;
      f=-f % print the iteration function value f
  end
  </code></pre>
  
  <p><em>confunnwprod.m</em></p>
  
  <pre><code>%CONSTRAINT FN
  function [c, ceq]=confunnwprod(x)
  globalvars;
      for i=1:length(eNodeBs_global),
          B_j_global(i)=...
          calculate_blocking_probability_with_x(eNodeBs_global(i),x(i));
          c(i)=B_j_global(i)-eta_global(i);
      end
  
      ceq=[];
  end
  </code></pre>
  
  <p><em>calculate_blocking_probability_with_x.m</em></p>
  
  <pre><code>% function to calculate probability based on x
  function prob=calculate_blocking_probability_with_x(eNodeBs_global_var,x_var)
  globalvars;
      % based on sc_element_global and x_var, calculate probability
  
      prob=blocking_probability_with_x;
  end
  </code></pre>
  
  <p>Inputs:</p>
  
  <pre><code>% Inputs set
  x=[50 50 50 50 50 50];
  sc_array={0.1,0.5,0.8};
  
  % Output set
  sc_element=0.1
  x=50
  f=41.0172
  opt_f=39.09
  
  sc_element=0.5
  x=50
  f=770.43
  opt_f=39.09
  </code></pre>
  
  <p>Here I can see the <code>f</code> value is way different than the final <code>opt_f</code> value. It should have been closer to the final value. That is the expectation, but I don't see such a result.</p>
  
  <p><strong>Question</strong>: Why the function value, <code>f</code>, inside the iteration changes with <code>sc_element</code>, but the optimized function value <code>opt_f</code> remains the same?
  It looks like any change in <code>sc_element</code> does not have effect on <code>opt_f</code>, but internally <code>f</code> value is changed.</p>