LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Constrained Non-linear Fit do not opimalize if bounds array is connected

Solved!
Go to solution

I try to fit gauss function with Constrained Non-linear Fit, I get different results with or without set bounds.

When i set bounds, then the fit is not optimalize and the result parameters are same as the initial parameters.

If the bounds array is not conected then the fit get optimalized. Strange fatc is that, the best fit parameters are in between the bounds.

I do not understand why i dont recieve same result in both cases (best fit parameters are not found when bounds are conected).

Please can someone explain this behiavor and how to fix setting to get optimal result even with bounds connected.

 

 

Front panelFront panel 

Block Diagram.png

0 Kudos
Message 1 of 7
(2,473 Views)

Any reason you are not using Gaussian Peak fit?

0 Kudos
Message 2 of 7
(2,459 Views)

That said, your bounds are reversed (you swapped lower bounds with upper bounds, leaving no valid range)

 

altenbach_0-1590785928747.png

 

0 Kudos
Message 3 of 7
(2,434 Views)
Solution
Accepted by OpiCAN00

Your code was such a mess that I tried to clean it up a bit for better troubleshooting.

 

Model:

You must be coming from a text programming background, because all you really need is the following (No FOR loop, No formula node, etc.):

 

altenbach_0-1590785398321.png

 

 

Bounds generation:

Your are inputting the existing parameters as array AND as scalars. Seems redundant. All you need is an array of parameters and an array of % freedom values (note that I reversed upper and lower bounds for correct function!):

 

altenbach_1-1590785556404.png

 

Attached is a quick attempt at cleanup. Modify as needed.

Message 4 of 7
(2,426 Views)

Thank You very much,

my dumb mistake to swap the bounds. I agree the code which i posted is kinda mess. I was overworked and copy/paste some code artefacts from different projects. I use formula node in model function and Constrained Non-linear Fit, because in near future i will need to extend code to fit more (asymmetric) gausses in one spectra and also one function with 28 parameters (It seems to me easier to write formula than combine many mathematical functions.). I wanted to learn how Constrained Non-linear Fit  work and I thought this way of solve fitting would be best for my tasks. I see the code can even look nice if it is optimized. I have to work on my programming style :).

0 Kudos
Message 5 of 7
(2,391 Views)
Solution
Accepted by OpiCAN00

@OpiCAN00 wrote:

my dumb mistake to swap the bounds. .


Not necessarily dumb. If I had written the implementations I would sort the limits so we always have a valid range and we can just call them limit A and limit B. Whatever is smaller is the lower limit automatically. 😉

 


@OpiCAN00 wrote:

because in near future i will need to extend code to fit more (asymmetric) gausses in one spectra and also one function with 28 parameters (It seems to me easier to write formula than combine many mathematical functions.).


Well, if one Gaussian is simpler graphically, several gaussians are also simpler than a formula node. Here's a very old example for a model that automatically scales to the number of gaussians based on the number of parameters (note that the width and area are define differently, so rescale as needed). You would have one additional parameter for asymmetry.

 

altenbach_0-1590855956460.png

 

Message 6 of 7
(2,366 Views)

Well, it is nice alogrithm. It will be useful for sure. Thanks 🙂

0 Kudos
Message 7 of 7
(2,357 Views)