]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UIUCModel/uiuc_1Dinterpolation.cpp
Updated to match changes in radiostack.[ch]xx
[flightgear.git] / src / FDM / UIUCModel / uiuc_1Dinterpolation.cpp
1 /**********************************************************************
2
3  FILENAME:     uiuc_1Dinterpolation.cpp
4
5 ----------------------------------------------------------------------
6
7  DESCRIPTION:  reads in the yData and xData arrays and the value of x 
8                to be interpolated on; performs 1D interpolation, 
9                i.e. y=f(x)
10
11 ----------------------------------------------------------------------
12
13  STATUS:       alpha version
14
15 ----------------------------------------------------------------------
16
17  REFERENCES:   syntax based on interp function in c172_aero.c
18                mathematics based on linear interpolation functions 
19                found in
20                Kreyszig, Erwin. Advanced Engineering Mathematics, 
21                7th ed. NY: John Wiley & Sons, 1993.
22
23 ----------------------------------------------------------------------
24
25  HISTORY:      02/03/2000   initial release
26
27 ----------------------------------------------------------------------
28
29  AUTHOR(S):    Jeff Scott         <jscott@mail.com>
30
31 ----------------------------------------------------------------------
32
33  VARIABLES:
34
35 ----------------------------------------------------------------------
36
37  INPUTS:       -array of x data
38                -array of y data
39                -max number of data pairs
40                -x value to be interpolated on
41
42 ----------------------------------------------------------------------
43
44  OUTPUTS:      -y as function of x
45
46 ----------------------------------------------------------------------
47
48  CALLED BY:    uiuc_coefficients.cpp
49
50 ----------------------------------------------------------------------
51
52  CALLS TO:     none
53
54 ----------------------------------------------------------------------
55
56  COPYRIGHT:    (C) 2000 by Michael Selig
57
58  This program is free software; you can redistribute it and/or
59  modify it under the terms of the GNU General Public License
60  as published by the Free Software Foundation.
61
62  This program is distributed in the hope that it will be useful,
63  but WITHOUT ANY WARRANTY; without even the implied warranty of
64  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
65  GNU General Public License for more details.
66
67  You should have received a copy of the GNU General Public License
68  along with this program; if not, write to the Free Software
69  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
70  USA or view http://www.gnu.org/copyleft/gpl.html.
71
72 **********************************************************************/
73 #include <simgear/compiler.h>    // MSVC: to disable C4244 d to f warning
74
75 #include "uiuc_1Dinterpolation.h"
76
77
78 double uiuc_1Dinterpolation( double xData[100], double yData[100], int xmax, double x )
79 {
80   double x1=0, x2=0, y1=0, y2=0, L1=0, L2=0;
81   int i=2;
82   float yfx=0;
83
84   //check bounds on x to see if data range encloses it
85   // NOTE: [1] is first element of all arrays, [0] not used
86   if (x <= xData[1])         //if x less than lowest x
87     {
88       yfx = yData[1];        //let y equal lowest y
89     }
90   else if (x >= xData[xmax]) //if x greater than greatest x
91     {
92       yfx = yData[xmax];     //let y equal greatest y
93     }
94   else                       //x between xmax and x min
95     {
96       /*loop increases i until x is less than a known x, 
97         e.g. Alpha from LaRCsim less than Alpha given in 
98         tabulated data; once this value is found, i becomes 
99         the upper bound and i-1 the lower bound*/
100       while (xData[i] <= x)    //bracket upper bound
101         {
102           i++;
103         }
104       x2 = xData[i];          //set upper bounds
105       y2 = yData[i];
106       x1 = xData[i-1];        //set lower bounds
107       y1 = yData[i-1];
108
109       //calculate Langrange polynomial coefficients 
110       //(see Kreyszig, pg. 937)
111       L1 = (x - x2) / (x1 - x2);
112       L2 = (x - x1) / (x2 - x1);
113
114       //solve for y=f(x)
115       yfx = L1 * y1 + L2 * y2;
116     }
117   return yfx;
118 }
119
120 // end uiuc_1Dinterpolation.cpp