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

**URL:** <https://discourse.julialang.org/t/hello-all-ive-been-trying-to-use-lsqfit-to-fit-a-hyperbolic-tangent-function-t/55649>\
**Category:** General Usage\
**Tags:** fit\
**Created:** [February 19, 2021, 8:29pm UTC](https://discourse.julialang.org/t/hello-all-ive-been-trying-to-use-lsqfit-to-fit-a-hyperbolic-tangent-function-t/55649 "2021-02-19T20:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 19, 2021, 8:29pm UTC](https://discourse.julialang.org/t/hello-all-ive-been-trying-to-use-lsqfit-to-fit-a-hyperbolic-tangent-function-t/55649/1 "2021-02-19T20:29:41Z")

</div>

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 -

```julia
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

```julia
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

```julia
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 :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1613766545245600?thread_ts=1613766545.245600&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 19, 2021, 8:41pm UTC](https://discourse.julialang.org/t/hello-all-ive-been-trying-to-use-lsqfit-to-fit-a-hyperbolic-tangent-function-t/55649/2 "2021-02-19T20:41:20Z")

</div>

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](https://transfer.sh/seiAb/0.0grams.csv)

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 20, 2021, 7:33am UTC](https://discourse.julialang.org/t/hello-all-ive-been-trying-to-use-lsqfit-to-fit-a-hyperbolic-tangent-function-t/55649/3 "2021-02-20T07:33:17Z")

</div>

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 -

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