tidy3d.plugins.autograd.functions.least_squares

Contents

tidy3d.plugins.autograd.functions.least_squares#

class least_squares[source]#

Bases:

Perform least squares fitting to find the best-fit parameters for a model function.

Parameters:
  • func (Callable[[np.ndarray, float], np.ndarray]) – The model function to fit. It should accept the independent variable x and a tuple of parameters, and return the predicted dependent variable values.

  • x (np.ndarray) – Independent variable data.

  • y (np.ndarray) – Dependent variable data.

  • initial_guess (Tuple[float, ...]) – Initial guess for the parameters to be optimized.

  • max_iterations (int = 100) – Maximum number of iterations for the optimization process.

  • tol (float = 1e-6) – Tolerance for convergence. The optimization stops when the change in parameters is below this threshold.

Returns:

The optimized parameters that best fit the model to the data.

Return type:

np.ndarray

Raises:

np.linalg.LinAlgError – If the optimization does not converge within the specified number of iterations.

Example

>>> import numpy as np
>>> def linear_model(x, a, b):
...     return a * x + b
>>> x_data = np.linspace(0, 10, 50)
>>> y_data = 2.0 * x_data - 3.0
>>> initial_guess = (0.0, 0.0)
>>> params = least_squares(linear_model, x_data, y_data, initial_guess)
>>> print(params)
[ 2. -3.]

Inherited Common Usage