0001 function [y0,ya,ey1,ey2,chi2] = fit_profile_fxdrg(x,y,model,init,dispfit)
0002
0003
0004
0005
0006
0007 if nargin == 2,
0008 model = 1;
0009 init = [2,2];
0010 dispfit = 0;
0011 elseif nargin == 3,
0012 init = [2,2];
0013 dispfit = 0;
0014 elseif nargin == 4,
0015 dispfit = 0;
0016 end
0017
0018 y0 = y(1);
0019 ya = y(end);
0020
0021 if model == 1,
0022 [z,chi2] = fminsearch(@(z) sum((y - ((y0 - ya)*(1 - x.^z(1)).^z(2) + ya)).^2),init);
0023 ey1 = z(1);
0024 ey2 = z(2);
0025 elseif model == 2,
0026 [z,chi2] = fminsearch(@(z) sum((y - ((y0 - ya)*(exp(z) - exp(z*x.*x))/(exp(z)-1) + ya)).^2),init);
0027 ey1 = z;
0028 ey2 = NaN;
0029 end
0030
0031 if dispfit,
0032 if model == 1,
0033 yfit = (y0 - ya)*(1 - x.^ey1).^ey2 + ya;
0034 elseif model == 2,
0035 yfit = (y0 - ya)*(exp(z) - exp(ey1*x.*x))/(exp(ey1)-1) + ya;
0036 end
0037 figure('Name','Fit profile'),plot(x,y,'r',x,yfit,'b-');legend('Data','Fit')
0038 end
0039
0040
0041