% Define the data sets that you are trying to fit the
% function to. Make sure to import these into MatLab before running this
% t = (MahnazAntonPlatinum(:,2)+MahnazAntonPlatinum(:,1))./2;
% r = MahnazAntonPlatinum(:,6);

% Initialize the coefficients of the function
X0=[70000 280 10.5];

% Set an options file for LSQNONLIN to use the
% medium-scale algorithm
options = optimset('Largescale','off');

% Calculate the new coefficients using LSQNONLIN
[params,renorm]=lsqnonlin(@BGcomp,X0,[],[],options,t,r);

%define the fitted function
z=params(2)./t;
J51=120*zeta(5)-exp(-1.0.*z).*(z.^5+5*z.^4+20*z.^3+60*z.^2+120.*z+120);
J52=-exp(-2.0.*z).*(z.^5+5*z.^4/2+20*z.^3/4+60*z.^2/8+120.*z/16+120/32);
J53=-exp(-3.0.*z).*(z.^5+5*z.^4/3+20*z.^3/9+60*z.^2/27+120.*z/81+120/243);
J54=-exp(-4.0.*z).*(z.^5+5*z.^4/4+20*z.^3/16+60*z.^2/64+120.*z/256+120/1024);
J55=-exp(-5.0.*z).*(z.^5+5*z.^4/5+20*z.^3/25+60*z.^2/125+120.*z/625+120/3125);
J56=-exp(-6.0.*z).*(z.^5+5*z.^4/6+20*z.^3/36+60*z.^2/216+120.*z/1296+120/7776);
J57=-exp(-7.0.*z).*(z.^5+5*z.^4/7+20*z.^3/49+60*z.^2/343+120.*z/2401+120/16807);
J58=-exp(-8.0.*z).*(z.^5+5*z.^4/8+20*z.^3/64+60*z.^2/512+120.*z/4096+120/32768);
J59=-exp(-9.0.*z).*(z.^5+5*z.^4/9+20*z.^3/81+60*z.^2/729+120.*z/6561+120/59049);
J510=-exp(-10.0.*z).*(z.^5+5*z.^4/10+20*z.^3/100+60*z.^2/1000+120.*z/10000+120/100000);
BG1=t.^5/(params(2))^6.*J51;
BG2=t.^5/(params(2))^6.*J52;
BG3=t.^5/(params(2))^6.*J53;
BG4=t.^5/(params(2))^6.*J54;
BG5=t.^5/(params(2))^6.*J55;
BG6=t.^5/(params(2))^6.*J56;
BG7=t.^5/(params(2))^6.*J57;
BG8=t.^5/(params(2))^6.*J58;
BG9=t.^5/(params(2))^6.*J59;
BG10=t.^5/(params(2))^6.*J510;
BGT=BG1+BG2+BG3+BG4+BG5+BG6+BG7+BG8+BG9+BG10;
Y_fit = params(3)+params(1)*(BGT);
%plot the data along with the fit function
hold on;
plot(t,r,'b')
plot(t,Y_fit,'r')
hold off;