Skip to content

Commit

Permalink
fix: Improve root finding logic in fit_polynomial function to handle …
Browse files Browse the repository at this point in the history
…out-of-range roots more gracefully
  • Loading branch information
Akinori Machino committed Dec 26, 2024
1 parent 00b1186 commit 4a49f1a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/qubex/analysis/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,11 @@ def fit_polynomial(

roots = np.roots(fun)
real_roots = roots[np.isreal(roots)].real
root = real_roots[np.argmin(np.abs(real_roots))]
if root < np.min(x) or root > np.max(x):
print(f"No root found in the range [{np.min(x)}, {np.max(x)}].")
roots_in_range = real_roots[(real_roots >= np.min(x)) & (real_roots <= np.max(x))]
try:
root = roots_in_range[np.argmin(np.abs(roots_in_range))]
except ValueError:
print(f"No real root in the range for {target}.")
root = np.nan

fig = go.Figure()
Expand Down

0 comments on commit 4a49f1a

Please sign in to comment.