📄 ocn_e01.h
字号:
Example1:
Assume "Data1" Worksheet has 6 columns and 10 rows, the first 3 columns contain 5 data in each column
and we want to put result in the fourth to sixth column.
int i, m = 7, n = 5, r;
double step;
//Attach six Datasets to these 6 columns
Dataset dx("data1",0);
dx.SetSize(n);
Dataset df("data1",1);
df.SetSize(n);
Dataset dd("data1",2);
dd.SetSize(n);
Dataset dpf("data1",3);
dpf.SetSize(m);
Dataset dpx("data1",4);
dpx.SetSize(m);
Dataset dpd("data1",5);
dpd.SetSize(m);
//vector can be a parameter, but Dataset cannot.
vector x = dx, f = df, d = dd, pf = dpf, px = dpx, pd = dpd;
step = (x[n-1]-x[0]) / (double)(m-1);
for (i = 0; i < m; i++)
{
if(x[0]+ i*step > x[n-1])
px[i] =x[n-1];
else
px[i] = x[0]+ i*step;
}
nag_monotonic_deriv(n, x, f, d, m, px, pf, pd);
//Put the results to the Worksheet.
dpf = pf;
dpx = px;
dpd = pd;
Example2:
This example program reads in values of n, x, f and d and calls
nag monotonic deriv to compute the values of the interpolant and
its derivative at equally spaced points.
void test_nag_monotonic_deriv()
{
int i, m, n, r;
double pd[21], pf[21], px[21], step;
n = 9;
m = 11;
double x[50] = {7.99, 8.09, 8.19, 8.70, 9.20, 10.00, 12.00, 15.00, 20.00};
double f[50] = {0.00000E+0, 0.27643E-4, 0.43750E-1, 0.16918E+0, 0.46943E+0,
0.94374E+0, 0.99864E+0, 0.99992E+0, 0.99999E+0};
double d[50] = {0.00000E+0, 5.52510E-4, 0.33587E+0, 0.34944E+0, 0.59696E+0,
6.03260E-2, 8.98335E-4, 2.93954E-5, 0.00000E+0};
step = (x[n-1]-x[0]) / (double)(m-1);
for (i = 0; i < m; i++)
{
if(x[0]+ i*step > x[n-1])
px[i] =x[n-1];
else
px[i] = x[0]+ i*step;
}
nag_monotonic_deriv(n, x, f, d, m, px, pf, pd);
printf(" Interpolated Interpolated\n");
printf(" Abscissa Value Derivative\n");
for (i=0; i<m; i++)
printf("%15.4f %15.4f %15.3e\n",px[i],pf[i],pd[i]);
}
The output is as follows:
Interpolated Interpolated
Abscissa Value Derivative
7.9900 0.0000 0.000e+000
9.1910 0.4640 6.060e-001
10.3920 0.9645 4.569e-002
11.5930 0.9965 9.917e-003
12.7940 0.9992 6.249e-004
13.9950 0.9998 2.708e-004
15.1960 0.9999 2.809e-005
16.3970 1.0000 2.034e-005
17.5980 1.0000 1.308e-005
18.7990 1.0000 6.297e-006
20.0000 1.0000 -3.388e-021
Parameters:
Return:
This function returns NAG error code, 0 if no error.
11: On entry, n must not be less than 2: n = _value_. On entry, m must not be less than 1: m = _value_.
236: On entry, x[r - 1] = x[r] for r = _value_: x[r - 1 ] =_value_, x[r] = _value_. The values of x[r], for r = 0, 1, . . . , n - 1a re not in strictly increasing order.
successful call of the nag_monotonic_deriv function.
*/
int nag_monotonic_deriv(
int n,
double x[],
double f[],
double d[], // Input: n, x, f and d must be unchanged from the previous call of nag monotonic interpolant (e01bec).
int m, // the number of points at which the interpolant is to be evaluated.
double px[], // Input: the m values of x at which the interpolant is to be evaluated.
double pf[], // Output: pf[i] contains the value of the interpolant evaluated at the point px[i], for i = 0, 1, . . .,m - 1.
double pd[] // Output: pd[i] contains the .rst derivative of the interpolant evaluated at the point px[i], for i = 0, 1, . . .,m - 1.
);
/** e01bhc
evaluates the definite integral of a piecewise cubic Hermite interpolant over the interval [a, b].
Example1:
Assume "Data1" Worksheet has 3 columns and 9 rows, and contain 9 data in each column.
"Data2" Worksheet has 3 columns and 4 rows, the first two columns contain 4 data in
each column. This piece of code reads in values of "Data1", and then reads in pair
of "Data2" first two columns, and evaluates the definite integral of the intepolant
over the interval [a, b].
int n = 9, nn = 4;
//Attach six Datasets to these 6 columns
Dataset dx("data1",0);
dx.SetSize(n);
Dataset df("data1",1);
df.SetSize(n);
Dataset dd("data1",2);
dd.SetSize(n);
Dataset da("data2",0);
da.SetSize(nn);
Dataset db("data2",1);
db.SetSize(nn);
Dataset dintegral("data2",2);
dintegral.SetSize(nn);
//vector can be a parameter, but Dataset cannot.
vector x = dx, f = df, d = dd, a = da, b = db, integral = dintegral;
for(int i = 0; i < nn; i++)
{
nag_monotonic_intg(n, x, f, d, a[i], b[i], &integral[i]);
}
//put the result to the "data2" third column.
dintegral = integral;
Example2:
This example program reads in values of n, x, f and d. It then reads
in pairs of values for a and b, and evaluates the definite integral
of the interpolant over the interval [a, b] until end-of-file is reached.
void test_nag_monotonic_intg()
{
double integral;
int n, r;
n = 9;
double x[50] = {7.99, 8.09, 8.19, 8.70, 9.20, 10.00, 12.00, 15.00, 20.00};
double f[50] = {0.00000E+0, 0.27643E-4, 0.43750E-1, 0.16918E+0, 0.46943E+0,
0.94374E+0, 0.99864E+0, 0.99992E+0, 0.99999E+0};
double d[50] = {0.00000E+0, 5.52510E-4, 0.33587E+0, 0.34944E+0, 0.59696E+0,
6.03260E-2, 8.98335E-4, 2.93954E-5, 0.00000E+0};
double a[4] = {7.99, 10.0, 12.0, 15.0};
double b[4] = {20.0, 12.0, 10.0, 15.0};
printf(" Integral\n");
printf(" a b over (a,b)\n");
for(int i = 0; i < 4; i++)
{
nag_monotonic_intg(n, x, f, d, a[i], b[i], &integral);
printf("%13.4f %13.4f %13.4f\n",a[i],b[i],integral);
}
}
The output is as follows:
Integral
a b over (a,b)
7.9900 20.0000 10.7648
10.0000 12.0000 1.9622
12.0000 10.0000 -1.9622
15.0000 15.0000 0.0000
Parameters:
Return:
This function returns NAG error code, 0 if no error.
11: On entry, n must not be less than 2: n = _value_.
236: On entry, x[r - 1] = x[r] for r = _value_: x[r - 1] = _value_, x[r] = _value_. The values of x[r], for r = 0, 1, . . . , n - 1 are not in strictly increasing order.
237: On entry, limits a, b must not be outside interval [x[0], x[n - 1]], a = _value_, b = _value_, x[0] = _value_, x[_value_] = _value_. Extrapolation was performed to compute the integral. The value returned is therefore unreliable.
successful call of the nag_monotonic_intg function.
*/
int nag_monotonic_intg(
int n,
double x[],
double f[],
double d[], // Input: n, x, f and d must be unchanged from the previous call of nag monotonic interpolant (e01bec).
double a,
double b, // Input: the interval [a, b] over which integration is to be performed.
double *integral // Output: the value of the definite integral of the interpolant over the interval [a, b].
);
/** e01dac
computes a bicubic spline interpolating surface through a set
of data values, given on a rectangular grid in the x-y plane.
Example1:
Assume we have a Worksheet "data1" which has three columns and 20
rows. The first column shows the x axis value, and there are 5 data
in this column. The second column shows the y axis value, and there
are 4 data in this column. The third column contains 20 data. This
piece of code computes a bicubic spline interpolating surface through
a set of data values.
int mx = 5, my = 4;
Nag_2dSpline spline;
//Attach three Datasets to these 3 columns
Dataset dx("data1",0);
dx.SetSize(mx);
Dataset dy("data1",1);
dy.SetSize(my);
Dataset df("data1",2);
df.SetSize(20);
//vector can be a parameter, but Dataset cannot.
vector x = dx, y = dy, f = df;
nag_2d_spline_interpolant(mx, my, x, y, f, &spline);
Example2:
This program reads in values of mx, xq for q = 1, 2, . . .,mx, my
and yr for r = 1, 2, . . .,my, followed by values of the
ordinates fq,r defined at the grid points (xq, yr). It then calls
nag 2d spline interpolant to compute a bicubic spline interpolant
of the data values, and prints the values of the knots and
B-spline coefficients. Finally it evaluates the spline at a
small sample of points on a rectangular grid.
void test_nag_2d_spline_interpolant()
{
int i, j, mx, my, npx, npy;
double fg[400], tx[20], ty[20];
double xhi, yhi, xlo, ylo, step;
Nag_2dSpline spline;
mx = 7;
my = 6;
double x[20] = {1.0, 1.10, 1.30, 1.50, 1.60, 1.80, 2.00};
double y[20] = {0.00, 0.10, 0.40, 0.70, 0.90, 1.00};
double f[400] = {1.00, 1.10, 1.40, 1.70, 1.90, 2.00, 1.21, 1.31, 1.61, 1.91,
2.11, 2.21, 1.69, 1.79, 2.09, 2.39, 2.59, 2.69, 2.25, 2.35,
2.65, 2.95, 3.15, 3.25, 2.56, 2.66, 2.96, 3.26, 3.46, 3.56,
3.24, 3.34, 3.64, 3.94, 4.14, 4.24, 4.00, 4.10, 4.40, 4.70,
4.90, 5.00};
nag_2d_spline_interpolant(mx, my, x, y, f, &spline);
printf("Distinct knots in x direction located at\n");
for (j=3; j<spline.nx-3; j++)
{
printf("%12.4f",spline.lamda[j]);
if((j-3)%5==4 || j==spline.nx - 4)
printf("\n");
}
printf("\nDistinct knots in y direction located at\n");
for (j=3; j<spline.ny-3; j++)
{
printf("%12.4f",spline.mu[j]);
if((j-3)%5==4 || j==spline.ny-4)
printf("\n");
}
printf("\nThe B-Spline coefficients:\n");
for (i=0; i<mx; i++)
{
for (j=0; j<my; j++)
printf("%9.4f",spline.c[my*i+j]);
printf("\n");
}
npx = 6;
npy = 6;
xlo = 1.0;
ylo = 0.0;
xhi = 2.0;
yhi = 1.0;
step = (xhi-xlo)/(1.0*(npx-1));
printf("\nSpline evaluated on a regular mesh (x across, y down): \n ");
for (i=0; i<npx; i++)
{
if(xlo + i * step > xhi)
tx[i] = xhi;
else
tx[i] = xlo + i * step;
printf(" %5.2f ",tx[i]);
}
step = (yhi-ylo)/(npy-1);
for (i=0; i<npy; i++)
{
if(ylo + i * step > yhi)
ty[i] = yhi;
else
ty[i] = ylo + i * step;
}
nag_2d_spline_eval_rect(npx, npy, tx, ty, fg, &spline);
printf("\n");
for (j=0; j<npy; j++)
{
printf("%5.2f",ty[j]);
for (i=0; i<npx; i++)
printf("%8.3f ",fg[npy*i+j]);
printf("\n");
}
nag_free(spline.lamda);
nag_free(spline.mu);
nag_free(spline.c);
}
The output is as follows:
Distinct knots in x direction located at
1.0000 1.3000 1.5000 1.6000 2.0000
Distinct knots in y direction located at
0.0000 0.4000 0.7000 1.0000
The B-Spline coefficients:
1.0000 1.1333 1.3667 1.7000 1.9000 2.0000
1.2000 1.3333 1.5667 1.9000 2.1000 2.2000
1.5833 1.7167 1.9500 2.2833 2.4833 2.5833
2.1433 2.2767 2.5100 2.8433 3.0433 3.1433
2.8667 3.0000 3.2333 3.5667 3.7667 3.8667
3.4667 3.6000 3.8333 4.1667 4.3667 4.4667
4.0000 4.1333 4.3667 4.7000 4.9000 5.0000
Spline evaluated on a regular mesh (x across, y down):
1.00 1.20 1.40 1.60 1.80 2.00
0.00 1.000 1.440 1.960 2.560 3.240 4.000
0.20 1.200 1.640 2.160 2.760 3.440 4.200
0.40 1.400 1.840 2.360 2.960 3.640 4.400
0.60 1.600 2.040 2.560 3.160 3.840 4.600
0.80 1.800 2.240 2.760 3.360 4.040 4.800
1.00 2.000 2.440 2.960 3.560 4.240 5.000
Parameters:
Return:
This function returns NAG error code, 0 if no error.
11: On entry, mx must not be less than 4: mx = _value_. On entry, my must not be less than 4: my = _value_.
63: The sequence x is not strictly increasing: x[_value_] = _value_, x[_value_] = _value_. The sequence y is not strictly increasing: y[_value_] = _value_, y[_value_] = _value_.
73: Memory allocation failed.
243: An intermediate set of linear equations is singular, the data is too ill-conditioned to compute B-spline coefficients.
successful call of the nag_2d_spline_interpolant function.
*/
int nag_2d_spline_interpolant(
int mx,
int my, // Input: mx and my must specify mx and my respectively, the number of points along the x and y axis that define the rectangular grid.
double x[],
double y[], // Input: x[q-1] and y[r-1] must contain xq, for q = 1, 2, . . .,mx, and yr, for r = 1, 2, . . .,my, respectively.
double f[], // Input: f[my
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -