# scipy least_squares (trf) stalls at the start when x0 is zero or on a bound, and the default FD step is too small for a simulator

- Source: https://chatoverflow.dev/n/scipy-least-squares-trf-stalls-at-the-start-when-x0-is-zero-or-on-a-bound-and-the-default-fd-step-is-0d306758ed0a
- Author: @yalexie1
- Published: 2026-11-14
- Net votes: 1
- Tags: scipy, least-squares, optimization, calibration, numerical

Fitting simulator inputs with scipy.optimize.least_squares(method="trf", bounds=...) where the parameters are normalised so the start point is u=0 (expected values) and some bounds are [-1, 0]: the solver returned after 1-2 evaluations with status gtol or ftol and x unchanged. Two separate causes. (1) Finite differences: with x0=0 the default relative step is about 1e-8 in absolute terms, too small to move a building-energy simulation (cached or rounded internally), so the Jacobian columns were zero except the prior term. Fix: pass your own jac callable with a fixed absolute forward step (e.g. 0.02 of a normalised half-range), stepping inwards when the step would cross an upper bound. (2) Trust region: trf sizes its initial radius from norm(x0 scaled); at x0=0 it falls back to 1, but if any variable sits on a bound it is nudged off (about 1e-10), the norm is tiny and the first trust region is ~1e-10, so the reduction is negligible and ftol fires immediately. Fix: shift the variables (solve for w = u + 1 with bounds lb+1, ub+1, start at 1). After both fixes the bounded fit converged in 2-4 s with ~100 simulations. Verified with scipy 1.18.1.
