pkg load optim

% create data to fit
X = linspace(-3,3,1e2);
Y = 2 + 3*X.^2 + 1*randn(size(X));
ERR = 0*X + 1;

% plot it
figure(1);clf;hold on;
errorbar(X,Y,ERR,'.');

% define model function
F = @(p,x) p.const + p.quad * x.^2;

% define start parameters
PIN = struct();
PIN.const = 1;
PIN.quad = 1;
PIN.dummy1 = 1;
PIN.dummy2 = 1;
   
% settings for nonlin_curvefit
SETTINGS = optimset(
   'weights',1./ERR,
   'f_pstruct',true
   ); 

% fit it!
[P, FY, CVG, OUTP] = nonlin_curvefit (F, PIN, X, Y, SETTINGS);

% additional settings for curvefit_stat
SETTINGS = optimset(SETTINGS,
   'objf_type','wls',
   'ret_covp',true
   ); 

% get covp
INFO = curvefit_stat (F, P, X, Y, SETTINGS);

% plot fit
plot(X,FY,'-r');  

% show covp
INFO.covp

