Python Forum
curve fitting matlotlib scipy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
curve fitting matlotlib scipy
Hi, Could someone please advise me how I can improve the fit of my curve such that the fit goes through all my points and resembles the graph below?

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


def exponential(x, a, b):
    return abs(a*(1-2*np.exp(-b*x)))

pars, cov = curve_fit(f=exponential, xdata=t, ydata=m, p0 = [0, 0], bounds=(-np.inf, np.inf))

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.scatter(t, m, s=50, color='#00b3b3', label='Data')
ax.plot(t, exponential(t, *pars), linestyle='--', linewidth=2, color='black')
[Image: 2227418_orig.gif]

I'm trying to fit the solid line. I'm expecting the fit would be like the dashed line with no abs() in the fit code (although clearly that's not what the data is doing).

Many thanks. Cam
As far as I can tell; my script does fit the points but it is doing it over a far larger region than I would like. I've experimented by changing the bounds, to no effect. I've also experimented with changing the initial estimate p0; varied y values cause the fit to become flat.

Besides the bounds and initial estimate p0, I can't see what the problem would. Do I perhaps need a different type of custom curve fitting?
does the abs() function even work in this sense for fitting(abs(a*(1-2*np.exp(-b*x)))?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error in running the Elliott Fitting function shashisourabh 8 943 Mar-27-2020, 04:19 PM
Last Post: Larz60+
  Stretching a curve and modifying it IlikePi 0 429 Feb-01-2020, 03:41 PM
Last Post: IlikePi
  Fitting experimental data with differential equations : Optimization madoko 6 1,926 Jan-17-2019, 11:30 AM
Last Post: scidam
  Fitting Lognormal Data Carolyn 3 7,504 May-21-2018, 12:10 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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