function z_star = run_opt(f_name, n)
% Function to run a finite dimensional optimization problem
% based on a discretization of a Mayer problem in optimal control.

% f_name points to a user-supplied function with a single input argument
% n is a discretization parameter. The finite-dimensional problem arises 
% by treating the (scalar) control as piecewise constant
% The function referenced by f_name must define the elements of
% the underlying optimal control problem. See 'zermelo' as an example.

%% Problem data

   PAR = feval(str2func(f_name), n);
   
% some lines omitted

%% Algorithm set up
   OPT = optimset(optimset('fmincon'), ...
                       'LargeScale','off', ...
                       'Algorithm', 'active-set', ...
                       'Display' , 'iter', ...
                       'UseParallel', 'Always');   
   h_cost = @(z) general_cost( z, PAR);
   h_cnst = @(z) general_constraint( z, PAR);
   
%% Run the algorithm
    [z_star, f_star, exit] = ...
                  fmincon(h_cost, z0, [], [], [], [], LB, UB, h_cnst, OPT);            
  if exit >=0 && isfield(PAR, 'plot')
    feval(PAR.plot, z_star, PAR)
  end
   