function [fval fit_curve]=FitFunc(X,xdata,ydata,W)

if nargin<4
    W=1;    %No weighting
end

a=X(1);
b=X(2);

model=a*xdata+b;
data=ydata;

fval=sum( (model-data).^2./W);

if nargout>1
    fit_curve=model;
end

end

