Curve fitting with NonlinearSolve.jl

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 !!!

1 Like