Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
funcs.cpp
Go to the documentation of this file.
1 /**
2  * @file funcs.cpp file containing miscellaneous
3  * numerical functions.
4  */
5 /*
6  * Copyright 2001-2003 California Institute of Technology
7  * See file License.txt for licensing information
8  */
9 
10 #include "cantera/numerics/funcs.h"
13 
14 using namespace std;
15 
16 #ifndef FTN_TRAILING_UNDERSCORE
17 #define _DPOLFT_ dpolft
18 #define _DPCOEF_ dpcoef
19 #else
20 #define _DPOLFT_ dpolft_
21 #define _DPCOEF_ dpcoef_
22 #endif
23 
24 extern "C" {
25  int _DPOLFT_(integer* n, doublereal* x, doublereal* y, doublereal* w,
26  integer* maxdeg, integer* ndeg, doublereal* eps, doublereal* r,
27  integer* ierr, doublereal* a);
28 
29  int _DPCOEF_(integer* l, doublereal* c, doublereal* tc, doublereal* a);
30 }
31 
32 namespace Cantera
33 {
34 
35 doublereal linearInterp(doublereal x, const vector_fp& xpts,
36  const vector_fp& fpts)
37 {
38  if (x <= xpts[0]) {
39  return fpts[0];
40  }
41  if (x >= xpts.back()) {
42  return fpts.back();
43  }
44  vector_fp::const_iterator loc =
45  lower_bound(xpts.begin(), xpts.end(), x);
46  int iloc = int(loc - xpts.begin()) - 1;
47  doublereal ff = fpts[iloc] +
48  (x - xpts[iloc])*(fpts[iloc + 1]
49  - fpts[iloc])/(xpts[iloc + 1] - xpts[iloc]);
50  return ff;
51 }
52 
53 //! Fits a polynomial function to a set of data points
54 /*!
55  * Given a collection of points X(I) and a set of values Y(I) which
56  * correspond to some function or measurement at each of the X(I),
57  * subroutine DPOLFT computes the weighted least-squares polynomial
58  * fits of all degrees up to some degree either specified by the user
59  * or determined by the routine. The fits thus obtained are in
60  * orthogonal polynomial form. Subroutine DP1VLU may then be
61  * called to evaluate the fitted polynomials and any of their
62  * derivatives at any point. The subroutine DPCOEF may be used to
63  * express the polynomial fits as powers of (X-C) for any specified
64  * point C.
65  *
66  * @param n The number of data points.
67  *
68  * @param x A set of grid points on which the data is specified.
69  * The array of values of the independent variable. These
70  * values may appear in any order and need not all be
71  * distinct. There are n of them.
72  *
73  * @param y array of corresponding function values. There are n of them
74  *
75  * @param w array of positive values to be used as weights. If
76  * W[0] is negative, DPOLFT will set all the weights
77  * to 1.0, which means unweighted least squares error
78  * will be minimized. To minimize relative error, the
79  * user should set the weights to: W(I) = 1.0/Y(I)**2,
80  * I = 1,...,N .
81  *
82  * @param maxdeg maximum degree to be allowed for polynomial fit.
83  * MAXDEG may be any non-negative integer less than N.
84  * Note -- MAXDEG cannot be equal to N-1 when a
85  * statistical test is to be used for degree selection,
86  * i.e., when input value of EPS is negative.
87  *
88  * @param ndeg output degree of the fit computed.
89  *
90  * @param eps Specifies the criterion to be used in determining
91  * the degree of fit to be computed.
92  * (1) If EPS is input negative, DPOLFT chooses the
93  * degree based on a statistical F test of
94  * significance. One of three possible
95  * significance levels will be used: .01, .05 or
96  * .10. If EPS=-1.0 , the routine will
97  * automatically select one of these levels based
98  * on the number of data points and the maximum
99  * degree to be considered. If EPS is input as
100  * -.01, -.05, or -.10, a significance level of
101  * .01, .05, or .10, respectively, will be used.
102  * (2) If EPS is set to 0., DPOLFT computes the
103  * polynomials of degrees 0 through MAXDEG .
104  * (3) If EPS is input positive, EPS is the RMS
105  * error tolerance which must be satisfied by the
106  * fitted polynomial. DPOLFT will increase the
107  * degree of fit until this criterion is met or
108  * until the maximum degree is reached.
109  *
110  * @param r Output vector containing the first LL+1 Taylor coefficients
111  * where LL=ABS(ndeg).
112  * P(X) = r[0] + r[1]*(X-C) + ... + r[ndeg] * (X-C)**ndeg
113  * ( here C = 0.0)
114  *
115  * @return returns the RMS error of the polynomial of degree ndeg .
116  */
117 doublereal polyfit(int n, doublereal* x, doublereal* y, doublereal* w, int maxdeg, int& ndeg, doublereal eps, doublereal* r)
118 {
119  integer nn = n;
120  integer mdeg = maxdeg;
121  integer ndg = ndeg;
122  doublereal epss = eps;
123  integer ierr;
124  int worksize = 3*n + 3*maxdeg + 3;
125  vector_fp awork(worksize,0.0);
126  vector_fp coeffs(n+1, 0.0);
127  doublereal zer = 0.0;
128 
129  _DPOLFT_(&nn, x, y, w, &mdeg, &ndg, &epss, &coeffs[0],
130  &ierr, &awork[0]);
131  if (ierr != 1) throw CanteraError("polyfit",
132  "DPOLFT returned error code IERR = " + int2str(ierr) +
133  "while attempting to fit " + int2str(n) + " data points "
134  + "to a polynomial of degree " + int2str(maxdeg));
135  ndeg = ndg;
136  _DPCOEF_(&ndg, &zer, r, &awork[0]);
137  return epss;
138 }
139 
140 }
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:39
doublereal polyfit(int n, doublereal *x, doublereal *y, doublereal *w, int maxdeg, int &ndeg, doublereal eps, doublereal *r)
Fits a polynomial function to a set of data points.
Definition: funcs.cpp:117
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:157
Contains declarations for string manipulation functions within Cantera.
Header for a file containing miscellaneous numerical functions.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
doublereal linearInterp(doublereal x, const vector_fp &xpts, const vector_fp &fpts)
Linearly interpolate a function defined on a discrete grid.
Definition: funcs.cpp:35