exp

mdhelper.fit.exponential.exp(x: ndarray, *args: float) ndarray[source]

General exponential model.

\[y=\sum_{k=1}^na_k\exp{(b_kx)}\]
Parameters:
xnumpy.ndarray

One-dimensional array containing \(x\)-values.

*argsfloat

Fitting parameters for the exponential term(s), ordered as \(a_1,\,b_1,\,a_2,\,b_2,\ldots,\,a_n,\,b_n\), where \(n\) is the number of terms in the model. As such, the number of variable positional arguments must be even.

Returns:
fitnumpy.ndarray

Fitted \(y\)-values.

Examples

Generate \(x\)- and \(y\)-values (with error), and then use scipy.optimize.curve_fit() to fit coefficients for a two-term exponential model.

>>> from scipy import optimize
>>> rng = np.random.default_rng()
>>> x = np.linspace(-0.1, 0.1, 10)
>>> err = (2 * rng.random(x.shape) - 1) / 10
>>> y = np.exp(-8 * x) + np.exp(12 * x) + err
>>> pk, _ = optimize.curve_fit(
        lambda x, a1, b1, a2, b2: exp(x, a1, b1, a2, b2), x, y
    )
>>> pk
array([ 1.13072662, -6.90042351,  0.88706719, 12.87854508])

Evaluate the fitted \(y\)-values using the coefficients.

>>> exp(x, *pk)
array([2.49915084, 2.25973234, 2.09274413, 2.00061073, 1.98962716,
       2.07080543, 2.26106343, 2.58486089, 3.07642312, 3.78274065])