3

I'm given the following equation and asked to find a few different things one of which is the least squares estimate.

function

I have the experimentally determined data and write the function.

xData = np.array([0, 2, 5, 10, 15, 20, 25, 30]) # hours
yData = np.array([ 0.398,  0.543,  1.56,  4.34,  7.22, 9.86 , 10.5, 10.6]) # biomass, g/L

def func1(t,x_infin,x_0,k):
    'nonlinear function in a and b to fit to data'
    g = x_infin/ (1+(((x_infin-x_0)/x_0)*math.exp(-k*t)))
    
    return g

I then set up curve_fit()

initial_guess = [10.5,0.0,16.5] # intial guesses for x_infin, x_0 and k
pars, pcov = curve_fit(func1, xData, yData, p0 = initial_guess)

and get

only size-1 arrays can be converted to Python scalars

I'm not sure where I am passing an array where it shouldn't be. My understanding of the curve_fit() from the documentation is that I'm passing in the function, xdata ydata and then my guesses for the parameters.

Could someone point out where i'm going wrong?

1 Answer 1

3

curve_fit uses arrays under the hood for the t parameter while it does its thing.

math.exp only works on scalar inputs; use numpy.exp to work with arrays

Sign up to request clarification or add additional context in comments.

1 Comment

Also, beware that your guess isn't very good and the resulting fit will be way off. Remember that the lapse rate k of the exponential term will significantly affect how quickly your function increases. For a slower increase, like displayed in your data, guess a much lower value of k (~0.5 will work)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.