Use x as a wave not a variable during fitting
imksh2000
Function SF(StructureFactor,occ,ASF,ci,Q,z,u)
variable occ,z,u
variable/c ci
wave ASF, Q
wave/c StructureFactor
StructureFactor = occ * ASF * exp(ci*Q*z)*exp(-0.5*(Q*u)^2)
End
Function Fitting_Engine(p,x)
wave p; variable x
wave/c F_Ni_fit, F_NiO_1_fit, F_NiO_2_fit, F_NiO_3_fit
wave ASF_Ni
nvar u_Ni
SF(F_Ni_fit, 1, ASF_Ni,sqrt(-1),x,d_spacing,u_Ni)
wave/c F_ctr
F_ctr = 1 / (1-exp(-sqrt(-1)*x*d_spacing))
SF(F_NiO_1_fit, 2, ASF_Ni,sqrt(-1),x,p[0],0.5)
SF(F_NiO_2_fit, 2, ASF_Ni,sqrt(-1),x,p[1],0.5)
SF(F_NiO_3_fit, 2, ASF_Ni,sqrt(-1),x,p[2],0.5)
return magsqr(F_Ni_fit * F_ctr + F_NiO_1_fit + F_NiO_2_fit + F_NiO_3_fit)
End
Function Do_Fit()
wave coefwave, Q_exp, I_exp
Make/O/N=(numpnts(Q_exp)) I_fit
wave/c F_Ni
Duplicate/O/C F_Ni F_Ni_fit, F_NiO_1_fit, F_NiO_2_fit, F_NiO_3_fit
FuncFit/NTHR=0 Fitting_Engine coefwave I_exp /X=Q_exp /I=1 /D= I_fit
End
variable occ,z,u
variable/c ci
wave ASF, Q
wave/c StructureFactor
StructureFactor = occ * ASF * exp(ci*Q*z)*exp(-0.5*(Q*u)^2)
End
Function Fitting_Engine(p,x)
wave p; variable x
wave/c F_Ni_fit, F_NiO_1_fit, F_NiO_2_fit, F_NiO_3_fit
wave ASF_Ni
nvar u_Ni
SF(F_Ni_fit, 1, ASF_Ni,sqrt(-1),x,d_spacing,u_Ni)
wave/c F_ctr
F_ctr = 1 / (1-exp(-sqrt(-1)*x*d_spacing))
SF(F_NiO_1_fit, 2, ASF_Ni,sqrt(-1),x,p[0],0.5)
SF(F_NiO_2_fit, 2, ASF_Ni,sqrt(-1),x,p[1],0.5)
SF(F_NiO_3_fit, 2, ASF_Ni,sqrt(-1),x,p[2],0.5)
return magsqr(F_Ni_fit * F_ctr + F_NiO_1_fit + F_NiO_2_fit + F_NiO_3_fit)
End
Function Do_Fit()
wave coefwave, Q_exp, I_exp
Make/O/N=(numpnts(Q_exp)) I_fit
wave/c F_Ni
Duplicate/O/C F_Ni F_Ni_fit, F_NiO_1_fit, F_NiO_2_fit, F_NiO_3_fit
FuncFit/NTHR=0 Fitting_Engine coefwave I_exp /X=Q_exp /I=1 /D= I_fit
End
wave ASF, Q
variable occ,u, xx
variable/c ci = sqrt(-1)
return ( occ * ASF * exp(ci*Q*xx)*exp(-0.5*(Q*u)^2))
End
Function Fittting_Engine(...)
...
F_Ni_fit = SF(ASF_Ni, d_spacing, 1, u_Ni, x)
F_NiO_1 = SF(...)
...
end
In summary, this example has fundamental problems with the syntax in defining and using functions.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
June 12, 2017 at 09:02 am - Permalink
And there is constant d_spacing = 2.492 in the very front of this script. I omitted it.
June 12, 2017 at 05:12 pm - Permalink
Sit down and do this ONE STEP AT A TIME. Write the SF function so that it works properly. It is totally incorrect and incoherent as it stands now. Test the SF function by doing this on the command line ...
Where somecomplexwave is a predefined complex wave. Plot somecomplexwave as a function of Q to prove that you have the correct function. Right now, your SF function will NOT work AT ALL in this way. This means, it will NOT WORK AT ALL in a FuncFit call.
Once you have SF written correctly, write the Fitting_Engine(...) function (it should NOT use the terms p and x as inputs by the way, it should for example use ww and xx). Make sure that Fitting_Engine creates a proper wave using a comparable test to the one above.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
June 13, 2017 at 06:21 am - Permalink
Your code works, but really not quite right!
The function of SF is just to get a value, it is no need to pass a wave for this purpose
Fitting_Engine with this format can only return a number type value, even you return a wave. In fact, the waves F_NiO_xxxxx can be replaced with variables.
If you want the fitting function return a wave one time, please read the help about all at once fitting technicks:
June 14, 2017 at 05:58 am - Permalink