function objective_value = objective(params,paramlabels,upperbound,lowerbound,modfilename,objective_label,shocks_labels)

global M_ oo_



newparams = min(max(params,lowerbound),upperbound);
nparams = size(paramlabels,1);
for indx = 1:nparams
    eval([paramlabels(indx,:),'=newparams(indx);']) 
end

%save parameters to disk 
paramline = strmerge(cellstr(paramlabels),' ');
eval(['save param_overwrite ',paramline])

try  % here to impose penalty if the BK conditions are not satisfied

eval(modfilename)

order = 2;

nshocks = size(shocks_labels,1);
this_shock = zeros(nshocks,1);
s1 = mymkdata2(M_,oo_,1,order,shocks_labels,this_shock(:,end),zeros(M_.endo_nbr,1),zeros(M_.endo_nbr,1),objective_label);

objective_value = nan;

% change sign of objective since we are minimizing instead of maximizing
eval(['objective_value = -s1.',objective_label,'_irf(end);'])

%impose penalty

if max(abs(newparams-params))>1e-10
   objective_value = objective_value+1e10; 
end

catch 
     objective_value = 1e10;
end
