Hello all. I’ve been trying to use LsqFit
to fit a hyperbolic tangent function to some data about terminal velocity in free fall. Using SciPy’s curve_fit
function, it seems to work fine. However, in Julia, it’s nowhere near the correct fit.
Here’s the function I’ve written to fit the curve -
function fit_terminal_velocity(data::CSV.File)
# Create base model with random initial parameters
p0 = rand(2)
model(t, p) = p[1] .* tanh.(p[2]/p[1] .* t)
# We are fitting v(t), so lets get those values
# We also want to go from t = 0 to t = t_max
t = data.t .- minimum(data.t)
y = data.y
v = diff(y) ./ diff(t)
tv = t[1:end-1]
fitted_model = lsqfit.curve_fit(model, v, tv, p0)
return fitted_model, model
end
And it gives
p[1] = -1.04293e-10
p[2] = 0.00917912
Whereas Python gives p = [-623.23056677, -319.5100746 ]
, which fit the model correctly. For reference, the code in python is
f = lambda t, g, vt : vt * np.tanh(g/vt * t)
popts, pcov = curve_fit(f, t[1:], v)
Where curve_fit
is scipy.optimize.curve_fit
.
I think it may be something to do with the fact that LsqFit requires p0
but SciPy doesn’t? I would really appreciate if anybody can help me figure out why curve fitting isn’t working in Julia properly. Thank you so much!
Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message ) (More Info)