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.
Solved! Go to Solution.
Your code was such a mess that I tried to clean it up a bit for better troubleshooting.
You must be coming from a text programming background, because all you really need is the following (No FOR loop, No formula node, etc.):
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!):
Attached is a quick attempt at cleanup. Modify as needed.
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 :).
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. 😉
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.