Exponential fits
Use the fitexp (or fitexponential) function for a single exponential:
using EasyFit, Plots, Random
Random.seed!(1)
x = sort(rand(10))
y = @. 0.3 * exp(5x) + 0.1 * rand()
fit = fitexp(x, y)------------ Single Exponential fit -----------
Equation: y = a exp(-x/b) + c
With: a = 0.3050834373658658
b = -0.20074766557145926
c = 0.03012428890578168
Correlation coefficient, R² = 0.9999880977348544
Average square residue = 0.0006642534841694786
Predicted Y: ypred = [ 0.374112770172065, 0.4198833422319349, ...]
residues = [ 0.0009528362402029256, -0.04154735009274524, ...]
-----------------------------------------------
scatter(x, y, label="data", framestyle=:box)
plot!(fit.x, fit.y, label="mono-exponential fit", linewidth=2)Add n=N for a sum of N exponentials:
y2 = @. 0.3 * exp(5x) + 0.7 * exp(-3x) + 0.05 * rand()
fit2 = fitexp(x, y2, n=2)-------- Multiple-exponential fit -------------
Equation: y = sum(a[i] exp(-x/b[i]) for i in 1:2) + c
With: a = [0.301865881112783, 0.7342486782729205]
b = [-0.20021482019934322, 0.3871205673646169]
c = -0.020626490199642106
Correlation coefficient, R² = 0.9999983845518983
Average square residue = 8.535087603205752e-5
Predicted Y: ypred = [1.0097851531531887, 1.0119384471725585, ...]
residues = [-0.0010922579198962001, -0.00019432144731190704, ...]
-----------------------------------------------
scatter(x, y2, label="data", framestyle=:box)
plot!(fit2.x, fit2.y, label="bi-exponential fit", linewidth=2)The intercept c is fitted freely by default; it can be fixed to a constant value with the c keyword, and lower/upper bounds can be set as described in Bounds and fixed parameters:
fitexp(x, y2, n=2, c=0.0)-------- Multiple-exponential fit -------------
Equation: y = sum(a[i] exp(-x/b[i]) for i in 1:2) + c
With: a = [0.3009373697396999, 0.71542591752802]
b = [-0.200097247836303, 0.37373457414467537]
c = 0.0
Correlation coefficient, R² = 0.9999983827747133
Average square residue = 8.544481945015926e-5
Predicted Y: ypred = [1.0102044990115289, 1.0119959170492272, ...]
residues = [-0.0006729120615560635, -0.00013685157064324827, ...]
-----------------------------------------------