1 /**********************************************************************
3 FILENAME: uiuc_1Dinterpolation.cpp
5 ----------------------------------------------------------------------
7 DESCRIPTION: reads in the yData and xData arrays and the value of x
8 to be interpolated on; performs 1D interpolation,
11 ----------------------------------------------------------------------
15 ----------------------------------------------------------------------
17 REFERENCES: syntax based on interp function in c172_aero.c
18 mathematics based on linear interpolation functions
20 Kreyszig, Erwin. Advanced Engineering Mathematics,
21 7th ed. NY: John Wiley & Sons, 1993.
23 ----------------------------------------------------------------------
25 HISTORY: 02/03/2000 initial release
26 09/01/2002 (RD) added second interpolation routine
29 ----------------------------------------------------------------------
31 AUTHOR(S): Jeff Scott <jscott@mail.com>
32 Robert Deters <rdeters@uiuc.edu>
34 ----------------------------------------------------------------------
38 ----------------------------------------------------------------------
40 INPUTS: -array of x data
42 -max number of data pairs
43 -x value to be interpolated on
45 ----------------------------------------------------------------------
47 OUTPUTS: -y as function of x
49 ----------------------------------------------------------------------
51 CALLED BY: uiuc_coefficients.cpp
53 ----------------------------------------------------------------------
57 ----------------------------------------------------------------------
59 COPYRIGHT: (C) 2000 by Michael Selig
61 This program is free software; you can redistribute it and/or
62 modify it under the terms of the GNU General Public License
63 as published by the Free Software Foundation.
65 This program is distributed in the hope that it will be useful,
66 but WITHOUT ANY WARRANTY; without even the implied warranty of
67 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68 GNU General Public License for more details.
70 You should have received a copy of the GNU General Public License
71 along with this program; if not, write to the Free Software
72 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
74 **********************************************************************/
75 #include <simgear/compiler.h> // MSVC: to disable C4244 d to f warning
77 #include "uiuc_1Dinterpolation.h"
80 double uiuc_1Dinterpolation( double xData[100], double yData[100], int xmax, double x )
82 double x1=0, x2=0, y1=0, y2=0, L1=0, L2=0;
86 //check bounds on x to see if data range encloses it
87 // NOTE: [1] is first element of all arrays, [0] not used
88 if (x <= xData[1]) //if x less than lowest x
90 yfx = yData[1]; //let y equal lowest y
92 else if (x >= xData[xmax]) //if x greater than greatest x
94 yfx = yData[xmax]; //let y equal greatest y
96 else //x between xmax and x min
98 /*loop increases i until x is less than a known x,
99 e.g. Alpha from LaRCsim less than Alpha given in
100 tabulated data; once this value is found, i becomes
101 the upper bound and i-1 the lower bound*/
102 while (xData[i] <= x) //bracket upper bound
106 x2 = xData[i]; //set upper bounds
108 x1 = xData[i-1]; //set lower bounds
111 //calculate Langrange polynomial coefficients
112 //(see Kreyszig, pg. 937)
113 L1 = (x - x2) / (x1 - x2);
114 L2 = (x - x1) / (x2 - x1);
117 yfx = L1 * y1 + L2 * y2;
123 int uiuc_1Dinterpolation( double xData[], int yData[], int xmax, double x )
125 double x1=0, x2=0, xdiff=0;
130 //check bounds on x to see if data range encloses it
131 // NOTE: [1] is first element of all arrays, [0] not used
132 if (x <= xData[1]) //if x less than lowest x
134 yfx = yData[1]; //let y equal lowest y
136 else if (x >= xData[xmax]) //if x greater than greatest x
138 yfx = yData[xmax]; //let y equal greatest y
140 else //x between xmax and x min
142 /*loop increases i until x is less than a known x,
143 e.g. Alpha from LaRCsim less than Alpha given in
144 tabulated data; once this value is found, i becomes
145 the upper bound and i-1 the lower bound*/
146 while (xData[i] <= x) //bracket upper bound
150 x2 = xData[i]; //set upper bounds
152 x1 = xData[i-1]; //set lower bounds
158 else if (x < x1+xdiff/2)
166 // end uiuc_1Dinterpolation.cpp