]> git.mxchange.org Git - flightgear.git/blob - src/FDM/UIUCModel/uiuc_1Dinterpolation.cpp
Make yasim accept the launchbar and hook properties. They are not tied to anything...
[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                09/01/2002   (RD) added second interpolation routine
27                             for integer case
28
29 ----------------------------------------------------------------------
30
31  AUTHOR(S):    Jeff Scott         <jscott@mail.com>
32                Robert Deters      <rdeters@uiuc.edu>
33
34 ----------------------------------------------------------------------
35
36  VARIABLES:
37
38 ----------------------------------------------------------------------
39
40  INPUTS:       -array of x data
41                -array of y data
42                -max number of data pairs
43                -x value to be interpolated on
44
45 ----------------------------------------------------------------------
46
47  OUTPUTS:      -y as function of x
48
49 ----------------------------------------------------------------------
50
51  CALLED BY:    uiuc_coefficients.cpp
52
53 ----------------------------------------------------------------------
54
55  CALLS TO:     none
56
57 ----------------------------------------------------------------------
58
59  COPYRIGHT:    (C) 2000 by Michael Selig
60
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.
64
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.
69
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
73  USA or view http://www.gnu.org/copyleft/gpl.html.
74
75 **********************************************************************/
76 #include <simgear/compiler.h>    // MSVC: to disable C4244 d to f warning
77
78 #include "uiuc_1Dinterpolation.h"
79
80
81 double uiuc_1Dinterpolation( double xData[100], double yData[100], int xmax, double x )
82 {
83   double x1=0, x2=0, y1=0, y2=0, L1=0, L2=0;
84   int i=2;
85   float yfx=0;
86
87   //check bounds on x to see if data range encloses it
88   // NOTE: [1] is first element of all arrays, [0] not used
89   if (x <= xData[1])         //if x less than lowest x
90     {
91       yfx = yData[1];        //let y equal lowest y
92     }
93   else if (x >= xData[xmax]) //if x greater than greatest x
94     {
95       yfx = yData[xmax];     //let y equal greatest y
96     }
97   else                       //x between xmax and x min
98     {
99       /*loop increases i until x is less than a known x, 
100         e.g. Alpha from LaRCsim less than Alpha given in 
101         tabulated data; once this value is found, i becomes 
102         the upper bound and i-1 the lower bound*/
103       while (xData[i] <= x)    //bracket upper bound
104         {
105           i++;
106         }
107       x2 = xData[i];          //set upper bounds
108       y2 = yData[i];
109       x1 = xData[i-1];        //set lower bounds
110       y1 = yData[i-1];
111
112       //calculate Langrange polynomial coefficients 
113       //(see Kreyszig, pg. 937)
114       L1 = (x - x2) / (x1 - x2);
115       L2 = (x - x1) / (x2 - x1);
116
117       //solve for y=f(x)
118       yfx = L1 * y1 + L2 * y2;
119     }
120   return yfx;
121 }
122
123
124 int uiuc_1Dinterpolation( double xData[], int yData[], int xmax, double x )
125 {
126   double x1=0, x2=0, xdiff=0;
127   int y1=0, y2=0;
128   int i=2;
129   int yfx=0;
130
131   //check bounds on x to see if data range encloses it
132   // NOTE: [1] is first element of all arrays, [0] not used
133   if (x <= xData[1])         //if x less than lowest x
134     {
135       yfx = yData[1];        //let y equal lowest y
136     }
137   else if (x >= xData[xmax]) //if x greater than greatest x
138     {
139       yfx = yData[xmax];     //let y equal greatest y
140     }
141   else                       //x between xmax and x min
142     {
143       /*loop increases i until x is less than a known x, 
144         e.g. Alpha from LaRCsim less than Alpha given in 
145         tabulated data; once this value is found, i becomes 
146         the upper bound and i-1 the lower bound*/
147       while (xData[i] <= x)    //bracket upper bound
148         {
149           i++;
150         }
151       x2 = xData[i];          //set upper bounds
152       y2 = yData[i];
153       x1 = xData[i-1];        //set lower bounds
154       y1 = yData[i-1];
155
156       xdiff = x2 - x1;
157       if (y1 == y2)
158         yfx = y1;
159       else if (x < x1+xdiff/2)
160         yfx = y1;
161       else
162         yfx = y2;
163     }
164   return yfx;
165 }
166
167 // end uiuc_1Dinterpolation.cpp