Skip to content
Advertisement

Fitting data to a complementary error function with multiple variables in Python

I am having trouble to fit experimental data to a complementary error function in Python 3.7.4.

JavaScript

More precisely, I want to fit my data to the complementary error function consisting of the integrand function with the parameters a, b, c, and the cerf function doing the actual integration. The integration should go from x (the argument of the function) to +infinity. Afterwards, I wanted to use standard curve_fit from scipy. But now I am getting a value error as follows:

JavaScript

I would be really thankful, if someone knew how to do the fit of the function with the x-arguments as the lower boundary for the integral.

The data look like this:

JavaScript

Advertisement

Answer

According to the documentation, scipy.integrate.quad does not take arrays, and it cannot call a function with arguments. So, we have to construct a helper function f within the function that is addressed by scipy.curve_fit:

JavaScript

Sample output:

enter image description here

This approach is not the fastest – every x-value is integrated individually. Maybe there are other scipy.integrate functions that can work with numpy arrays; I would not know.
The part evaluating f instead of its integrated values is not really necessary here. But I used it initially to verify that cerf works as expected, so I left it in the script.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement