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.4258118442636656
b = 0.10939648944557535 ± 1.342121650694185
Correlation coefficient, R² = 0.9689439025845498
Average square residue = 5.07983534386018
Predicted Y: ypred = [0.22986804206821387, 0.35525560018618085, ...]
residues = [0.11493274443951518, 0.17863051237333605, ...]
--------------------------------------------
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.24286901538176014, 1.0]
b = [-0.1916298763073778, 39941.12388845093]
c = -0.3783283409422839
Correlation coefficient, R² = 0.999920548280107
Average square residue = 0.00419185236967261
Predicted Y: ypred = [0.8970796652547437, 0.9355847716236423, ...]
residues = [-0.09251628959823255, -0.05202541438993313, ...]
-----------------------------------------------
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.2999999999999984, 0.699999999999997]
b = [-0.19999999999999976, 0.3333333333333474]
c = 0.0
Correlation coefficient, R² = 1.0
Average square residue = 1.7454780123179293e-29
Predicted Y: ypred = [0.9895959548529736, 0.9876101860135746, ...]
residues = [-2.6645352591003757e-15, -7.771561172376096e-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.