### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: More complex functions for fitting  (Read 895 times)

#### prema

• New Member
• Posts: 16
##### More complex functions for fitting
« on: August 07, 2024, 02:23:03 pm »
Good afternoon,

I'm developing a program for diffraction data visualisation and possible fitting. I like your fitting series, and it works quite nicely. But I would need to fit data with some more complex functions such as Gaussian, Lorentzian or pseudo-Voigt. They have multiple parameters. I can write the code to calculate them. But currently (I followed your fitting demo), the TFitEquqtion = feCustom allows only a sum of basic functions for each parameter. With the feCustom, there is no possibility to replicate, for example, the exponential function (a*exp(bx)) because only one parameter is passed to the basic function, but the exponential needs two.

Can someone suggest a direction on how one could proceed? Thank you in advance.

With best regards

Premek

#### wp

• Hero Member
• Posts: 12280
##### Re: More complex functions for fitting
« Reply #1 on: August 07, 2024, 02:41:16 pm »
You are right, the fitting procedures in TAChart are valid only for functions which are linear or can be linearized. A Gaussian fit is not possible, it would require implementing a non-linear fiiting method (Levenberg-Marquardt or so) which I don't plan to do for the moment.

You could use the LMath library (search for it, I think it is on SourceForge) or the DMath library which is in Online-Package-Manager (but I never used it and cannot judge how well it performs in Lazarus (the "D" stands for "Delphi"),,,). These libraries contain a set of non-linear fitting procedures, Gaussian, Logistic etc. You then can take the fit results and calculate the fit curves by means of TACharts TFunctionSeries.

You can find an LMath example is in my gitub repository https://github.com/wp-xyz/LazSamples/tree/master/math/lmath/gaussfit.

#### prema

• New Member
• Posts: 16
##### Re: More complex functions for fitting
« Reply #2 on: August 08, 2024, 08:22:05 am »
Thanks a lot for the suggestions. I know DMath. I tested it, and it forks ± fine, but it is quite complicated to fix some parameters for the fit. I will try to think about it in more detail.