Bounds and fixed parameters
Lower and upper bounds can be set on the parameters of most functions using the l=lower(...) and u=upper(...) keyword arguments. For example:
using EasyFit, Random
Random.seed!(1)
x = sort(rand(10))
y = sort(rand(10))
fitlinear(x, y, l=lower(a=5.0), u=upper(a=10.0))------------------- Linear Fit -------------
Equation: y = ax + b
With: a = 5.0 ± 2.425811844259398
b = 0.10939648944064116 ± 1.3421216506918239
Correlation coefficient, R² = 0.96894390258455
Average square residue = 5.079835343842307
Predicted Y: ypred = [0.2298680420632797, 0.3552556001812467, ...]
residues = [0.11493274443458101, 0.17863051236840188, ...]
--------------------------------------------
y2 = @. 0.3 * exp(5x) + 0.7 * exp(-3x)
fitexp(x, y2, n=2, l=lower(a=[0.0, 0.0]), u=upper(a=[1.0, 1.0]))-------- Multiple-exponential fit -------------
Equation: y = sum(a[i] exp(-x/b[i]) for i in 1:2) + c
With: a = [0.24286901538049652, 0.9999999414631084]
b = [-0.19162987630718206, 39941.12383937575]
c = -0.37832765630400506
Correlation coefficient, R² = 0.9999205482801068
Average square residue = 0.004191852369330125
Predicted Y: ypred = [0.897080291354768, 0.9355853977235492, ...]
residues = [-0.09251566349820828, -0.052024788290026214, ...]
-----------------------------------------------
Bounds on the intercepts or limiting values are not supported directly, but it is possible to fix them to a constant value instead. For example:
fitlinear(x, y, b=5.0)------------------- Linear Fit -------------
Equation: y = ax + b
With: a = -6.185371264876846 ± 3.1224947249739716
b = 5.0 ± 1.7275733006563927
Correlation coefficient, R² = 0.9689439025845495
Average square residue = 8.416641666626477
Predicted Y: ypred = [4.850967744034566, 4.695854024243377, ...]
residues = [4.736032446405868, 4.519228936430532, ...]
--------------------------------------------
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.2999999999999992, 0.699999999999998]
b = [-0.19999999999999987, 0.3333333333333413]
c = 0.0
Correlation coefficient, R² = 1.0
Average square residue = 8.940012727449997e-30
Predicted Y: ypred = [0.9895959548529747, 0.9876101860135749, ...]
residues = [-1.5543122344752192e-15, -5.551115123125783e-16, ...]
-----------------------------------------------
The normalized exponential-decay fit, fitexpdecay (see Normalized exponential decay), follows the same convention for its constant term c; its weights and decay rates instead follow built-in constraints ($\sum_i a_i + c = 1$ and $b_i > 0$) rather than user-set bounds.