# Curve fitting with NonlinearSolve.jl

**URL:** <https://discourse.julialang.org/t/curve-fitting-with-nonlinearsolve-jl/119238>\
**Category:** General Usage\
**Tags:** curve-fitting\
**Created:** [September 10, 2024, 6:50am UTC](https://discourse.julialang.org/t/curve-fitting-with-nonlinearsolve-jl/119238 "2024-09-10T06:50:56Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![juluisca](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@juluisca](https://discourse.julialang.org/u/juluisca)\
**Post date:** [December 24, 2024, 1:34am UTC](https://discourse.julialang.org/t/curve-fitting-with-nonlinearsolve-jl/119238/10 "2024-12-24T01:34:14Z")

</div>

Updated to v1.11 and all running smoothly. Just did tiny changes to garrek code:

using NonlinearSolve

```
model(x, p) = @. p[1] * exp(-x / p[2]) + p[3]

xdata = range(0, 10, length=100)
ydata = model(xdata, [1.0, 2.0, 0.0]) .+ 0.1 * randn(length(xdata))

function residual(p, data)
    x = data[1]
    y = data[2]
    Y_pred = @. p[1] * exp(-x / p[2]) + p[3]
    return Y_pred .- y
end

p0 = [0.8, 0.4, 0.2]
data = [xdata, ydata]
prob = SciMLBase.NonlinearLeastSquaresProblem(residual, p0, data)
res = solve(prob, LevenbergMarquardt())
return res

```

plot(xdata, ydata)  
plot!(xdata, model(xdata, prob))

Thanks !!!

---

_[View the full topic](https://discourse.julialang.org/t/curve-fitting-with-nonlinearsolve-jl/119238)._
