| 1 | ///////////////////////////////////////////////////////////////// | 
|---|
| 2 | // | 
|---|
| 3 | // lagrange | 
|---|
| 4 | //_______________________________________________________________ | 
|---|
| 5 | // | 
|---|
| 6 | //  Created: Sun Jun 14 14:10:18 MET DST 1998 | 
|---|
| 7 | //  Author:  Jose Carlos Gonzales | 
|---|
| 8 | //  Purpose: Macro for Lagrange interpolation of 3rd. order | 
|---|
| 9 | //  Notes: | 
|---|
| 10 | // | 
|---|
| 11 | ///////////////////////////////////////////////////////////////// | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | // @T \newpage | 
|---|
| 15 |  | 
|---|
| 16 | // @section Source code of {\tt lagrange.h} | 
|---|
| 17 |  | 
|---|
| 18 | // @code | 
|---|
| 19 |  | 
|---|
| 20 | //++ | 
|---|
| 21 | // Formula for Lagrange interpolation of 3rd. order | 
|---|
| 22 | // x: value to be interpolated | 
|---|
| 23 | // t: table(2xN), table[0]: abscissas, table[1]: ordinates | 
|---|
| 24 | // n: higher value of abscissas, such that t[0][n] <= x | 
|---|
| 25 | //-- | 
|---|
| 26 |  | 
|---|
| 27 | #define Lagrange(t,n,x)   ((t[1][ (n) ]*((x-t[0][(n)+1])*(x-t[0][(n)+2]))/ \ | 
|---|
| 28 | ((t[0][ (n) ]-t[0][(n)+1])*(t[0][ (n) ]-t[0][(n)+2])))+ \ | 
|---|
| 29 | (t[1][(n)+1]*((x-t[0][ (n) ])*(x-t[0][(n)+2]))/ \ | 
|---|
| 30 | ((t[0][(n)+1]-t[0][ (n) ])*(t[0][(n)+1]-t[0][(n)+2])))+ \ | 
|---|
| 31 | (t[1][(n)+2]*((x-t[0][ (n) ])*(x-t[0][(n)+1]))/ \ | 
|---|
| 32 | ((t[0][(n)+2]-t[0][ (n) ])*(t[0][(n)+2]-t[0][(n)+1]))) \ | 
|---|
| 33 | ) | 
|---|
| 34 |  | 
|---|
| 35 | //++ | 
|---|
| 36 | // Macro to find, and save in variable "m", the value of | 
|---|
| 37 | // "n" to be used in the "Lagrange{t,n,x)" macro | 
|---|
| 38 | //-- | 
|---|
| 39 |  | 
|---|
| 40 | #define FindLagrange(t,m,x)  {m = 0; while (t[0][++m] < x);} --m | 
|---|
| 41 |  | 
|---|
| 42 | // @endcode | 
|---|
| 43 |  | 
|---|
| 44 | // @subsection Sample program | 
|---|
| 45 |  | 
|---|
| 46 | // @code | 
|---|
| 47 |  | 
|---|
| 48 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 49 | // Here follows a sample program using this two macros | 
|---|
| 50 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 51 | // #include <iostream.h> | 
|---|
| 52 | // #include "lagrange.h" | 
|---|
| 53 | // | 
|---|
| 54 | // void main(void) | 
|---|
| 55 | // { | 
|---|
| 56 | //   float data[2][20]; | 
|---|
| 57 | //   int i, number; | 
|---|
| 58 | //   float x, y; | 
|---|
| 59 | // | 
|---|
| 60 | //   for (i=0; i<20; ++i) { | 
|---|
| 61 | //     data[0][i] = i*10.; | 
|---|
| 62 | //     data[1][i] = 3.0*data[0][i]*data[0][i]; | 
|---|
| 63 | //     cout << data[0][i] << ' ' << data[1][i] << '\n'; | 
|---|
| 64 | //   } | 
|---|
| 65 | // | 
|---|
| 66 | //   while (1==1) { | 
|---|
| 67 | //     cout << "Enter x = "; | 
|---|
| 68 | //     cin >> x; | 
|---|
| 69 | //     FindLagrange(data,number,x); | 
|---|
| 70 | //     y = Lagrange(data,number,x); | 
|---|
| 71 | //     cout << x << ' ' << y << '\n'; | 
|---|
| 72 | //   } | 
|---|
| 73 | // } | 
|---|
| 74 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 75 |  | 
|---|
| 76 | // @endcode | 
|---|