PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_fit_1dcheb_con (e02ag)
Purpose
nag_fit_1dcheb_con (e02ag) computes constrained weighted least squares polynomial approximations in Chebyshev series form to an arbitrary set of data points. The values of the approximations and any number of their derivatives can be specified at selected points.
Syntax
[
a,
s,
np1,
wrk,
ifail] = e02ag(
k,
xmin,
xmax,
x,
y,
w,
xf,
yf,
ip,
lwrk, 'm',
m, 'mf',
mf)
[
a,
s,
np1,
wrk,
ifail] = nag_fit_1dcheb_con(
k,
xmin,
xmax,
x,
y,
w,
xf,
yf,
ip,
lwrk, 'm',
m, 'mf',
mf)
Description
nag_fit_1dcheb_con (e02ag) determines least squares polynomial approximations of degrees up to to the set of data points with weights , for . The value of , the maximum degree required, is to be prescribed by you. At each of the values , for , of the independent variable , the approximations and their derivatives up to order are constrained to have one of the values , for , specified by you, where .
The approximation of degree
has the property that, subject to the imposed constraints, it minimizes
, the sum of the squares of the weighted residuals
, for
, where
and
is the value of the polynomial approximation of degree
at the
th data point.
Each polynomial is represented in Chebyshev series form with normalized argument
. This argument lies in the range
to
and is related to the original variable
by the linear transformation
where
and
, specified by you, are respectively the lower and upper end points of the interval of
over which the polynomials are to be defined.
The polynomial approximation of degree
can be written as
where
is the Chebyshev polynomial of the first kind of degree
with argument
. For
, the function produces the values of the coefficients
, for
, together with the value of the root mean square residual,
where
is the number of data points with nonzero weight.
Values of the approximations may subsequently be computed using
nag_fit_1dcheb_eval (e02ae) or
nag_fit_1dcheb_eval2 (e02ak).
First
nag_fit_1dcheb_con (e02ag) determines a polynomial
, of degree
, which satisfies the given constraints, and a polynomial
, of degree
, which has value (or derivative) zero wherever a constrained value (or derivative) is specified. It then fits
, for
, with polynomials of the required degree in
each with factor
. Finally the coefficients of
are added to the coefficients of these fits to give the coefficients of the constrained polynomial approximations to the data points
, for
. The method employed is given in
Hayes (1970): it is an extension of Forsythe's orthogonal polynomials method (see
Forsythe (1957)) as modified by Clenshaw (see
Clenshaw (1960)).
References
Clenshaw C W (1960) Curve fitting with a digital computer Comput. J. 2 170–173
Forsythe G E (1957) Generation and use of orthogonal polynomials for data fitting with a digital computer J. Soc. Indust. Appl. Math. 5 74–88
Hayes J G (ed.) (1970) Numerical Approximation to Functions and Data Athlone Press, London
Parameters
Compulsory Input Parameters
- 1:
– int64int32nag_int scalar
-
, the maximum degree required.
Constraint:
where is the total number of constraints and is the number of data points with nonzero weights and distinct abscissae which do not coincide with any of the .
- 2:
– double scalar
- 3:
– double scalar
-
The lower and upper end points, respectively, of the interval
. Unless there are specific reasons to the contrary, it is recommended that
xmin and
xmax be set respectively to the lowest and highest value among the
and
. This avoids the danger of extrapolation provided there is a constraint point or data point with nonzero weight at each end point.
Constraint:
.
- 4:
– double array
-
must contain the value of the independent variable at the th data point, for .
Constraint:
the must be in nondecreasing order and satisfy .
- 5:
– double array
-
must contain , the value of the dependent variable at the th data point, for .
- 6:
– double array
-
must contain the weight
to be applied to the data point
, for
. For advice on the choice of weights see the
E02 Chapter Introduction. Negative weights are treated as positive. A zero weight causes the corresponding data point to be ignored. Zero weight should be given to any data point whose
and
values both coincide with those of a constraint (otherwise the denominators involved in the root mean square residuals
will be slightly in error).
- 7:
– double array
-
must contain , the value of the independent variable at which a constraint is specified, for .
Constraint:
these values need not be ordered but must be distinct and satisfy .
- 8:
– double array
-
lyf, the dimension of the array, must satisfy the constraint
.
The values which the approximating polynomials and their derivatives are required to take at the points specified in
xf. For each value of
,
yf contains in successive elements the required value of the approximation, its first derivative, second derivative,
th derivative, for
. Thus the value,
, which the
th derivative of each approximation (
referring to the approximation itself) is required to take at the point
must be contained in
, where
where
and
. The derivatives are with respect to the independent variable
.
- 9:
– int64int32nag_int array
-
must contain , the order of the highest-order derivative specified at , for . implies that the value of the approximation at is specified, but not that of any derivative.
Constraint:
, for .
- 10:
– int64int32nag_int scalar
-
The dimension of the array
wrk.
Constraint:
, where , for .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
x,
y,
w. (An error is raised if these dimensions are not equal.)
, the number of data points to be fitted.
Constraint:
.
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
xf,
ip. (An error is raised if these dimensions are not equal.)
, the number of values of the independent variable at which a constraint is specified.
Constraint:
.
Output Parameters
- 1:
– double array
-
.
contains the coefficient in the approximating polynomial of degree , for and .
- 2:
– double array
-
.
contains
, for
, the root mean square residual corresponding to the approximating polynomial of degree
. In the case where the number of data points with nonzero weight is equal to
,
is indeterminate: the function sets it to zero. For the interpretation of the values of
and their use in selecting an appropriate degree, see
General in the E02 Chapter Introduction.
- 3:
– int64int32nag_int scalar
-
, where is the total number of constraint conditions imposed: .
- 4:
– double array
-
Contains weighted residuals of the highest degree of fit determined . The residual at
is in element , for . The rest of the array is used as workspace.
- 5:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | , |
or | , |
or | , |
or | , |
or | lwrk is too small (see Arguments), |
or | . |
(Here is the total number of constraint conditions.)
-
-
for some .
-
-
, or
is not in the interval
xmin to
xmax for some
, or the
are not distinct.
-
-
is not in the interval
xmin to
xmax for some
.
-
-
for some .
-
-
, where is the number of data points with nonzero weight and distinct abscissae which do not coincide with any . Thus there is no unique solution.
-
-
The polynomials and/or cannot be determined. The problem supplied is too ill-conditioned. This may occur when the constraint points are very close together, or large in number, or when an attempt is made to constrain high-order derivatives.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
No complete error analysis exists for either the interpolating algorithm or the approximating algorithm. However, considerable experience with the approximating algorithm shows that it is generally extremely satisfactory. Also the moderate number of constraints, of low-order, which are typical of data fitting applications, are unlikely to cause difficulty with the interpolating function.
Further Comments
The time taken to form the interpolating polynomial is approximately proportional to , and that to form the approximating polynomials is very approximately proportional to .
To carry out a least squares polynomial fit without constraints, use
nag_fit_1dcheb_arb (e02ad). To carry out polynomial interpolation only, use
nag_interp_1d_cheb (e01ae).
Example
This example reads data in the following order, using the notation of the argument list above:
- mf
- ,
,
Y-value and derivative values (if any) at , for
- m
- ,
,
, for
- , xmin, xmax
The output is:
- the root mean square residual for each degree from to ;
- the Chebyshev coefficients for the fit of degree ;
- the data points, and the fitted values and residuals for the fit of degree .
The program is written in a generalized form which will read any number of datasets.
The dataset supplied specifies
data points in the interval
with unit weights, to which are to be fitted polynomials,
, of degrees up to
, subject to the
constraints:
Open in the MATLAB editor:
e02ag_example
function e02ag_example
fprintf('e02ag example results\n\n');
n = 5;
x = [0.5; 1; 2; 2.5; 3];
y = [0.03; -0.75; -1; -0.1; 1.75];
w = [1; 1; 1; 1; 1];
ip = int64([1;0]);
xf = [0; 4];
yf = [1; -2; 9];
k = int64(4);
xmin = 0;
xmax = 4;
lwrk = int64(200);
[a, s, np1, wrk, ifail] = e02ag(...
k, xmin, xmax, x, y, w, xf, yf, ip, lwrk);
fprintf('Degree residual\n');
lyf = 3;
for i=lyf:k
fprintf('%5d%15.2e\n',i,s(i+1));
end
p = a(k+1,:);
fprintf('\n i x(i) y(i) Fit Residual\n');
for i = 1:n
[fiti, ifail] = e02ak(k, xmin, xmax, p, int64(1), x(i));
fprintf('%6d%11.4f%11.4f%11.4f%11.2e\n',...
i, x(i), y(i), fiti, fiti-y(i));
end
e02ag example results
Degree residual
3 2.55e-03
4 2.94e-03
i x(i) y(i) Fit Residual
1 0.5000 0.0300 0.0310 1.02e-03
2 1.0000 -0.7500 -0.7508 -7.81e-04
3 2.0000 -1.0000 -1.0020 -2.00e-03
4 2.5000 -0.1000 -0.0961 3.95e-03
5 3.0000 1.7500 1.7478 -2.17e-03
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015