Fit function with use of a structure

Hello,

I am a quite new Igor Pro user but already convinced by its high capabilities in data analysis.
At the present time, I am trying to fit a heavy function to data. My fit function has to use external wave as input data, therefore I am trying to write a structure based fit function.
I have already read the manual documentation about structures and fitting, but I still don't understand how structure based fit function works.
Here is the documentation code:
// The structure definition
Structure expFitStruct
    Wave coefw // required coefficient wave
    Variable x  // required X value input
    Variable x0 // constant
EndStructure

   
// The fitting function
Function fitExpUsingStruct(s) : FitFunc
    Struct expFitStruct &s
    return s.coefw[0] + s.coefw[1]*exp(-(s.x-s.x0)/s.coefw[2])
End
// The driver function that calls FuncFit:
Function expStructFitDriver(pw, yw, xw, xOff)
    Wave pw // coefficient wave- pre-load it with initial guess Wave yw
    Wave xw
    Wave yw
    Variable xOff
   
    Variable doODR
    // An instance of the structure. We initialize the x0 constant only, // Igor (FuncFit) will initialize coefw and x as required.
    STRUCT expFitStruct fs
    fs.x0 = xOff // set the value of the X offset in the structure
    FuncFit fitExpUsingStruct, pw, yw /X=xw /D /STRC=fs
    // no history report for structure fit functions. We print our own // simple report here:
    print pw
    Wave W_sigma
    print W_sigma
End


And here the execution code
Make/D/O/N=100 expData,expDataX
expDataX = enoise(0.5)+100.5
expData = 1.5+2*exp(-(expDataX-100)/0.2) + gnoise(.05)
Display expData vs expDataX
ModifyGraph mode=3,marker=8
   
Make/D/O expStructCoefs = {1.5, 2, .2}
expStructFitDriver(expStructCoefs, expData, expDataX, 100)


My question therefore is : How does fitExpUsingStruct know that "pw" is the parameter wave and "yw" the wave to fit? The structure has never be initialized, therefore there is no link between "fs.coeff" and "pw", nor "fs.x" and "xw".
I must be missing some fundamental key, any explanation would be appreciated.
This problem blocks me because my fit function is not analytic and I don't know how to deal with the data in order to get it work.

Tanks!!
You are looking at the all-at-once variant of the structure fit function. The parts of your structure are interpreted positionally: your structure *must* start with three waves. The first is always the coefficient wave regardless of its name, the second is the Y wave which you must fill in with model values. The third is the X wave containing the X values at which you must evaluate your fit function. The rest of the structure is free for you to define and use in any way you wish. Igor will not look at or manipulate any but the first three members of the structure. (There's a lot of complicated code that analyzes your structure to make sure it has the required parts, and to manipulate those parts while ignoring the rest).

You do not see the Y data wave, and the X wave passed to your fit function is not the same as the X data you provide to the FuncFit command. The coefficient wave may or may not be the original coefficient wave from your driver function. Since FuncFit will fill in the first three members of your structure, you do not set them yourself in the driver function. Your driver must fill in any additional members. If you are using the structure to pass some auxiliary wave to the fit function, your driver must set that wave in the structure before calling FuncFit.

The job your fit function must perform is to fill up the Y wave in the structure with one model value for each X value in the X wave in the structure.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thank you very much John, it solved ma problem and the explanations helped me so much in understanding how it works :-)

Protra