Weird stuff and better fit behaviour when providing a constraint wave

I've been trying to fit a fairly complicated function (sum of 5 Lorentzians, where 2 of them dominate the signal) and running into witchcraft-like behaviour.

Last Friday I could only get a good fit by constraining my peak heights to > 0.

Today I seemed not to need that constraint, but I ran into a weird problem -- I found that constraining the order of peaks (ie a constraint wave with something lie "K4 > K7" in it) changed the fit results in an unexpected way. So I prepared a simple example using a sum of 3 Gaussians -- attached now.

After preparing the attached example, however, I ran into even more weird behaviour! Returning to my original Lorentzians, I could no longer see a difference by constraining K4 > K7...

What is going on? How can I ensure repeatable results from Igor?

(Bonus question -- my Igor plots are uglier since switching to a new windows PC, is there any way to fix this?)
170918 weird fit behaviour.pxp (34.43 KB)
Most important bit of advice here: always use double-precision waves for your coefficient wave. Unlike the other waves involved in a curve fit, your coefficient wave is the actual wave passed to your user-defined fitting function. Using a single-precision wave discards useful precision during the computations.

But, wait, you say- how do I do that? And the answer is, use Make/D. The default (with just "Make") is single-precision. We perhaps confuse things a bit by causing the Make Waves dialog to generate a Make/D command by default, and if you don't notice that you may not be used to making certain of the /D flag.

Your peaks are heavily overlapped- a fit to a single Gaussian looks almost as good as your three-peak fit. That can lead to mysteriously erratic behavior because the fits sort of trade off- you can probably get a pretty good fit by moving one of the side peaks out along the tail of the middle peak while reducing the amplitude. That can leave the fit swimming around without strong guidance on where to go next.

Paradoxically, fitting fake data that has no noise can be problematic also because when you get close to the solution floating-point truncation errors may cause weird things to happen to the derivatives. But if your real data have peaks as closely spaced as your fake data, it will require very clean data without a lot of noise, as the noise will make the mysteriously erratic behavior even more erratic.

Finally, perhaps you have your own reasons for rolling your own peak fit function. I used Multipeak Fit 2 to fit your fake data:

Fit completed 3:49 PM Monday, September 18, 2017
Y data wave: root:yw
X data wave: root:xw
Chi square: 1.0436e-11
Total fitted points: 128
Multi-peak fit version 2.22
Total Peak Area = 655.81 +/- 0.0013784

Baseline    Type: Constant

    y0 =    4.0512e-09   +/-    3.6809e-08

Peak 0  Type: Gauss

    Location =  120  +/-    1.3373e-05
    Height =    10   +/-    2.8523e-05
    Area =  177.25   +/-    0.00057306
    FWHM =  16.651   +/-    6.4289e-06

    Fit function parameters
    Location =  120 +/- 1.3373e-05
    Width = 10  +/- 3.861e-06
    Height =    10  +/- 2.8523e-05

Peak 1  Type: Gauss

    Location =  130  +/-    3.1503e-06
    Height =    20   +/-    4.4835e-05
    Area =  354.49   +/-    0.0011173
    FWHM =  16.651   +/-    1.5174e-05

    Fit function parameters
    Location =  130 +/- 3.1503e-06
    Width = 10  +/- 9.1126e-06
    Height =    20  +/- 4.4835e-05

Peak 2  Type: Gauss

    Location =  140  +/-    1.8918e-05
    Height =    7    +/-    2.8313e-05
    Area =  124.07   +/-    0.0005684
    FWHM =  16.651   +/-    9.0625e-06

    Fit function parameters
    Location =  140 +/- 1.8918e-05
    Width = 10  +/- 5.4426e-06
    Height =    7   +/- 2.8313e-05


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Hi John,

Thanks a lot for the feedback.

I ensured that I am using make/D now...

I have been building my own fit functions since MPF2 (which is awesome) didn't allow me flexible constraints on my peak fitting. I ran into errors when specifying those constraints (ages ago, can't remember the details).

The other thing is that I need to "remote control" MPF2 and fit datasets automatically. I spent some time trying to figure out remote control but couldn't. I also want to display my results dynamically without discovering conflicting hooks in MPF2. Is there a package demoing these features, for example?

j
You're probably better off doing what you're doing. You can use the MPF2 engine without getting into all the UI stuff. But it's not particularly easy.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thanks.

By the way, I just noticed that hiding graphs* was the reason I was getting a small error message in MPF2: a hidden notebook causes an error on a line
                Notebook $(s.winName), selection={startOfFile, endOfFile}
.

*Hiding graphs by Misc -> Miscellaneous Settings -> Miscellaneous -> Minimize Icon is Hide [enable]