Tie several variables to a common value

General forum for EasySpin: questions, how to's, etc.
thanasis
Local Expert
Posts: 245
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Tie several variables to a common value

Post by thanasis »

Sometimes, we need to define multiple instances of the same variable which should have the same value (i.e. be treated as a single variable from an error-minimization perspective).

E.g. three hyperfine couplings to the same spin:

Code: Select all

ACuxy = 15;
ACuz = 150;
Sys.S=1/2;
Sys.g=[gxy gz];
Sys.Nucs='Cu,Cu,Cu';
Sys.A=[ACuxy ACuz; ACuxy ACuz; ACuxy ACuz];
Vary.g = [0 0];
Vary.A = [0 50; 0 50; 0 50];
Or, three spins forming an equilateral triangle:

Code: Select all

J = 100;
Sys.S = [1/2 1/2 1/2];         % three electron spins
Sys.ee = [J J J];          % three coupling (1-2,1-3,2-3)
Vary.ee = [10 10 10];
In both cases, each instance of the interaction ACuz or J is initiated at the same value and varies within the same intervals, but it follows its own course during the minimisation process. So, even if physically the value for all variables should be the same, computationally they may diverge.

Is there a way to tie all interactions together, so as they are fitted as a single variable?

Thanks in advance
Matt Krzyaniak
EasySpin Guru
Posts: 153
Joined: Tue Jul 22, 2014 11:01 am
Location: Northwestern University

Re: Tie several variables to a common value

Post by Matt Krzyaniak »

You can do that relatively easily through custom functions, refer to http://www.easyspin.org/forum/viewtopic.php?p=587#p587 as a general way to implement a similar idea, or towards the bottom of http://easyspin.org/easyspin/documentat ... tting.html under the heading User-defined simulation functions
thanasis
Local Expert
Posts: 245
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Tie several variables to a common value

Post by thanasis »

Thanks for the pointers!

I will look into them.
thanasis
Local Expert
Posts: 245
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Tie several variables to a common value

Post by thanasis »

OK, as per http://easyspin.org/easyspin/documentat ... omfunction, I have defined the function constrain.m as follows:

Code: Select all

function y = constrain(Sys1,Exp,Opt);
fullSys = Sys1;
fullSys.A = [Sys1.A; Sys1.A; Sys1.A];
fullSys.Nucs = 'Cu,Cu,Cu';
[x,y] = pepper(fullSys,Exp,Opt);
return
Then, I define my system where I want to tie together the three hyperfines and call constrain to do the fit

Code: Select all

gxy = 1.5;
gz=2.23;
w1=4;
w2=4;
ACuz=220;

%The spin system
%--------------------------------------------------------------------------
Sys1.S=1/2;
Sys1.g=[gxy gz];
Sys1.lwpp=[w1 w2];
Sys1.Nucs='Cu,Cu,Cu';
Sys1.A=[0 ACuz; 0 ACuz; 0 ACuz];

%Experimental
%--------------------------------------------------------------------------
Exp.Temperature = 4.1; Exp.mwFreq=9.423859; Exp.CenterSweep=[380.0 240.0]; Exp.nPoints=2048;

% Fitting
%--------------------------------------------------------------------------
Vary1.g = [0.1 0.1];
Vary1.lwpp = [5 5];
Vary1.A = [0 50; 0 50; 0 50];
[B, spc, params] = eprload('lm024_6.spc')
FitOpt.Method = 'simplex fcn'; % simplex algorithm, data as is
FitOpt.Scaling = 'maxabs';
esfit('constrain',spc,Sys1,Vary1,Exp,[],FitOpt);
The fitting screen comes up with the data curvem but when I start it I get the error message:

Code: Select all

Error using pepper (line 179)
Size of Sys.A (9x2) is inconsistent with number of nuclei (3) and electrons (1).
Error in pepper (line 127)
Error in constrain (line 7)
[x,y] = pepper(fullSys,Exp,Opt);
Error in esfit>assess (line 794)
Error in esfit_simplex (line 77)
Error in esfit>runFitting (line 651)
Error while evaluating UIControl Callback
thanasis
Local Expert
Posts: 245
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Tie several variables to a common value

Post by thanasis »

OK, probably I found my error, but please do confirm:

In my fitting code I changed to:

Code: Select all

 Sys1.A=[0 ACuz];
...and...

Code: Select all

Vary1.A = [0 50];
and then the three hyperfines are fitted as one.
Stefan Stoll
EasySpin Creator
Posts: 1073
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: Tie several variables to a common value

Post by Stefan Stoll »

Sys.A must contain one set of one/two/three hyperfine values for each pair of electron and nuclear spins. So if you have 3 electron spins and 3 magnetic nuclei, you need 9 hyperfine sets. See the documentation here.
thanasis
Local Expert
Posts: 245
Joined: Thu Jan 21, 2016 6:28 am
Location: Strasbourg

Re: Tie several variables to a common value

Post by thanasis »

Indeed, I had looked into that to simulate the full system.

However, to lighten up the CPU/memory load, I have been trying to fit only the S = 1/2 ground state, which I coupled to the three nuclear spins.
(I suppose my code is valid for that approach?)

As a second step, I will be looking into fitting the whole system, including J coupling and AE.

Thanks for the feedback.
Yahor Savich
Newbie
Posts: 6
Joined: Sun Aug 14, 2016 5:38 pm

Re: Tie several variables to a common value

Post by Yahor Savich »

This is a very useful thread, but I have to use different Exp variables for my components. I am fitting a multi-component oriented data with different orientation and here is the custom function I use:

Code: Select all

function [x,y] = pepperOG(Sys,Exp)
   Exp.Ordering = @(phi,theta) gaussian(theta,Sys.theta0*pi/180,Sys.Dtheta*pi/180).*gaussian(phi,Sys.phi0*pi/180, Sys.Dphi*pi/180);
   Exp.mwFreq = Sys.mwFreq;
   [x,y] = pepper(Sys,Exp);
During the fitting I want to keep A and g tensors to be the same for all components, but still be varied. However, if I double the number of species:

Code: Select all

function [x,y] = dimer(Sys,Exp)
    fullSys = Sys;
    fullSys.g = [Sys.g; Sys.g]
    fullSys.S = [1/2 1/2]
    fullSys.A = [Sys.A, Sys.A];
    fullSys.Nucs = '14N';
    fullSys.ee=1e-3;
    [x,y] = pepper(fullSys,Exp)
I cannot have different Exp variables for those species.

So my question is how to keep Exp.Ordering to be different for different components but still vary the same A and g tensors for all components. This post explains how to perform multi-frequency fit, by merging axes while having two different Exp variables, but I have slightly different problem.
Stefan Stoll
EasySpin Creator
Posts: 1073
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: Tie several variables to a common value

Post by Stefan Stoll »

If you have a multi-component situation, then it is better to use

Code: Select all

[B,spc] = pepper({Sys1,Sys2},Exp,Opt)
instead of merging Sys1 and Sys2 with a fake coupling. If your Exp.Ordering needs to be different for the different components, use two pepper calls and add the results.
Yahor Savich
Newbie
Posts: 6
Joined: Sun Aug 14, 2016 5:38 pm

Re: Tie several variables to a common value

Post by Yahor Savich »

Yes, that was my motivation to construct pepperOG function. But since esfit parses through the components, the code below won't keep same g tensors changing simultaneously:

Code: Select all

Sys1.g = [2.007 2.006 2.002];
Sys2.g = Sys1.g;
Vary1.g = [0 0 0.005];
Vary2.g = 0;
Currently I have to vary g tensors for Sys1 and Sys2 individually and equate them manually
Post Reply