function val = getOptim(theta, obj,Y,WN,mats)
% trace = 0;
% if trace > 0
% if (trace>0) {
%         cat('Starting optimization...\n')
%         cat('Function value at starting point = ',
%         sprintf("%10.4f", obj(theta, ...)), '\n')
%     }
i = 1;
improvement = inf ;
prev_llk = 0 ;
% myparscale = 10.^round(log10(abs(theta))) ;



while improvement > 0.0001
%     options = optimoptions('fminunc','Algorithm','quasi-newton','TypicalX',myparscale);
%     options = optimoptions('fminsearch','TypicalX',myparscale);
%     options = optimoptions('fminunc','Algorithm','trust-region','HessianFcn',[],'TypicalX',myparscale);
%     problem.options = options;
%     problem.x0 = theta ;
%     problem.objective = obj ;
%     problem.solver = 'fminunc';
%     problem.solver = 'fminsearch';
%     res =  fminunc(problem);
%     options = optimset('MaxIter',500,'TolX', 1e-8);
%    res = fminsearch(obj, theta) ; 
%      up = theta + 10*myparscale ; 
%     lp = theta - 10*myparscale ;
%     options = optimoptions('fminunc','Display','iter','MaxIterations',500,'StepTolerance',1e-4,'TypicalX',myparscale);
%     problem.options = options;
%     problem.x0 = theta ;
%     problem.objective = obj ;
%     problem.solver = 'fminunc';
%     res =  fminunc(problem);
%     res =  fmincon(obj,theta,[],[],[],[],lp,up,[],options);
%     res =  fmincon(obj,theta,[],[],[],[],lp,up);
      
     % options = optimset('Display', 'iter','MaxIter',100);
     f = @(x,Y,W,mats)obj(x,Y,WN,mats) ;
     fun = @(x)f(x,Y,WN,mats) ;
     options = optimset('Display', 'off','MaxIter',100);
     res =  fminsearch(fun,theta, options) ;
     improvement = abs(fun(res) - prev_llk) ;
     prev_llk  = fun(res) ;
     theta = res ;
%      if (trace > 0)
%              cat('iteration ', i,', likelihood = ', sprintf("%10.4f", res$value),'\n')
    i = i+1;
end

% options = optimset('Display', 'iter','MaxIter',50000);
options = optimset('Display', 'off','MaxIter',50000);
res =  fminsearch(fun,theta, options) ;

% if (trace > 0)
%         cat('improvement = ', improvement, ' -- proceed to final step\n')
% options = optimoptions('fminunc','Algorithm','quasi-newton','TypicalX',myparscale,'MaxIterations',50000);
%options = optimoptions('fminsearch','TypicalX',myparscale,'MaxIterations',50000);
% options = optimoptions('fminunc','Algorithm','trust-region','HessianFcn',[],'TypicalX',myparscale,'MaxIterations',50000);
%problem.options = options;
%problem.x0 = theta ;
%problem.objective = obj ;
%problem.solver = 'fminunc';
%problem.solver = 'fminsearch';
%res =  fminunc(problem);
%  options = optimset('MaxIter',50000);
% res =  fminsearch(obj,theta, options);
%  res = fminunc(obj,theta) ;
 
% res =  fminsearch(obj,theta);

% up = theta + myparscale ; 
% lp = theta - myparscale ;
% res = fmincon(obj,theta,[],[],[],[],lp,up);

% if (trace > 0) {
%         cat('final Nelder-Mead step, likelihood = ',
%             sprintf("%10.4f", res$value),'\n')
%         cat('Convergence:', res$convergence, "\n")
%         print(res$message)
%     }

val = res ;
end