Use x as a wave not a variable during fitting

Hi. I tried to make a user fit function as follow but why x should be defined as a variable instead of a wave?


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
The SF function returns NOTHING. Inside it, every point in the wave StructureFactor will also be set to the same numerical value. The wave or variable d_spacing is undeclared. The input values to SF in Fitting_Engine are also entirely jumbled. Perhaps you mean to write something equivalent to this instead ...

Function SF(ASF, Q, occ, u, xx)
        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
Thanks, jjweimer. However, in my script, z is not a variable. It is parameter to fit. Instead, Q is variable.
And there is constant d_spacing = 2.492 in the very front of this script. I omitted it.

jjweimer wrote:
The SF function returns NOTHING. Inside it, every point in the wave StructureFactor will also be set to the same numerical value. The wave or variable d_spacing is undeclared. The input values to SF in Fitting_Engine are also entirely jumbled. Perhaps you mean to write something equivalent to this instead ...

Function SF(ASF, Q, occ, u, xx)
        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

Yes. I see my mistake. The continued issue here is, the code you provide is cluttered and incorrect. It is so cluttered that even I could not correct it without making additional mistakes.

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 ...

somecomplexwave = SF(....)


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

 

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:

displayhelptopic "All-At-Once Fitting Functions"