function coeff = funcGN(coeff,polyN,poly, h,w,im,b, hu,hfull, K, nsmp, t_exposure,t_readout)

Kmat0 = gen_conv_mat(h,w,hu,hfull,K, coeff,polyN,poly, t_exposure,t_readout, nsmp);

N = numel(coeff);
% residual
r = [];
% jacobian
J = [];

Kmat = cell(1,N);
eps = 1e-8;
parfor i = 1:N
%     fprintf('%d ',i);
    dcoeff = zeros(1,N); dcoeff(i) = eps;
    Kmat{i} = gen_conv_mat(h,w,hu,hfull,K, coeff+dcoeff,polyN,poly, t_exposure,t_readout, nsmp);
end
fprintf('\n');

% br = 0.05;
padding = 1-hu;
for i = 1:size(im,2)
    Icur = reshape(im(:,i),h,w);
    bcur = reshape(b(:,i),h,w);
    Icurk = reshape(Kmat0*Icur(:),h,w);
%     rcur = ignore_border(Icurk-bcur,br);
    rcurp = Icurk-bcur;
    rcur = rcurp(padding+1:end-padding,padding+1:end-padding);
    r = [r; rcur(:)];

    j = zeros((h-2*padding)*(w-2*padding),N);
    parfor ix = 1:N
        tmp = (reshape(Kmat{ix}*Icur(:),h,w) - Icurk)./eps;
%         tmp = ignore_border(tmp,br);
        tmp = tmp(padding+1:end-padding,padding+1:end-padding);
        j(:,ix) = tmp(:);
    end
    J = [J; j];
end
% norm(r(:))
% save('numapprox.mat','r','J');
delta = ((J'*J+0) \ (-J'*r))'; % 10 is for 10\|delta\|_2^2 -- 09-Oct-2014
coeff = coeff+ 1*delta;