clc; clear All; %To search for the minimum of an arbitrary function, MATLAB pro- MATLAB provides the function fminsearch. %To use it, you have to write an additional short function calculating the value of the function to be minimized. %If you would want,e. g., to ?t a Lorentzian y = a1/((x - a2)^2 + a3) to a data set Xi, Yi, %you should define in MATLAB a function resulting in the sum of the squared residuals %function d = devsum(a) %global X Y %The start values for the parameters (a1, a2, a3) could of course be chosen in a better way when the data set is known. %An example for the ?t with a Lorentzian is shown in Fig. 2, the data set there was de?ned by % a(1)=20, a(2)=20, a(3)=20 X = linspace(0,100,200); %Y = 20./( 1 + ( (X-30)./20 ).^2+0.08*randn(size(X))); % wikipeida definition b1 = 12; b2 = 1200; b3 = 50; b4 = 10; b5 = 0.1; b=[b1,b2,b3,b4,b5]; %Y = 1/(sqrt(2*pi)*b1)*exp(-abs(X-b3).^b2/(2*b1.^b2) + 0.08*randn(size(X))); Y = b(1)+b(2)*exp( -0.5*(abs(X-b(3))/(b(4))).^b(5) + 0.08*randn(size(X))); % super gaussian plot(X',Y','.'); hold on; %dis = 1/(sqrt(2*pi)*b1)*exp(-abs(X-b3).^b2/(2*b1.^b2)); dis = b(1)+b(2)*exp( -0.5*(abs(X-b(3))/(b(4))).^b(5)); plot(X',dis','k'); hold on; % a(1) = base % a(2) = A; amplitude % a(3) = x0; center % a(4) = D; sigma_0 % a(5) = N; a1 = b1*0.8; a2 = b2*1.4; a3 = b3*1.2; a4 = b4*1.2; a5 = b5*1.2; %SupGau = a(1)+a(2)*exp(-abs(X-a(3))/(a(4))).^a(5); % super gaussian a0 = [a1,a2,a3,a4,a5]; opts = optimset('TolX',1e-4,'MaxFunEvals',10000,'MaxIter',10000,'Display','on'); [fitpara,fval,flag,err] = fminsearch(@SupGau_devsum,a0,opts,X,Y); display('sigma_0:'); fitpara(1) display('N'); fitpara(2) display('x0'); fitpara(3) %y_fit = 1/(sqrt(2*pi)*fitpara(1))*exp(-abs(X-fitpara(3)).^fitpara(2)/(2*fitpara(1).^fitpara(2))); y_fit = fitpara(1)+ fitpara(2)*exp( -0.5*(abs(X-fitpara(3))/(fitpara(4))).^fitpara(5)); plot(X',y_fit','r') %The function fminsearch can be tuned by additional options, one can demand a certain accuracy, restrict the number of optimization steps etc. %For a detailed description consult Help