function [f,g,op]=fun2(par,seed)
% like fun only this is for calibrating nig (replace "fun" in
% noisyminimize)    input three parameters  par=[log(alpha-abs(1+beta)), beta,delta]
 % f gives the value of the option at the 10 strike prices, and the columns
 % of g give the values of the gradient for the same options with respect
 % to the three parameters.
 % note: in the NIG, alpha>|beta|  and delta>0 so put beta=par(2),
 % alpha=abs(par(2))+exp(par(1)) and delta=exp(par(3));
 nsim=10000; 
ca=[173.1000  124.8000   88.5000   53.7000   39.3000   27.3000 17.8500 10.9500  6.2000   3.2500]; % observed prices
K=[950  1005 1050  1100  1125  1150   1175 1200  1225 1250]; % strike prices
no=length(K);  % no=number of options.
T=.3699; S0=1116.84;   % maturity and initial value
    alpha=abs(par(2))+exp(par(1));
    op=nigcall(S0,K,.01,T,alpha,par(2),exp(par(3)),nsim,seed);
    g= nigcall(S0,K,.01,T,abs(par(2))+exp(par(1)+.01),par(2),exp(par(3)),nsim,seed)-op;   % increments=.05
    g=[g ;nigcall(S0,K,.01,T,abs(par(2)+.01)+exp(par(1)),par(2)+.01,exp(par(3)),nsim,seed)-op];
    %g=100*g;
    g=100*[g ;nigcall(S0,K,.01,T,abs(par(2))+exp(par(1)),par(2),exp(par(3)+.01),nsim,seed)-op]; 
    f=sum((op-ca).^2) ; % this is the objective function we want to minimize
    g=2*(op-ca)*g';  % this is the estimate of gradient of the objective function-not adjusted fo covariance.
