From 3de4b35e0efa5c21c7ab262d48e1c3797ac7b629 Mon Sep 17 00:00:00 2001 From: Leandro Martinez Date: Thu, 26 Sep 2024 13:30:16 -0300 Subject: [PATCH] simplify find best fit loop --- src/find_best_fit.jl | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/find_best_fit.jl b/src/find_best_fit.jl index 82e7960..106ae43 100644 --- a/src/find_best_fit.jl +++ b/src/find_best_fit.jl @@ -13,17 +13,16 @@ function find_best_fit(model, X, Y, np, options, lower, upper) try initP!(p0, options, lower, upper) fit = curve_fit(model, X, Y, p0, lower=lower, upper=upper) - sum_residues = sum(fit.resid .^ 2) - if abs(sum_residues - best) < options.besttol - nbest = nbest + 1 - if sum_residues < best + sum_residues = sum(abs2, fit.resid) + fit_improve = sum_residues - best + if fit_improve < options.besttol + if fit_improve < 0 + nbest = 1 best = sum_residues best_fit = deepcopy(fit) + else + nbest = nbest + 1 end - elseif sum_residues < best - nbest = 1 - best = sum_residues - best_fit = deepcopy(fit) end catch msg if options.debug