📄 ocn_e02.h
字号:
for (r = 0; r < n+1; ++r)
{
success =nag_1d_cheb_eval(n+1, an, xcap[r], &fit);
printf(" %3ld%11.4f%11.4f%11.4f\n",r+1,xcap[r],f[r],fit);
}
}
The output is following:
Chebyshev
J coefficient A(J)
1 2.5320000
2 1.1303095
3 0.2714893
4 0.0443462
5 0.0055004
6 0.0005400
7 0.0000307
8 -0.0000006
9 -0.0000004
10 0.0000049
11 -0.0000200
R Abscissa Ordinate Fit
1 1.0000 2.7182 2.7182
2 0.9511 2.5884 2.5884
3 0.8090 2.2456 2.2456
4 0.5878 1.7999 1.7999
5 0.3090 1.3620 1.3620
6 0.0000 1.0000 1.0000
7 -0.3090 0.7341 0.7341
8 -0.5878 0.5555 0.5555
9 -0.8090 0.4452 0.4452
10 -0.9511 0.3863 0.3863
11 -1.0000 0.3678 0.3678
Return:
This function returns NAG error code, 0 if no error
11: On entry, nplus1 must not be less than 2: nplus1 = _value_.
successfully call of the nag_1d_cheb_interp_fit function.
*/
int nag_1d_cheb_interp_fit(
int nplus1, //the number n + 1 of data points (one greater than the degree n of the interpolating polynomial.
const double f[],
double a[] //the coefficient a[j] in the interpolating polynomial,for j = 1,2,.., n + 1.
); //Least-squares curve fit with polynomials,selected data points
/** e02bac
computes a weighted least-squares approximation to an arbitrary set of data points by a
cubic spline with knots prescribed by the user. Cubic spline interpolation can also be carried out.
Return NAG error, 0 if no error.
Example1:
Assume "Data1" Worksheet has three columns, every column has 14 data. After call the function,
we get the residual sum of squares and the result of a Nag_Spline structure variable.
//Attach three Datasets to these 3 columns
int m=14;
Dataset xx("data1",0);
Dataset yy("data1",1);
Dataset dweights("data1",2);
xx.SetSize(m);
yy.SetSize(m);
dweights.SetSize(m);
double ss;
Nag_Spline spline;
vector x, y, weights;
x = xx;
y = yy;
weights = dweights;
BOOL success = nag_1d_spline_fit_knots(m, x, y, weights, &ss, &spline);
Example2:
Determine a weighted least-squares cubic spline approximation with five intervals (four interior knots)
to a set of 14 given data points. Tabulate the data and the corresponding values of the approximating
spline, together with the residual errors, and also the values of the approximating spline at points
half-way between each pair of adjacent data points.
void test_nag_1d_spline_fit_knots()
{
int ncap, ncap7, j, m, r, wght;
double weights[200] = {0.20, 0.20, 0.30, 0.70, 0.90, 1.00, 1.00, 1.00, 0.80, 0.50, 0.70, 1.00, 1.00, 1.00};;
double xarg, ss, fit;
double fg[12];
double x[200] = {0.20, 0.47, 0.74, 1.09, 1.60, 1.90, 2.60, 3.10, 4.00, 5.15, 6.17, 8.00, 10.00, 12.00};
double y[200] = {0.00, 2.00, 4.00, 6.00, 8.00, 8.62, 9.10, 8.90, 8.15, 7.00, 6.00, 4.54, 3.39, 2.56};
Nag_Spline spline;
int success;
m = 14;
ncap = 5;
wght = 2;
ncap7 = ncap+7;
spline.n = ncap7;
fg[4] = 1.50;
fg[5] = 2.60;
fg[6] = 4.00;
fg[7] = 8.00;
spline.lamda = fg;
printf("spline = %d\n", spline.n);
ASSERT(spline.n == 12);
int nSize=sizeof(Nag_Spline);
success = nag_1d_spline_fit_knots(m, x, y, weights, &ss, &spline);
printf("\nNumber of distinct knots = %ld\n\n", ncap+1);
printf("Distinct knots located at \n\n");
for (j=3; j<ncap+4; j++)
{
printf("%8.4f",spline.lamda[j]);
if((j-3)%6==5 || j==ncap+3)
printf("\n");
}
printf("\n\n J B-spline coeff c\n\n");
for (j=0; j<ncap+3; ++j)
printf(" %ld %13.4f\n",j+1,spline.c[j]);
printf("\nResidual sum of squares = ");
printf("%9.2e\n\n",ss);
printf("Cubic spline approximation and residuals\n");
printf(" r Abscissa Weight Ordinate Spline Residual\n\n");
for (r=0; r<m; ++r)
{
nag_1d_spline_evaluate(x[r], &fit, &spline);
printf("%3ld %11.4f %11.4f %11.4f %11.4f %10.1e\n",r+1,x[r],weights[r],y[r],fit,fit-y[r]);
if (r<m-1)
{
xarg = (x[r] + x[r+1]) * 0.5;
nag_1d_spline_evaluate(xarg, &fit, &spline);
printf(" %14.4f %35.4f\n",xarg,fit);
}
}
nag_free(spline.c);
}
The output is following:
spline = 12
Number of distinct knots = 6
Distinct knots located at
0.2000 1.5000 2.6000 4.0000 8.0000 12.0000
J B-spline coeff c
1 -0.0465
2 3.6150
3 8.5724
4 9.4261
5 7.2716
6 4.1207
7 3.0822
8 2.5597
Residual sum of squares = 1.78e-03
Cubic spline approximation and residuals
r Abscissa Weight Ordinate Spline Residual
1 0.2000 0.2000 0.0000 -0.0465 -4.7e-02
0.3350 1.0622
2 0.4700 0.2000 2.0000 2.1057 1.1e-01
0.6050 3.0817
3 0.7400 0.3000 4.0000 3.9880 -1.2e-02
0.9150 5.0558
4 1.0900 0.7000 6.0000 5.9983 -1.7e-03
1.3450 7.1376
5 1.6000 0.9000 8.0000 7.9872 -1.3e-02
1.7500 8.3544
6 1.9000 1.0000 8.6200 8.6348 1.5e-02
2.2500 9.0076
7 2.6000 1.0000 9.1000 9.0896 -1.0e-02
2.8500 9.0353
8 3.1000 1.0000 8.9000 8.9125 1.2e-02
3.5500 8.5660
9 4.0000 0.8000 8.1500 8.1321 -1.8e-02
4.5750 7.5592
10 5.1500 0.5000 7.0000 6.9925 -7.5e-03
5.6600 6.5010
11 6.1700 0.7000 6.0000 6.0255 2.6e-02
7.0850 5.2292
12 8.0000 1.0000 4.5400 4.5315 -8.5e-03
9.0000 3.9045
13 10.0000 1.0000 3.3900 3.3928 2.8e-03
11.0000 2.9574
14 12.0000 1.0000 2.5600 2.5597 -3.5e-04
Return:
This function returns NAG error code, 0 if no error.
11: On entry, spline.n must not be less than 8: spline.n = _value_.
73: Memory allocation failed.
239: On entry, user-specified knots must be interior to the data interval, spline.lamda[4] must be greater than x[0] and spline.lamda[spline.n-5] must be less than x[m-1]: spline.lamda[4] = _value_, x[0] = _value_, spline.lamda[_value_] = _value_, x[_value_] = _value_.
62: The sequence spline.lamda is not increasing: spline.lamda[_value_] = _value_, spline.lamda[_value_] = _value_. This condition on spline.lamda applies to user-speci.ed knots in the interval spline.lamda[4], spline.lamda[spline.n-5]. The sequence x is not increasing: x[_value_] = _value_, x[_value_] = _value_.
240: On entry, the weights are not strictly positive: weights[_value_] = _value_.
244: Too many knots for the number of distinct abscissae, mdist: spline.n = _value_, mdist = _value_. These must satisfy the constraint spline.n = mdist + 4.
241: The conditions speci.ed by Schoenberg and Whitney fail. The conditions speci.ed by Schoenberg andWhitney (1953) fail to hold for at least one subset of the distinct data abscissae. That is, there is no subset of spline.n-4 strictly increasing values, x[r0], x[r1],. . . ,x[rspline.n-5], among the abscissae such that x[r0] < spline.lamda[0] < x[r4], x[r1] < spline.lamda[1] < x[r5], . . . x[rspline.n-9] < spline.lamda[spline.n-9] < x[rspline.n-5]. This means that there is no unique solution: there are regions containing too many knots compared with the number of data points.
successfully call of the nag_1d_spline_fit_knots function.
*/
int nag_1d_spline_fit_knots(
int m, //the number m of data points.
const double x[], //the values xr of the independent variable (abscissa), for r = 1, 2,...,m.
const double y[], //the values yr of the of the dependent variable (ordinate), for r = 1, 2,...,m.
const double weights[], //the values wr of the weights, for r = 1, 2, . .,m.
double *ss, //the residual sum of squares
Nag_Spline *spline //pointer to structure of type Nag_Spline.
); //Least-squares curve fit with cubic splines e02bac
/** e02bbc
evaluates a cubic spline from its B-spline representation.
Example:
Evaluate at 9 equally-spaced points in the interval [1.0, 9.0] the cubic spline with
(augmented) knots 1.0, 1.0, 1.0, 1.0, 3.0, 6.0, 8.0, 9.0, 9.0, 9.0, 9.0 and normalised
cubic B-spline coefficients 1.0, 2.0, 4.0, 7.0, 6.0, 4.0, 3.0.
void test_nag_1d_spline_evaluate()
{
double a, b,ncap7;
int m =9;
int ncap = 4;
int r;
double lamda[11] = {1.00, 1.00, 1.00, 1.00, 3.00, 6.00, 8.00, 9.00, 9.00, 9.00, 9.00};
double c[11] = {1.00, 2.00, 4.00, 7.00, 6.00, 4.00, 3.00};
double s;
double x;
int i, j;
Nag_Spline spline;
ncap7 = ncap +7;
spline.n = ncap7;
spline.lamda = lamda;
spline.c = c;
a = spline.lamda[3];
printf("a = %f\n",a);
b = spline.lamda[ncap+3];
printf("b = %f\n",b);
printf("Augmented set of knots stored in spline.lamda:\n");
for(j = 0; j < ncap7-1; j++)
printf("%10.4f",lamda[j]);
printf("\nB-spline coefficients stored in spline.c\n\n");
for(j = 0; j< ncap+3; j++)
printf("%10.4f",c[j]);
printf("\n x Value of cubic spline\n\n");
for (r=1; r<=m; ++r)
{
x = 1.0*((m-r) * a + (r-1) * b) /(m-1);
s= 0;
nag_1d_spline_evaluate(x, &s, &spline);
printf("%10.4f%15.4f\n",x,s);
}
}
The output is following:
Augmented set of knots stored in spline.lamda:
1.0000 1.0000 1.0000 1.0000 3.0000 6.0000
8.0000 9.0000 9.0000 9.0000 9.0000
B-spline coefficients stored in spline.c:
1.0000 2.0000 4.0000 7.0000 6.0000 4.0000
3.0000
x Value of cubic spline
1.0000 1.0000
2.0000 2.3779
3.0000 3.6229
4.0000 4.8327
5.0000 5.8273
6.0000 6.3571
7.0000 6.1905
8.0000 5.1667
9.0000 3.0000
Return:
This function returns NAG error, 0 if no error.
11: On entry, spline.n must not be less than 8: spline.n = _value_.
238: On entry, x must satisfy spline.lamda[3] = x = spline.lamda[spline.n-4]: spline.lamda[3] = _value_, x = _value_, spline.lamda[_value_] = _value_. In this case s is set arbitrarily to zero.
successfully call of the nag_1d_spline_evaluate function.
*/
int nag_1d_spline_evaluate(
double x, //the argument x at which the cubic spline is to be evaluated.
double *s, //the value of the spline, s(x).
Nag_Spline *spline //Pointer to structure of type Nag_Spline.
); //Evaluation of cubic splines.
/** e02bcc
evaluates a cubic spline and its first three derivatives from its B-spline
representation.
Example:
Compute, at the 7 arguments x = 0, 1, 2 , 3, 4, 5, 6, the left- and right-hand
values and first 3 derivatives of the cubic spline defined over the interval
0 <= x <= 6 having the 6 interior knots x = 1, 3, 3, 3, 4, 4, the 8 additional
knots 0, 0, 0, 0, 6, 6, 6, 6, and the 10 B-spline coe.cients 10, 12, 13, 15,
22, 26, 24, 18, 14, 12. The input data items (using the notation of Section 4)
comprise the following values in the order indicated:
痭 m
spline.lamda[j], for j = 0, 1, . . . , 痭 + 6
spline.c[j], for j = 0, 1, . . . , 痭 + 2
x m values of x
The example program is written in a general form that will enable the values
and derivatives of a cubic spline having an arbitrary number of knots to be
evaluated at a set of arbitrary points. Any number of data sets may be supplied.
void test_nag_1d_spline_deriv()
{
int i, j, l, m, ncap, ncap7;
double s[4];
double lamd[14] = {0.0, 0.0, 0.0, 0.0, 1.0, 3.0, 3.0, 3.0, 4.0, 4.0, 6.0, 6.0, 6.0, 6.0};
double c1[14] = {10.0, 12.0, 13.0, 15.0, 22.0, 26.0, 24.0, 18.0, 14.0, 12.0};
double x[7] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
Nag_Spline spline;
Nag_DerivType derivs;
printf("e02bcc Example Program Results\n");
ncap = 7;
m = 7;
ncap7 = ncap+7;
spline.n = ncap7;
spline.lamda = lamd;
spline.c = c1;
for( i = 0; i < 11; i++)
printf("%f ",spline.lamda[i]);
printf(" x Spline 1st deriv 2nd deriv 3rd deriv");
for (i= 0; i< m; i++)
{
derivs = Nag_LeftDerivs;
for (j=1; j<=2; j++)
{
nag_1d_spline_deriv(derivs, x[i], s, &spline);
if(derivs ==Nag_LeftDerivs)
{
printf("\n\n%11.4f Left",x[i]);
for (l=0; l<4; l++)
printf("%11.4f",s[l]);
}
else
{
printf("\n%11.4f Right",x[i]);
for (l=0; l<4; l++)
printf("%11.4f",s[l]);
}
derivs = Nag_RightDerivs;
}
}
}
The output is following:
x Spline 1st deriv 2nd deriv 3rd deriv
0.0000 Left 10.0000 6.0000 -10.0000 10.6667
0.0000 Right 10.0000 6.0000 -10.0000 10.6667
1.0000 Left 12.7778 1.3333 0.6667 10.6667
1.0000 Right 12.7778 1.3333 0.6667 3.9167
2.0000 Left 15.0972 3.9583 4.5833 3.9167
2.0000 Right 15.0972 3.9583 4.5833 3.9167
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -