%% 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 the fitting function with fittype
% notice that it is quite human readable
% Matlab automatically treats x as independent variable
f=fittype(@(A,x0,gamma, x) A ./ (1 +((x-x0)/gamma).^2) )

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

% assign initial guessed parameters 
% [A, x0, gamma] they are in the order of the appearance 
% in the above fit function definition
pin=[3,3,1];

% 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) 

% it is good idea to compare fit and data visually
builtin_fit_check(x,y, fitobject);
