Python Forum
Using scipy.optimize: curve_fit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using scipy.optimize: curve_fit
#1
Hey there,

I would like to model exponential data by a curve_fit with scipy. The fit fits well for small x-values, but less so for bigger x-values. Is there anything I can do to improve the overall fit? Ideally, the measured data and fit should meet at the horizontal line of y = 0.01, since the slope in this area is important.

I have tried "trimming" the measured data, i.e. taking out small or large x-values, but did not really find a good solution. Are there any hinds or recommendations, what I could do to find a better solution? I have checked the documentation, but did not find anything useful for this specific case. I have also tried to combine my function with a scypy.optimize.minimize, but this did not work out either.

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit 

# Create x and y values in dataframe
x_022_i = [2.0, 8.0, 14.0, 20.0, 26.0, 32.0, 38.0, 44.0, 50.0, 56.0, 62.0, 68.0, 74.0, 80.0, 92.0, 98.0, 104.0]
y_022_i = [1.043, 1.053, 1.343, 0.746, 0.544, 0.453, 0.248, 0.147, 0.056, 0.046, 0.041, 0.024, 0.011, 0.004, 0.002, 0.001, 0.001]

d = {'x_022_i': x_022_i, 'y_022_i': y_022_i}
df = pd.DataFrame(data=d)

# Define functions
def fun_datafit(xdata, ydata):
    popt, pcov = curve_fit(func,xdata,ydata)
    perr = np.sqrt(np.diag(pcov))
    return popt, pcov, perr

def func(x, b): 
    return np.exp(-b*x)

# Determine optimal parameter
popt_022_i, pcov_022_i, perr_022_i = fun_datafit(df["x_022_i"], df["y_022_i"])
b_opt = popt_022_i[0] 
print(popt_022_i[0])

# y-values fitted
y_022_i_fit = func(df["x_022_i"], b_opt)

# Plot
print(y_022_i_fit.iloc[12:13])
plt.plot(x_022_i,y_022_i,label="exp",marker='o', linestyle= 'None') # original data
plt.plot(x_022_i,y_022_i_fit, label="fit") # fit
plt.hlines(0.01,0,100, color='k') # 1% concentration
plt.xlabel("Time")
plt.ylabel("Normalized concentration")
plt.yscale("log")
plt.legend()
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to optimize analog gauge reader? kadink 0 762 May-19-2023, 08:58 PM
Last Post: kadink
  SOLVED: scipy.optimize.least_squares problem Skytter13 2 2,846 Mar-06-2022, 10:17 AM
Last Post: Skytter13
  Help with Scipy optimize for numerical problem Jbjbjb1 0 1,564 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1
  How to get coefficient of determination R2 after scipy.curve_fit? AlekseyPython 0 1,880 Feb-12-2021, 09:03 AM
Last Post: AlekseyPython
  scipy.optimize.basinhopping generates unstable output bb19x11 0 1,664 Mar-09-2020, 04:07 PM
Last Post: bb19x11
  scipy curve_fit for multiple independent variables Jay_Nerella 1 7,203 May-08-2019, 02:08 AM
Last Post: scidam
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 22,746 Aug-18-2018, 02:27 AM
Last Post: scidam
  minimize with scipy.optimize tobenmoben 0 2,803 Feb-17-2018, 01:47 PM
Last Post: tobenmoben
  curve_fit rizal 3 4,307 Dec-20-2017, 04:53 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020