esfit with my own simulation function

A place to report and discuss potential bugs
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

esfit with my own simulation function

Post by Emilien »

Hi,
I try to perform esfit with my own simulation function.
When fitting is started, the current simulation is not shown and rmsd is constant.
When stopped and saved, the simulation is plotted.

Here is my code:

Code: Select all

clear; close;

[B,spc]=eprload('Coli_2.DSC');
load('coli2.mat'); %load Sys Exp 

Sys.Aperp=Sys.A(2);
Sys.Apar=Sys.A(1);
Sys=rmfield(Sys,'A');

Vary.Aperp = 15;
%Vary.A = [0 100;0 100];
Vary.Apar =90;

esfit('mysim',spc,Sys,Vary,Exp)
Here is mysim.m

Code: Select all

function [x,y] = mysim(Sys,Exp,SimOpt)
Sys.A = [Sys.Apar Sys.Aperp Sys.Aperp];
[x,y] = chili(Sys,Exp,SimOpt);
end
Here are the data:
data.zip
(5.28 KiB) Downloaded 399 times
Thanks fo your help!

PS:I know that there is an easiest solution for this example...
Stefan Stoll
EasySpin Creator
Posts: 1057
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: esfit with my own simulation function

Post by Stefan Stoll »

This is a bug in EasySpin 4.5.5. To work around it, define your function with a single output argument:

Code: Select all

function y = mysim(Sys,Exp,SimOpt)
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

Re: esfit with my own simulation function

Post by Emilien »

Stefan, thanks for your answer.
Your solution enables the simulation to be displayed in esfit. But unfortunately, when the fitting process is stoped, an error occured:
??? Error using ==> mysim
Too many output arguments.

Error in ==> C:\easyspin-4.5.5\easyspin\esfit.p>runFitting at 669

??? Error while evaluating uicontrol Callback


leading to another error when trying to save result

??? Reference to non-existent field 'rmsd'.

Error in ==> C:\easyspin-4.5.5\easyspin\esfit.p>refreshFitsetList at 1197


Error in ==> C:\easyspin-4.5.5\easyspin\esfit.p>saveFitsetCallback at 1223


??? Error while evaluating uicontrol Callback
Stefan Stoll
EasySpin Creator
Posts: 1057
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: esfit with my own simulation function

Post by Stefan Stoll »

Clearly, at some place esfit wants your function to return one output, and at another one it wants two. Try this:

Code: Select all

function varargout = mysim(Sys,Exp,SimOpt)
%...
if nargout==1, varargout = {y}; else varargout = {x,y}; end
return
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

Re: esfit with my own simulation function

Post by Emilien »

Wonderful, it works!
Thanks a lot!
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

Re: esfit with my own simulation function

Post by Emilien »

Hi,

it actually works for one component...
Nevrtheless I need to perform a simulation with 2 components with different restrictions.
For example, one component (Sys1) has restriction and has to be simulated with mysim.m. The other component has no restriction and has to be simulated with chili...

I've naively tried

Code: Select all

esfit({'mysim' 'chili'},spc,{Sys1 Sys2},{Vary1 Vary2},Exp)
but it doesn't work...
Is there a solution for that?
Thanks.
Stefan Stoll
EasySpin Creator
Posts: 1057
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: esfit with my own simulation function

Post by Stefan Stoll »

Hmm, this is an interesting use case. As it, ES cannot accommodate custom fits of multi-component data in a convenient manner. Internally, esfit just runs a for loop and calls the sim function for each component in turn.

But, as often, there's a hack: Merge your two spin system structures into one, and then use myfit to simulate the multicomponent-spectrum:

Code: Select all

Sys.g1 = 2;
Sys.g2 = 2.1;
Sys.weight1 = 0.5;
% Vary.g1 = ...  etc
esfit('mysim',Sys,Vary,...);
The custom sim function should do the following:

Code: Select all

Sys1.g = Sys.g1;
[x,y1]= pepper(Sys1,...);
Sys2.g = Sys.g2;
[x,y2] = pepper(Sys2,...)l
y = y1*Sys.weight1 + y2*(1-Sys.weight1).
Ugly, but it should work.
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

Re: esfit with my own simulation function

Post by Emilien »

It works...
But "ugly" as you said, because all fields have to be redefined... Do you think that in a future release of esfit, we could specify the sim function for each component? If "Internally, esfit just runs a for loop and calls the sim function for each component in turn", it could be possible for this loop to call different sim functions, isn't it?
Stefan Stoll
EasySpin Creator
Posts: 1057
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: esfit with my own simulation function

Post by Stefan Stoll »

That would be one possible approach. I'll put it on the to-do list. Thanks for the suggestion!
Emilien
Resident User
Posts: 61
Joined: Mon Apr 20, 2015 12:13 am

Re: esfit with my own simulation function

Post by Emilien »

Hello,
does the new version of esfit include this suggestion? That would be great!
Post Reply