using Plots
x = range(-10, 10, step=0.1)
y1 = x.^2
y2 = x.^3;
plot(x, y1)
plot!(x, y2)
data = x .+ 2 .* (rand(length(x)) .- 0.5)
201-element Vector{Float64}:
-10.31719114891199
-9.252777808251585
-9.727274506201773
-10.533496184224852
-10.124384166706065
-10.469282901901282
-9.34864114762013
-8.38139904171323
-9.692686210106158
-8.751845722515894
-8.322203422262488
-8.32868832229196
-8.908576450328301
⋮
8.235039042035996
9.899622717871985
9.967513447235895
9.300076417934994
9.689476207326404
10.120459761277047
9.559907388490089
8.957913889734394
9.772912793884611
8.972580732459075
10.825929492745397
9.945407457616637
plot(x, data, line=nothing, marker=:circ, legend=:topleft, label="noise")
plot!(x, x, line=(:solid, 2), label="original")
using LsqFit
model(x, p) = @. p[1] + p[2]*x
model (generic function with 1 method)
fit = curve_fit(model, x, data, [0., 0.])
fit.param
2-element Vector{Float64}:
-0.01447258312225462
1.0053215561142779
plot!(x, model(x, fit.param), line=(:solid, 2), label="fit")