# Hello all. I've been trying to use `LsqFit` to fit a hyperbolic tangent function t

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.

1 Like

Hello, I am the poster on slack. If anybody has a solution to this it would be greatly appreciated
Here is the data I’m trying to fit - https://transfer.sh/seiAb/0.0grams.csv

1 Like

Hello everybody!

I figured out the answer! Python’s `scipy.optimize.curve_fit` sets the array of initial parameters to ones if not supplied. `LsqFit` on the other hand does not try to determine the parameters automagically and thus needs them passed in. SciPy sets the initial parameters all to ones if not passed, so if I set the initial parameters to ones in Julia as well, I get the exact same fit. So the new code looks like -

``````function fit_terminal_velocity(data::CSV.File)
# Create base model with random initial parameters
p0 = ones(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
``````

(changed `rand(2)` to `ones(2)`)
Solved!

2 Likes