Error: [only length-1 arrays can be converted to Python scalars] when changing variable order
Question: Dear Stackoverflow Community,

I am very new to Python and to programming in general, so please don't get mad
when I don't get your answers and ask again.

I am trying to fit a curve to experimental data with
scipy.optimization.curve_fit. This is my code:

    
    
    %matplotlib inline
    import matplotlib.pyplot as plt
    import numpy as nm
    from __future__ import division
    import cantera as ct
    from matplotlib.backends.backend_pdf import PdfPages
    import math as ma
    import scipy.optimize as so
    
    
    R = 8.314
    T = nm.array([700, 900, 1100, 1300, 1400, 1500, 1600, 1700])
    k = nm.array([289, 25695, 763059, 6358040, 14623536, 30098925, 56605969, 98832907])
    
    
    def func(A, E, T):
        return A*ma.exp(-E/(R*T))
    popt, pcov = so.curve_fit(func, T, k)
    

Now this code works for me, but if I change the function to:

    
    
    def func(T, A, E)
    

and keep the rest I get:

    
    
    TypeError: only length-1 arrays can be converted to Python scalars
    

Also I am not really convinced by the Parameter solution of the first one. Can
anyone tell me what happens when you change the variable order?


Answer: I wonder whether you data shows an exponential decay of rate. The mathematical
model may not be the most suitable one.

![enter image description here](http://i.stack.imgur.com/gSt4y.png)See the doc
string of `curve_fit`

> f : callable The model function, f(x, ...). It must take the independent
> variable as the first argument and the parameters to fit as separate
> remaining arguments.

since your formula is essentially: `k=A*ma.exp(-E/(R*T))`, the right order of
parameters in `func` should be `(T, A, E)` or `(T, E, A)`.

Regarding the order of `A` and `E`, they don't really matter. If you flip
them, the result will get flipped as well:

    
    
    >>> def func(T, A, E):
        return A*ma.exp(-E/(R*T))
    
    >>> so.curve_fit(func, T, k)
    (array([  8.21449078e+00,  -5.86499656e+04]), array([[  6.07720215e+09,   4.31864058e+12],
           [  4.31864058e+12,   3.07102992e+15]]))
    >>> def func(T, E, A):
        return A*ma.exp(-E/(R*T))
    
    >>> so.curve_fit(func, T, k)
    (array([ -5.86499656e+04,   8.21449078e+00]), array([[  3.07102992e+15,   4.31864058e+12],
           [  4.31864058e+12,   6.07720215e+09]]))
    

I didn't get your `typeerror` at all.

