Fitting easyspin functions with lsqcurvefit problem

General forum for EasySpin: questions, how to's, etc.
Post Reply
dusanm
Newbie
Posts: 4
Joined: Mon Apr 10, 2017 2:20 am

Fitting easyspin functions with lsqcurvefit problem

Post by dusanm »

I am trying to use Matlab fitting function lsqcurvefit to fit easyspin based functions and it just does not work?! I make function which accept Intensity, B0 and linewidth, then I convert the values to Sys values, insert into pepper and it should behave as y=f(x) function. What I got is that I have the line but the initial values does not go to the best values. It does not work for simple single line?!
Stefan Stoll
EasySpin Creator
Posts: 1041
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: Fitting easyspin functions with lsqcurvefit problem

Post by Stefan Stoll »

Please post a minimal exampe code that demonstrates this problem.
dusanm
Newbie
Posts: 4
Joined: Mon Apr 10, 2017 2:20 am

Re: Fitting easyspin functions with lsqcurvefit problem

Post by dusanm »

Here is the code:

Code: Select all

% Fitting easyspin functions using lsqcurvefit
clear;

% open file, if you use open file:
% [StrFile,StrDir,FilterIndex] = uigetfile('*.spe','Open file','c:\Users\Dusan\Documents\EPRmerenja\Marija ZoranSap');
% StrFullPath = [StrDir StrFile];
% open file, if you use direct link from HD 
StrFullPath = 'c:\Users\Dusan\Documents\EPRmerenja\Marija ZoranSap\Pani.spe'; % your path here!
[B,I,param]=eprload(StrFullPath);
% File can be found at https://www.dropbox.com/s/7h4rzln5r20egzw/Pani.spe?dl=0

I=(I-I(1))*100/param.Gain;
% Initial values
Ip = 40000; g0p = 2.001; DBp = 0.3; Cp = 0;
initial = [Ip, g0p, DBp, Cp];
LB = [10 2.0000 0.01 -1000]; UB = [400000 2.005 1 1000];

% Fitting Easyspin single Lor line
FitES = lsqcurvefit(@fEasyspinForumLor, initial, B, I,LB,UB);
IinitialES = fEasyspinForumLor(initial,B);
IFittedES = fEasyspinForumLor(FitES,B);

% Fitting with Lorentzian
FitLor = lsqcurvefit(@fLorForum, initial, B, I,LB,UB);
IinitialLor = fLorForum(initial,B);
IFittedLor = fLorForum(FitLor,B);

% Plot experimental, initial and fitted curve
figure;
plot(B,I,'c','linewidth',5,'DisplayName','Exp'); hold on;
plot(B,IinitialES,'b--','linewidth',4,'DisplayName','ES init.'); hold on;
plot(B,IFittedES,'b','linewidth',3,'DisplayName','ES fitted');  hold on;
plot(B,IinitialLor,'r--','linewidth',3,'DisplayName','Lor init.'); hold on;
plot(B,IFittedLor,'r','linewidth',2,'DisplayName','Lor fitted'); % hold on;
legend('show');
grid on;

and the functions are here:
Lorentzian:

Code: Select all


% Lorentzian plus constant
function y = fLorForum(cc,x)

frek = 9.361;
B0 = mhz2mt(frek *1000) * gfree / cc(2); % from g to B
y=-(2*(cc(1)/pi)*cc(3)*(x-B0))./(cc(3)^2 + (x-B0).^2).^2 + cc(4); % derivative Lor, version with int ~ I only. 
% Identical to easysin version according to documentation.
end
and this is Lorentzian made using Easyspin:

Code: Select all

%Easyspin Lorentzian 

function y = fEasyspinForumLor(c,x)

Exp.mwFreq = 9.361; % frequency
% gg = (mhz2mt(frek *1000) / c(2)) * gfree; % ovo prebacuje B u g
Exp.Range = [x(1) x(end)];
Exp.nPoints = length(x);
Sys.g = c(2);
Sys.lw = [0 c(3)*2];
[xP,yP1] = pepper(Sys,Exp);
y = c(1) * 1.807713 * yP1 / 1000 +c(4);
y = y.';
end
The experimental file is here:
https://www.dropbox.com/s/7h4rzln5r20egzw/Pani.spe?dl=0

There is factor 1.8077 / 1000 that has to be used to obtain same intensity for Easyspin Lorentzian to compare to Lorentzian line.
if you fit this spectrum you got fit that is no good as the one obtained with explicit Lorentzian line?!? The same initial values are used.
Stefan Stoll
EasySpin Creator
Posts: 1041
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: Fitting easyspin functions with lsqcurvefit problem

Post by Stefan Stoll »

In addition to the normalized lineshape function, the Lorentzian lineshape in the EasySpin spectrum generated by pepper also includes three additional intensity factors: (1) the quantum-mechanical transition probability, (2) the difference in populations between the two levels involved in the transition (Boltzmann factors), and (3) the 1/g factor to convert from the frequency domain to the field domain.

You can read about these factors in the publications listed here, esp. the 2014 review.
Post Reply