function [H,dH]=hfunc(theta);
x=xgmm;
delta=theta(1); thet=theta(2); gamma=theta(3); lambda=theta(4);
sige=theta(5); gbar=theta(6); rho=theta(7); sigm=theta(8);
beta=1.03^(-.25);
e=ones(112,1);
lt=(log(x(:,5))-(1-thet)*log(x(:,12))-thet*log(x(:,11)))/(1-thet);
gbt=x(:,13).*(x(:,17).^(thet/(1-thet))).*x(:,7);
gbtl=x(:,15).*(x(:,18).^(thet/(1-thet))).*x(:,19);
H(:,1)=delta-(1+x(:,10)-x(:,3));
H(:,2)=x(:,1)/beta-thet*x(:,4)-1+delta;
H(:,3)=gamma-(1-thet)*x(:,6)./x(:,7);
H(:,4)=lt-lambda;
H(:,5)=lt.^2-2*lambda*lt+lambda^2-sige^2;
H(:,6)=lambda-log( x(:,5).*(e./x(:,6)-x(:,13))./  ..
       (x(:,14)-x(:,15)) );
H(:,7)=lambda-log(x(:,5));
H(:,8)=lambda-log(x(:,3));
H(:,9)=lambda-log(x(:,9));
H(:,10)=log(gbt)-(1-rho)*log(gbar)-rho*log(gbtl);
H(:,11)=(log(gbt)-(1-rho)*log(gbar)-rho*log(gbtl)).*log(gbtl);
H(:,12)=(log(gbt)-(1-rho)*log(gbar)-rho*log(gbtl)).^2-sigm^2;
if nargout>1;
  dH=zeros(112,96);
  dH(:,1)=e;
  dH(:,9)=e; dH(:,10)=-x(:,4);
  dH(:,18)=x(:,6)./x(:,7); dH(:,19)=e;
  dH(:,28)=-e;
  dH(:,36)=-2*lt+2*lambda*e; dH(:,37)=-2*sige*e;
  dH(:,44)=e;
  dH(:,52)=e;
  dH(:,60)=e;
  dH(:,68)=e;
  dH(:,78)=-(1-rho)/gbar*e;  dH(:,79)=log(gbar)*e-log(gbtl);
  dH(:,86)=dH(:,78).*log(gbtl); dH(:,87)=dH(:,79).*log(gbtl);
  dH(:,94)=2*(log(gbt)-(1-rho)*log(gbar)-rho*log(gbtl)).*dH(:,78);
  dH(:,95)=2*(log(gbt)-(1-rho)*log(gbar)-rho*log(gbtl)).*dH(:,79);
  dH(:,96)=-2*sigm*e;
end;
