Python Forum
curve fitting matlotlib scipy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
curve fitting matlotlib scipy
#1
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

m=np.array([155.9,133.7,6.4,57.9])
t=np.array([100,150,200,250])


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
Reply
#2
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?
Reply
#3
does the abs() function even work in this sense for fitting(abs(a*(1-2*np.exp(-b*x)))?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sine fitting - extra modulation Pikano 2 989 Jan-04-2023, 07:41 AM
Last Post: Pikano
  Fitting transfer function model to FRF data ymohammadi 0 1,645 Feb-10-2022, 10:02 AM
Last Post: ymohammadi
  Gaussian Curve Fit using Scipy ODR 83dons 1 3,793 Feb-19-2021, 10:24 PM
Last Post: Tuxedo
  Error in running the Elliott Fitting function shashisourabh 9 5,152 Nov-19-2020, 06:54 PM
Last Post: osvan
  Stretching a curve and modifying it IlikePi 0 1,926 Feb-01-2020, 03:41 PM
Last Post: IlikePi
  Fitting experimental data with differential equations : Optimization madoko 6 6,758 Jan-17-2019, 11:30 AM
Last Post: scidam
  Fitting Lognormal Data Carolyn 3 15,943 May-21-2018, 12:10 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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