# How to fit data to an arrhenius equation

Hi, I'm pretty new to sage and am currently trying it out with cloud.sagemath and sage on ubuntu. I'm trying to fit some data to an arrhenius equation of a continuous stirred tank reactor-model. So far, I've managed to import data from a .csv-file and convert it into a matrix:

import numpy as np
import csv
R = 8.314462 [J/mol/K]

yield = []
temp = []
t_res = [] #residence time
conc_init = [] #initial concentration

m = matrix([[ float(_) for _ in line] for line in data2])

n = np.vstack(m)

##now I'm converting the matrix into single numpy-arrays:
yield = n[:,0]
temp = n[:,1]
t_res = n[:,2]
conc_init = n[:,4]
conc = conc_init - yield/100*conc_init #calculate actual concentrations


from here I tried to use the find_fit function to fit my data to the arrhenius-equation

var('E_A,A')
model(conc)=conc_init - A*exp(-E_A/(R*temp))*conc*t_res
data=(conc,conc_init,temp,t_res)
fit=find_fit(data,model)


when I finally evaluate I get this error:

(E_A, k_inf) Error in lines 36-36 Traceback (most recent call last): File "/projects/b9085414-cff2-4e5a-9c3b-872f47d41a7c/.sagemathcloud/sage_server.py", line 736, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/usr/local/sage/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/calculus/all.py", line 96, in symbolic_expression return SR(x) File "parent.pyx", line 1083, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:8902) File "coerce_maps.pyx", line 95, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4203) File "coerce_maps.pyx", line 90, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4110) File "ring.pyx", line 289, in sage.symbolic.ring.SymbolicRing._element_constructor_ (build/cythonized/sage/symbolic/ring.cpp:5332) TypeError

I actually don't have a clue what that means or how I can get this to work.

edit retag close merge delete

Sort by » oldest newest most voted

Agreed that the error is a little mysterious. Here is what is going on, in a simpler case where I just made the matrix matrix(2,[1,2,3,4]).

sage: A*exp(-E_A/(R*temp))
array([A*e^(-0.06013618199229246*E_A), A*e^(-0.03006809099614623*E_A)], dtype=object)


So far, so good.

sage: model(conc) = A*exp(-E_A/(R*temp))
<boom>


When we do this, as you can see at the top of the traceback, it creates a symbolic expression from the thing you put in it. Unfortunately, what symbolic expression would that be? Even

sage: symbolic_expression(temp)


yields the same error. We don't create symbolic expressions from arrays. (I've opened Trac 16761 for this.)

However, I'm not sure what the "right" thing here is. The documentation examples all have single-output functions, not vector-valued functions. That this may not have a well-defined one-size-fits-all solution is suggested by this question on the Mathematica list.

more