# Why is this scenario of function parameter estimation acting strangely? Using LsqFit

**URL:** <https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778>\
**Category:** General Usage\
**Tags:** package\
**Created:** [August 12, 2020, 4:05am UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778 "2020-08-12T04:05:53Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [August 12, 2020, 9:15am UTC](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778/2 "2020-08-12T09:15:30Z")

</div>

well seems to be related to [this thread](https://discourse.julialang.org/t/julia-vs-gnu-octave-for-plot-fitting-finding-peaks/42237/2). Note that Levenberg Marquardt is not the robustest Method for such kind of problems (multiple local minima).

Optim.jl provides a simple SA-Optimizer which tend to be more robust for this task

```julia
using Optim

loss(u) = sum((data .- model(t,u)).^2)
res = optimize(loss, p0_bounds, SimulatedAnnealing())

# Plot result
scatter(t, data)
plot!(t, model(t, Optim.minimizer(res)))

```

Or you can try the awesome [BlackBoxoptim.jl](https://github.com/robertfeldt/BlackBoxOptim.jl)

EDIT: To elaborate a bit on this:  
By simply plotting the value of `loss(u)` around the `p₀` you can visualize how challenging your problem actually is:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf304f94094e214cdcf5c733522b6a9f415a6a9f.png)

you can see multiple local minima next to the global one

---

_[View the full topic](https://discourse.julialang.org/t/why-is-this-scenario-of-function-parameter-estimation-acting-strangely-using-lsqfit/44778)._
