%% built in fit function usage example

% load initial data file
data=load('data_to_fit.dat');
x=data(:,1); % 1st column is x
y=data(:,2); % 2nd column is y

% define fitting function
% notice that it quite human readable
% Matlab automatically treat x as independent variable
f=fittype(@(A,x0,gamma, x) A ./ (1 +((x-x0)/gamma).^2) )

% let's see did Matlab guessed fit parameters right
coeffs = coeffnames(f)

% assign initial guess
pin=[3,3,1]; % [A, x0, gamma]

% We fit our data here
[fitobject,gof] = fit (x,y, f, 'StartPoint', pin)

disp('confidence interval/errorbars for  A, x0, and gamma');
ci = confint(fitobject) 

builtin_fit_check(x,y, fitobject);
