| 1 |
+++ |
| 2 |
title = "lmfit minimizer" |
| 3 |
weight = 40 |
| 4 |
+++ |
| 5 |
|
| 6 |
## lmfit minimizer |
| 7 |
|
| 8 |
This example demonstrates how to run a typical fitting task in BornAgain |
| 9 |
using the [lmfit](https://lmfit.github.io/lmfit-py/) package. |
| 10 |
|
| 11 |
```python |
| 12 |
import lmfit |
| 13 |
|
| 14 |
params = lmfit.Parameters() |
| 15 |
params.add('radius', value=7*nm, min=5*nm, max=8*nm) |
| 16 |
params.add('length', value=10*nm, min=8*nm, max=14*nm) |
| 17 |
|
| 18 |
fit_objective = ba.FitObjective() |
| 19 |
fit_objective.addFitPair(run_simulation, exp_data) |
| 20 |
fit_objective.initPrint(10) |
| 21 |
|
| 22 |
result = lmfit.minimize(fit_objective.evaluate_residuals, params) |
| 23 |
print(lmfit.fit_report(result)) |
| 24 |
``` |
| 25 |
|
| 26 |
The complete script is shown below. |
| 27 |
|
| 28 |
{{< show-ex file="fit/scatter2d/lmfit_basics.py" >}} |