ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 15 Oct 2020 09:54:00 +0200how 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?Wed, 14 Oct 2020 14:05:17 +0200https://ask.sagemath.org/question/53909/how-to-join-functions-with-an-intermediate-fit-to-obtain-smooth-derivatives/Answer by eric_g for <p>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.</p>
<p>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?</p>
<p>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?</p>
https://ask.sagemath.org/question/53909/how-to-join-functions-with-an-intermediate-fit-to-obtain-smooth-derivatives/?answer=53927#post-id-53927Probably, you are looking for [Hermite interpolation](https://en.wikipedia.org/wiki/Hermite_interpolation).
For interpolation up to the first order derivative, you can use SciPy's [CubicHermiteSpline](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicHermiteSpline.html#scipy.interpolate.CubicHermiteSpline), which is available in SageMath:
sage: from scipy.interpolate import CubicHermiteSpline
sage: CubicHermiteSpline?
Thu, 15 Oct 2020 09:54:00 +0200https://ask.sagemath.org/question/53909/how-to-join-functions-with-an-intermediate-fit-to-obtain-smooth-derivatives/?answer=53927#post-id-53927