# Custom nonlinear curve fitting package?

Hi all!
I have a dataset that I want to fit a function to in the form of:
y = a x^4 + b x^3 + c x^2 + dx + e + f sin(gx + h)
I was trying to use LsqFit.jl , but the results are not great (unless I guess the parameters myself).
Is there another package that might do a better job?

My LsqFit.jl attempt looks like this (maybe I can tweak something for better results?):

``````function fit_strain_poly(time, strain, A, f, ϕ, a, b, c, d, e)
P = [A, f, ϕ, a, b, c, d, e]
t0 = time
@. model(t, p) = p*sin(2*pi*p*t - p) + p*t^4 + p*t^3 + p*t^2 + p*t + p
fit = curve_fit(model,t0,strain,P)
A_f = fit.param
f_f = fit.param
ϕ_f = fit.param
a_f = fit.param
b_f = fit.param
c_f = fit.param
d_f = fit.param
e_f = fit.param
strain_f = model(t0, fit.param)
return strain_f, A_f, f_f, ϕ_f, a_f, b_f, c_f, d_f, e_f
end

fit_s1_poly, A1_poly, f1_poly, ϕ1_poly, a1_poly, b1_poly, c1_poly, d1_poly, e1_poly = fit_strain_poly(time_1, ε_1,4e-2, 3.36e-3, 9e-1, 0.1, 0.001, 0.1, 0.2, 0.1);
``````
1 Like

Adding that specific fit is on my todo list for SinusoidalRegressions. The algorithm I have in mind is based on this document, page 54. Its main advantage is that it does not require initial estimates. I hope I have time to implement it soon, but PRs are welcome too!

1 Like