% FitExperiment fits a curve to the data in the specified file
% if there are 3 columns it is assumed that the 3rd column is
% the uncertainty (sigma) in the values in the 2nd column.

close all;clear;
clc;

% Change these values if your data are in different columns.

xcol = 1;
ycol = 2;

% If you have more than 2 columns of data but none of them are
% uncertainties, set "errorcol" to 0.  Otherwise set this to
% the correct column.

errorcol = 3;

% If you wish to use a subset of the data you can specify the
% minimum and maximum values here.  If both are zero the entire
% data set will be used.

xminimum = 0;
xmaximum = 0;

filename = input('Enter the name of the data file: ','s');
data = load(filename);
if ((xminimum == 0) && (xmaximum == 0))
    indices = 1:1:size(data,1);
else
    indices = find(data(:,xcol)>=xminimum & data(:,xcol)<=xmaximum);
end
xvalues = data(indices,xcol);
yvalues = data(indices,ycol);
if ((size(data,2) > 2) && (errorcol > 0))
    weights = 1.0./(data(indices,errorcol).^2);
else
    weights = ones(size(yvalues));
end

format long

cftool(xvalues,yvalues,[],weights)
pause;
h = plot(fittedmodel,xvalues,yvalues);
% The location of the legend is given in compass points: North is top center
% NorthEast is top right corner, SouthWest is bottom left corner, etc.  You
% should choose a location such that the legend does not interfere with your
% data or model plot.
if ((size(data,2) > 2) && (errorcol > 0))
    legend(h, 'yvalues vs. xvalues with weights', 'model fit', 'Location', 'NorthEast' );
else
    legend(h, 'yvalues vs. xvalues', 'model fit', 'Location', 'NorthEast');
end	
xlabel('X Values (units)');
ylabel('Y Values (units)');
title(' ');
% show the results
fittedmodel
goodness

