function [obj] = Objfn(theta,empMom,shockMat,dyn)

global oo_ M_ options_ y_% Dynare structures to be pass to resol.m

%setting the parameters

set_param_value('BETTA',theta(1));
set_param_value('GAM',theta(2));
set_param_value('SIG',theta(3));
set_param_value('SIGZ',theta(4));

[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); %run model solution in Dynare
ystart = oo_.dr.ys(oo_.dr.order_var,1);
ss = ystart;

[err, y_] = dynare_simul_(3, M_.nstatic,M_.npred,M_.nboth,M_.nfwrd,M_.exo_nbr, ...
                          ystart,shockMat',M_.Sigma_e,options_.DynareRandomStreams.seed,ss, ...
                          oo_.dr.g_0, oo_.dr.g_1, oo_.dr.g_2, oo_.dr.g_3);

% this is just for demonstration purpose
y= reshape(y_(dyn.y,:), [12, 1250]);
dy = diff(log(sum(exp(y),1)));
c= reshape(y_(dyn.c,:), [12, 1250]);
dc = diff(log(sum(exp(c),1)));

empSmom = [mean(dy);var(dy);mean(dc);var(dc)]
dif = (empSmom - empMom);
obj = dif'* dif
        
end 