function [est,var1,var2,beta]=control2(f,g,intg,n)
%[est,var1,var2]=control(f,g,intg,n) 
%runs a simulation on the function f using control variate g  n times.
% intg is the integral of g--uses second version of control variate.
%outputs estimator and variances with and without control variate.
U=rand(1,n);
FN=eval(strcat(f,'(U)'));
CN=eval(strcat(g,'(U)'));
beta=cov(FN',CN');
beta=beta(1,2)/beta(2,2);
e=beta*intg+FN-beta*CN;
var1=var(FN);
var2=var(e);
est=mean(e);

