function [outvalue]=welfare_objective2(x_opt,x_opt_name,oo_,options_,M_)
% function [outvalue]=welfare_objective(x_opt,x_opt_name,oo_,options_,M_)

global    

%% set parameter for use in Dynare
for ii=1:size(x_opt_name,1)
    M_.params(strmatch(x_opt_name{ii,1},M_.param_names,'exact'))=x_opt(ii);
end

if any(x_opt<cell2mat(x_opt_name(:,2))) || any(x_opt>cell2mat(x_opt_name(:,3))) %make sure parameters are inside their bounds
    outvalue=10e6+sum([x_opt].^2); %penalty function
    return
end

var_list_ = {'WT'};
[info, oo_] = stoch_simul(M_, options_, oo_, var_list_); %get decision rules and moments
if info(1) %filter out error code
    outvalue=1e5+sum([x_opt].^2);
    return;
end
outvalue=-oo_.mean(strmatch('WT',var_list_,'exact')); %extract Welfare