Miscellaneous functions for handling interpolation.
More...
Go to the source code of this file.
|
void | MonotoneSplineCoeffs (double *x, double *y, double *dydx, int n, double *a, double *b, double *c, double *d) |
|
void | SplineCoeffs (double *x, double *f, double dfL, double dfR, int n, double *a, double *b, double *c, double *d) |
|
Miscellaneous functions for handling interpolation.
- Author
- A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
- Date
- Jan 2, 2014
Definition in file math_interp.c.
void MonotoneSplineCoeffs |
( |
double * |
x, |
|
|
double * |
y, |
|
|
double * |
dydx, |
|
|
int |
n, |
|
|
double * |
a, |
|
|
double * |
b, |
|
|
double * |
c, |
|
|
double * |
d |
|
) |
| |
Compute the cubic spline coefficients for interpolating a monotonic dataset known at node values f[k] = f(x[k]) and derivative dfdx(x[k]). The resulting spline will be the smoothest curve that passes through the control points while preserving monotonocity of the dataset but not necessarily continuity of the second derivative. Coefficients will be computed in each interval x[k] < x < x[k+1].
The x[k] may not be equally spaced.
- Parameters
-
[in] | x | 1D array of abscissas |
[in] | f | 1D array of function values |
[in] | dfdx | 1D array of function derivative |
[in] | n | the number of function values. |
[out] | a | the x^3 cubic coefficient |
[out] | b | the x^2 cubic coefficient |
[out] | c | the x cubic coefficient |
[out] | d | the (1) cubic coefficient |
Reference:
- "Monotonic cubic spline interpolation", G. Wolberg, I. Alfy, Proceedings of Computer Graphics International (1999)
- "An energy-minimization framework for monotonic cubic
spline interpolation", Wolberg & Alfy, J. of comput. and applied math. (2002) 143, 145
Definition at line 13 of file math_interp.c.
46 char cm, c1, c2, c2a, c2b, c2c;
47 double m,
alpha, beta, dx;
49 for (k = 0; k <
n-1; k++){
50 m = (y[k+1] - y[
k])/(x[k+1] - x[k]);
59 c1 = (alpha + beta - 2.0) <= 0.0;
60 c2 = (alpha + 2.0*beta - 2.0) > 0.0;
61 c2a = (2.0*alpha + beta - 3.0) <= 0.0;
62 c2b = (alpha + 2.0*beta - 3.0) <= 0.0;
63 c2c = (alpha*alpha + alpha*(beta-6.0) + (beta-3.0)*(beta-3.0)) < 0.0;
64 if ( (cm && c1) || (c2 && cm && (c2a || c2b || c2c))){
66 a[
k] = dx*(-2.0*m + dydx[
k] + dydx[k+1]);
67 b[
k] = dx*( 3.0*m - 2.0*dydx[
k] - dydx[k+1]);
71 print1 (
"! MonotoneSplineCoeffs(): monotonicity condition not ");
72 print1 (
"satisifed in SPLINE1 \n");
73 print1 (
"cm = %d, c1 = %d, c2 = %d, c2a = %d\n",cm,c1,c2,c2a);
84 alpha = 6.0*
a[
k]*0.5 + 2.0*b[
k];
85 beta = d[
k] + 0.5*(
c[
k] + 0.5*(b[
k] + 0.5*
a[
k]));
void print1(const char *fmt,...)
#define QUIT_PLUTO(e_code)
void SplineCoeffs |
( |
double * |
x, |
|
|
double * |
f, |
|
|
double |
dfL, |
|
|
double |
dfR, |
|
|
int |
n, |
|
|
double * |
a, |
|
|
double * |
b, |
|
|
double * |
c, |
|
|
double * |
d |
|
) |
| |
Compute the cubic spline coefficients for interpolating a dataset (not necessarily monotonic) known at node values f[k] = f(x[k]). The resulting spline will be continuous up to the second derivative. Coefficients will be computed in each interval x[k] < x < x[k+1].
The x[k] may not be equally spaced.
- Parameters
-
[in] | x | 1D array of abscissas |
[in] | f | 1D array of function values |
[in] | dfL | derivative at the leftmost node |
[in] | dfR | derivative at the rightmost node |
[in] | n | the number of function values. |
[out] | a | the x^3 cubic coefficient |
[out] | b | the x^2 cubic coefficient |
[out] | c | the x cubic coefficient |
[out] | d | the (1) cubic coefficient |
Reference:
- "Numerical Recipe in C, second edition", Section 3.3.
Definition at line 95 of file math_interp.c.
122 double p, sig, qn, un, dx;
124 double xm, xp, det, aa,bb,cc;
132 u[0] = (3.0/(x[1] - x[0])*(f[1] - f[0])/(x[1]-x[0]) - dfL);
140 for (i = 1; i <=
n-2;i++) {
141 sig = (x[
i] - x[i-1])/(x[i+1] - x[i-1]);
142 p = sig*d2f[i-1] + 2.0;
143 d2f[
i] = (sig - 1.0)/p;
144 u[
i] = (f[i+1] - f[
i])/(x[i+1]-x[i]) - (f[
i] - f[i-1])/(x[i]-x[i-1]);
145 u[
i] = (6.0*u[
i]/(x[i+1]-x[i-1]) - sig*u[i-1])/p;
148 un = (3.0/(x[
n-1]-x[
n-2]))*(dfR - (f[
n-1] - f[
n-2])/(x[
n-1]-x[
n-2]));
150 d2f[
n-1] = (un - qn*u[
n-2])/(qn*d2f[
n-2] + 1.0);
151 for (i =
n-2; i >= 0; i--) d2f[i] = d2f[i]*d2f[i+1] + u[i];
157 for (i = 0; i <
n-1; i++){
159 a[
i] = dx*dx/6.0*(d2f[i+1] - d2f[
i]);
160 b[
i] = dx*dx*0.5*d2f[
i];
161 c[
i] = f[i+1] - f[
i] - dx*dx/6.0*(2.0*d2f[
i] + d2f[i+1]);
169 det = bb*bb - 4.0*aa*cc;
171 xm = (-bb + sqrt(det))/(2.0*aa);
172 xp = (-bb - sqrt(det))/(2.0*aa);
174 if ((xm > 0.0 && xm < 1.0) || (xp > 0.0 && xp < 1.0)){
175 printf (
"! SplineCoeffs(): cubic is not monotonic, in [%8.3e, %8.3e]\n",
177 printf (
"! xm = %8.3e, xp = %8.3e\n",xm,xp);
189 xm = 6.0*
a[
i]*0.5 + 2.0*b[
i];
190 xp = d[
i] + 0.5*(
c[
i] + 0.5*(b[
i] + 0.5*
a[
i]));
#define ARRAY_1D(nx, type)
#define QUIT_PLUTO(e_code)