ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 04 Aug 2014 20:36:38 -0500How to fit data to an arrhenius equationhttps://ask.sagemath.org/question/23690/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](https://en.wikipedia.org/wiki/Arrhenius_equation) of a [continuous stirred tank reactor](https://en.wikipedia.org/wiki/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
data2 = list(csv.reader(file('manuel2.csv'), delimiter=';'))
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](http://www.sagemath.org/doc/reference/numerical/sage/numerical/optimize.html) I tried to use the find_fit function to fit my data to the [arrhenius-equation](https://en.wikipedia.org/wiki/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.Mon, 04 Aug 2014 08:20:33 -0500https://ask.sagemath.org/question/23690/how-to-fit-data-to-an-arrhenius-equation/Answer by kcrisman for <p>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 <a href="https://en.wikipedia.org/wiki/Arrhenius_equation">arrhenius equation</a> of a <a href="https://en.wikipedia.org/wiki/Continuous_stirred-tank_reactor">continuous stirred tank reactor</a>-model.
So far, I've managed to import data from a .csv-file and convert it into a matrix:</p>
<pre><code>import numpy as np
import csv
R = 8.314462 [J/mol/K]
yield = []
temp = []
t_res = [] #residence time
conc_init = [] #initial concentration
data2 = list(csv.reader(file('manuel2.csv'), delimiter=';'))
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
</code></pre>
<p>from <a href="http://www.sagemath.org/doc/reference/numerical/sage/numerical/optimize.html">here</a> I tried to use the find_fit function to fit my data to the <a href="https://en.wikipedia.org/wiki/Arrhenius_equation">arrhenius-equation</a></p>
<pre><code>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)
</code></pre>
<p>when I finally evaluate I get this error:</p>
<blockquote>
<p>(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</p>
</blockquote>
<p>I actually don't have a clue what that means or how I can get this to work.</p>
https://ask.sagemath.org/question/23690/how-to-fit-data-to-an-arrhenius-equation/?answer=23693#post-id-23693Agreed 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](http://trac.sagemath.org/ticket/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](http://mathematica.stackexchange.com/questions/5362/finding-a-fit-to-a-multi-dimensioned-function).Mon, 04 Aug 2014 20:36:38 -0500https://ask.sagemath.org/question/23690/how-to-fit-data-to-an-arrhenius-equation/?answer=23693#post-id-23693