I'm working on a project and I need to fit some data to the equation
`y = bmax_1 * np.exp(-koff_1 * x) + bmax_2 * np.exp(-koff_2*x) with bmax_1, koff_1, bmax_2, koff_2`

being the parameters. I have tried using `curve_fit`

, but the result is quite poor and give an R-squared value of 0.16. I'm wondering if there is something I can do to improve the fit?

```
#Define model function
def func(x, bmax_1, koff_1, bmax_2, koff_2):
return bmax_1*np.exp(-koff_1*x) + bmax_2 * np.exp(-koff_2 *x)
# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
val = func(xData, *parameterTuple)
return np.sum((yData - val) ** 2.0)
def generate_Initial_Parameters():
parameterBounds = []
parameterBounds.append([0.0, 200.0]) # search bounds for bamx_1
parameterBounds.append([0.0, 10.0]) # search bounds for koff_1
parameterBounds.append([0.0, 200.0]) # search bounds for bmax_2
parameterBounds.append([0.0, 10.0]) # search bounds for koff_2
# "seed" the numpy random number generator for repeatable results
result = differential_evolution(sumOfSquaredError, parameterBounds, seed=4)
return result.x
column = input('Column to be analysed: ')
xData = df.loc[:, 'T']
yData = df.loc[:, column]
geneticParameters = generate_Initial_Parameters()
fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters, bounds = )
print('Fitted parameters:', fittedParameters)
```

The fitted parameters are `[1.24066146e+02 1.48240328e-02 1.34805335e+01 8.26108828e-01]`

Read more here: https://stackoverflow.com/questions/64945709/how-to-improve-curve-fit-for-data-with-multiphasic-exponential-equation

### Content Attribution

This content was originally published by Menglu Wu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.