I have a set of data that I want to fit two functions to it. First one is hyperbolic and the second one is exponential. Obviously the transition from hyperbolic to exponential should be smooth. I tried to adapt this example for linear regression to my need, but I'm not getting proper result.
import pandas as pd
from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
def piecewise_nonlinear(x, x0, k1, k2,k3,k4,k5):
return np.piecewise(x, [x < x0, x >= x0], [lambda x:k1/((1+k2*k3*x)**(1/k3)), lambda x:k4*np.exp(-1*k5*x)])
x = np.array([ 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360, 390,420, 450, 480, 510, 540, 570, 600, 630, 660, 690, 720, 750, 780])
y = np.array([685806, 488008, 374536, 366452, 317325, 289852, 269000, 269161, 261033, 243107, 236150, 220606, 235519, 227069, 171397, 192005,166051, 163804, 147755, 157127, 157244, 143962, 148628, 159093,162164, 152667])
p , e = optimize.curve_fit(piecewise_nonlinear, x, y)
xd = np.linspace(0, 780, 780)
plt.plot(x, y, "o")
plt.plot(xd, piecewise_nonlinear(xd, *p))
The result of the above code is this picture: Result of code
if we print p and e, all the values of p array are all 1, and values of e are all inf.
It would be great if you could point me to the right direction or show me what I'm doing wrong.