Gaussian Curve fit lazarus / 31-LCL ---------------------------------------------------------------------- CraigNZ · 2020-06-09 18:46 Is there a lazarus component or ActiveX DLL for fitting a Gaussian curve to a set of data?  The equation is of the form: y = a * exp(-(x-b)^2 / (2c^2)) where a,b, and c are determined in the solution to minimize the difference between the curve fit and the data. In my Lazarus program I would have an array of x,y values and starting values for a,b and c.  I would specify the formula as a string (or is predefined).  I would also provide boundaries on the number of solve iterations to keep the function from hanging the program in an infinite loop.  The result would be the error (chi squared?) and the optimized coefficients a, b and c. I found one "DataFitX" which is an ActiveX object but it is no longer available by the developer. Craig ---------------------------------------------------------------------- PaulRowntree · 2020-06-09 19:25 If you are willing to code it, the Levenberg-Marquardt or simplex algorithms will work well.  I took mine from Numerical Recipes in Pascal.  Can share if you pm me. ---------------------------------------------------------------------- mr-highball · 2020-06-09 19:27 I'm not sure if lazstats could accomplish what you're after since I haven't used it personally, but it may be worth a peak to see, original post, https://forum.lazarus.freepascal.org/index.php/topic,8437.15.html source, https://sourceforge.net/p/lazarus- ccr/svn/HEAD/tree/applications/lazstats/ @wp is the one who ported it to work with newer lazarus / maintaining so reaching out to them may help too ---------------------------------------------------------------------- wp · 2020-06-09 21:07 I did not see a non-linear fit when I worked with all the forms of LazStats. I once wrote a Levenberg-Marquart non-linear fit for Delphi which certainly can be adapted to Lazarus. But I do not want to publish it because it is based on the routines in the Numerical Recipes which are subject to copyright. A good alternative seems to me DMath, it contains a unit unlfit (non- linear fit) which should do the job. It is available via OPM. There is a rather thick pdf manual, however, it is not included in the OPM repository. To get it download the library from the main author's site (https://www.unilim.fr/pages_perso/jean.debord/tpmath/tpmath.htm) and extract the dmath.pdf (do not use these units since they are for Delphi); there are also some examples (but not for non-linear fitting, AFAICS), they are for Delphi, too, but shouldn't be too difficult to convert. There is also a unit JmLevenbergMarquardt in the JediMath package (https://sourceforge.net/projects/jedimath/). Not sure whether this is the best download site. If it does not compile send me a PM with your E-Mail and I can send you my port which at least compiles, but I did not test it seriously. ---------------------------------------------------------------------- CraigNZ · 2020-06-09 21:59 Thanks guys, I am working through all the links you have so generously provided. Craig ---------------------------------------------------------------------- wp · 2020-06-09 22:29 Here is a simple demo doing the gauss fit with the DMath library. The fit itself is just a few lines of code, amazing! (The download contains all the required files and should compile out of the box). ---------------------------------------------------------------------- mr-highball · 2020-06-09 23:38 Nice @wp ---------------------------------------------------------------------- VTwin · 2020-06-10 00:03 Very nice. I use DMath reqularly. Highly recommended. ---------------------------------------------------------------------- CraigNZ · 2020-06-10 02:32 Thanks heaps @wp, that is what I was looking for.  I will try it out and learn more about DMath.  Your example really helps me to get started using it. Craig ---------------------------------------------------------------------- marcov · 2020-06-10 06:33 Quote from: wp on June 09, 2020, 11:07:33 pmThere is also a unit JmLevenbergMarquardt in the JediMath package (https://sourceforge.net/projects/jedimath/). Not sure whether this is the best download site. If it does not compile send me a PM with your E-Mail and I can send you my port which at least compiles, but I did not test it seriously. I would advise against jedimath and dmath. I both had problems with them wrt exceptions (too often, no attempts at checking against zero, overflow etc). Since I only needed linear fitting (but with very scattered data with outliers), in the end I used tpmath, but put a loop around it that removes points with the highest Residu till the fit is good enough.  Needed to bound the number of points removed though, since two points always have a fit :-) ---------------------------------------------------------------------- mischi · 2020-06-10 08:57 Unless you need some kind of integration with other work from you, i always address such tasks as non-linear fitting with data analysis programs like Origin or IgorPro with a preference for the latter. ---------------------------------------------------------------------- wp · 2020-06-10 10:17 Quote from: wp on June 09, 2020, 11:07:33 pmA good alternative seems to me DMath... Another option might be LMath (https://sourceforge.net/p/lmath- library/code/HEAD/tree/) which is based on DMath but is still actively developed. ---------------------------------------------------------------------- avra · 2020-06-10 12:44 Quote from: wp on June 10, 2020, 12:17:00 pmQuote from: wp on June 09, 2020, 11:07:33 pmA good alternative seems to me DMath... Another option might be LMath (https://sourceforge.net/p/lmath- library/code/HEAD/tree/) which is based on DMath but is still actively developed.Thank you for pointing out at LMath effort. I was not aware of it. ---------------------------------------------------------------------- glorfin · 2020-06-10 14:57 Quote from: wp on June 10, 2020, 12:17:00 pmQuote from: wp on June 09, 2020, 11:07:33 pmA good alternative seems to me DMath... Another option might be LMath (https://sourceforge.net/p/lmath- library/code/HEAD/tree/) which is based on DMath but is still actively developed. As a supporter of LMath, I would recommend to download files from here: https://sourceforge.net/projects/lmath-library/files/LMath/ I use Trunk for development and commit changes before proper testing. By the way, if somebody would like to contribute, give feedback or ideas, you are welcome. ---------------------------------------------------------------------- wp · 2020-06-10 15:15 You could write a PM to GetMem, or respond to https://forum.lazarus.freepascal.org/index.php/topic,34297.0.html, and ask for adding the latest released LMath to Online-Package-Manager. This way Lazarus users would find your library would easier. ---------------------------------------------------------------------- PaulRowntree · 2020-06-10 16:59 Quote from: avra on June 10, 2020, 02:44:13 pmQuote from: wp on June 10, 2020, 12:17:00 pmQuote from: wp on June 09, 2020, 11:07:33 pmA good alternative seems to me DMath... Another option might be LMath (https://sourceforge.net/p/lmath- library/code/HEAD/tree/) which is based on DMath but is still actively developed.Thank you for pointing out at LMath effort. I was not aware of it. Is LMath the full updated version of DMath or a subset? ---------------------------------------------------------------------- glorfin · 2020-06-10 17:09 Quote from: PaulRowntree on June 10, 2020, 06:59:14 pmIs LMath the full updated version of DMath or a subset? Means to compile it as a shared library were excluded, instead it is organized as a set of Lazarus packages. Everything else is preserved, with some additions.