GBA13
GBA13

Reputation: 215

Curve Fitting in Python not fitting my curve

I am currently doing a curve fit on some data to be input. My function is (where Ti is initial hot temp Ta is cold temp and c is the half life):

def extract_parameters(Ts, ts):
    def model(t, Ti, Ta, c):
        return (Ti - Ta)*math.e**(-t / c) + Ta
    popt, pcov = cf(model, ts, Ts, p0 = (10, 7, 6))
    Ti, Ta, c = popt
    xfine = np.linspace(0, 10, 101)
    yfitted = model(xfine, *popt)
    pl.plot(Ts, ts, 'o', label = 'data point')
    pl.plot(xfine, yfitted, label = 'fit')
    pylab.legend()
    pylab.show() 

When I enter:

extract_parameters(np.array([1,2,3,4,5,6,7,8,9,10]), np.array([10.0,9.0,8.5,8.0,7.5,7.3,7.0,6.8,6.6,6.3]))

My graph starts to fit right at the end but while my data starts at 10 my curve starts at about 240 and then sweeps down, which is not what I want. I thought setting p0 would help but it doesn't appear to help at all.

example of failed fit

Upvotes: 1

Views: 110

Answers (1)

Rufflewind
Rufflewind

Reputation: 8956

You have swapped Ts and ts in the argument to cf. It should be Ts (independent variable) follwed by ts (dependent variable).

popt, pcov = cf(model, Ts, ts, p0 = (10, 7, 6))

Upvotes: 1

Related Questions