ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Oct 2020 07:05:17 -0500how to join functions with an intermediate fit to obtain smooth derivativeshttps://ask.sagemath.org/question/53909/how-to-join-functions-with-an-intermediate-fit-to-obtain-smooth-derivatives/I want to modify a function which contains a pole for numerical simulations, e.g. limit it to a maximum value around the pole.
My idea is to connect two original functions f1 and f2 (e.g. one with pole and one constant) with an intermediate fit function between two x values x1 and x2. The fit function should continue the two original functions between x1 and x2 as well as their first two derivatives, and it should be monotonic in this range.
I found some hints how to fit (x,y) points with a lagrange_polynomial or find_fit to match a given function template, but these approaches do not seem to be extendable to fit the derivatives at the same time, e.g. fit points from (f1,f1', f1'') and (f2,f2',f2''). At least this seems to me what needs to be done. Is this a feasible approach, and how can it be solved?
Constructing a set of splines between x1 and x2 might be an alternative, but the implementation in the simulation system would be more complicated compared to a single polynomial. I guess 3 spline segments might be sufficient to create a "connector template", but a subsequent fit of a single function to this set of splines (to obtain a simpler implementation for the simulation) would probably violate the continuity of the derivatives, and I worry that construction of suitable intermediate points to obtain a monotonic fit function is another complication. Thus, again, this seems to result in a general multi-function fitting problem to which a genius may have found a general approach already?gg@gWed, 14 Oct 2020 07:05:17 -0500https://ask.sagemath.org/question/53909/curve fitting coefficients.https://ask.sagemath.org/question/32057/curve-fitting-coefficients/get the model coefficients, but I can not use them to plot the graph. How to solve?
see the code:
dados = [(0, -0.183440428023042),
(0.200000000000000, -0.131101157495126),
(0.400000000000000, 0.0268875670852843),
(0.800000000000000, 0.110532679260319),
(1.00000000000000, 0.253944632998395),
(1.20000000000000, 0.257190123748649),
(1.40000000000000, 0.531888837111346),
(1.60000000000000, 0.579048247883555),
(2.00000000000000, 0.935180993484717),
(2.20000000000000, 0.916600344376623),
(2.60000000000000, 1.13328608090532),
(2.80000000000000, 1.26893326843583),
(3.00000000000000, 1.10202945535186),
(3.40000000000000, 1.13391615491257)]
point(dados,color = "red",size=20,legend_label="pontos coletados")
modelo(x) = a*x+b;modelo
a,b = find_fit(dados, modelo);a;b
modelo(x) = a*x+b
point(dados,color = "red",size=20,legend_label="pontos coletados") + plot(modelo(x),(x,0,3))
Traceback (click to the left of this block for traceback)
...
ValueError: Variable 'x' not foundjmarcellopereiraMon, 04 Jan 2016 12:36:40 -0600https://ask.sagemath.org/question/32057/How 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.jonijonesMon, 04 Aug 2014 08:20:33 -0500https://ask.sagemath.org/question/23690/find best fit for implicit equation.https://ask.sagemath.org/question/10588/find-best-fit-for-implicit-equation/Hi,
I have to fit numerical data to an analytic model in the same way that is done by the find_fit command of sage.
The problem is that my model is an implicit function which can't be simplified. It's the solution of a differential equation and it's quite messy.
Any ideas about how could I solve this with sage.ZardozThu, 03 Oct 2013 12:35:34 -0500https://ask.sagemath.org/question/10588/Piecewise curve fitting polynomial datahttps://ask.sagemath.org/question/9989/piecewise-curve-fitting-polynomial-data/Hello all. I am curve fitting time series data using polyfit() and it works well for most of my data sets. I have noticed, however, that some data sets begin hyperbolic and move to exponential so as to best fit to two separate equations. What is simple way to fit my data to two different curve equations using Sage? Is there a good mathematical or programmatic method of determining when a data set is best served by piecewise curve fitting? Currently, I can only determine that empirically once the curve is plotted along with the data points.
NatashaNatashaThu, 04 Apr 2013 11:49:56 -0500https://ask.sagemath.org/question/9989/