# Fitting for piecewise function, with constraints on first/second derivative

I have the following problem.

We have a set of discrete points ($x_i$,$y_i$), defined for $0 \leq x_i \leq r$, where $r$ is an arbitrary value. For values of $0 \leq x \leq r$, the y value is obtained from polynomial interpolation over 6 points, three with $x_i < x$ and three with $x_i > x$.

For $x > r$, y must go to zero. The current technique used is to extrapolate with a polynomial matched on the last 6 points of the set, plus 3 points forced to zero. This leads to weird wiggling due to Runge's phenomenon.

One idea I have is to perform least squares on a negative exponential, which is guaranteed to go to numerical zero. However, the problem occurs at the point where we switch from interpolation to extrapolation, because we want to guarantee the first derivative to be continuous in $r$. Splines (at least the ones I know) don't satisfy these constraints, and a simple extrapolation to negative exponential will produce a non-continuous first derivative.

Do you have any hints on how to tackle this problem ?

Edit: this is an example of the data (I plot two curves, but they are the same, I just had this plot around).

I need to go to zero by extending the curve beyond point 16 so that it goes to zero only at +infinity (like an negative exponential), with the constraint that in point 16 it must be smooth. I removed the need for the second derivative to have the same sign. This is clearly not possible here.

Note that the points can be considered either exact or not. The problem is that for $x < r$ we assume the points are exact and when $x = x_i$ you should get $y_i$. However, when $x > r$, the points are not considered exact anymore for the extrapolation, thus the need for a least square fitting.

-
Are the $y_i$ values accurate, or error-contaminated? Where do these points come from? Do you have a plot of a representative example of these points? – J. M. Oct 25 '11 at 16:31
@J.M.: I refine the question – Stefano Borini Oct 25 '11 at 17:07
So you want your approximating function to pass through the given points? Those given points are accurate? – J. M. Oct 25 '11 at 17:13
@J.M.: yes, they are considered accurate, and the function must pass through them. – Stefano Borini Oct 25 '11 at 17:58
If the function must pass through the points, then the title "Least square fitting" is not right – leonbloy Oct 25 '11 at 18:35

You need to decide how fast you want your function to go to zero, and how sharply it is allowed to curve to get there. For instance, one candidate function has the shape: $$y = \frac{1}{1+x^2}$$ Or in general: $$y = \frac{a}{b+(x-c)^2}$$ This has three degrees of freedom, but there are only two constraints, $y(r)$ and $y'(r)$. So there is a whole family of such curves. Basically, the faster you want $y$ to tend to zero, the more sharply it has to bend.
Also, are you concerned about the asymptotic behaviour? If you want it to approach $0$ exponentially, then you could try something of the form $$y = e^{-x^2}$$ Or in general: $$y = ae^{-b(x-c)^2}$$ Again there is an extra degree of freedom, and you have to decide what you want from your model.
+1 This sounds ok to me. The problem, as stated, is almost trivial, the "constraints" for points $x_i$ are practically irrelevant, the only thing that matters (is seems) is continuity and smoothness at $x=r$. The final phrase ("when $x>r$, the points are not considered exact anymore for the extrapolation, thus the need for a least square fitting.") is still confusing, if not plainly wrong. – leonbloy Nov 22 '11 at 15:30