source: trunk/MagicSoft/Simulation/Detector/ReflectorII/lagrange.h@ 14814

Last change on this file since 14814 was 1000, checked in by blanch, 23 years ago
Change to avoid Lagrange function going out of array range.
File size: 2.0 KB
Line 
1/*
2/////////////////////////////////////////////////////////////////
3//
4// lagrange
5//_______________________________________________________________
6//
7// Created: Sun Jun 14 14:10:18 MET DST 1998
8// Author: Jose Carlos Gonzales
9// Purpose: Macro for Lagrange interpolation of 3rd. order
10// Notes:
11//
12/////////////////////////////////////////////////////////////////
13
14
15//++
16// Formula for Lagrange interpolation of 3rd. order
17// x: value to be interpolated
18// t: table(2xN), table[0]: abscissas, table[1]: ordinates
19// n: higher value of abscissas, such that t[0][n] <= x
20//--
21*/
22
23#define Lagrange(t,n,x) ((t[1][ (n) ]*((x-t[0][(n)+1])*(x-t[0][(n)+2]))/ \
24 ((t[0][ (n) ]-t[0][(n)+1])*(t[0][ (n) ]-t[0][(n)+2])))+ \
25 (t[1][(n)+1]*((x-t[0][ (n) ])*(x-t[0][(n)+2]))/ \
26 ((t[0][(n)+1]-t[0][ (n) ])*(t[0][(n)+1]-t[0][(n)+2])))+ \
27 (t[1][(n)+2]*((x-t[0][ (n) ])*(x-t[0][(n)+1]))/ \
28 ((t[0][(n)+2]-t[0][ (n) ])*(t[0][(n)+2]-t[0][(n)+1]))) \
29 )
30
31/*
32//++
33// Macro to find, and save in variable "m", the value of
34// "n" to be used in the "Lagrange{t,n,x)" macro
35//--
36*/
37
38#define FindLagrange(t,m,x) {m = 0; while (t[0][++m] < x) if (m+1>=nReflectivity) break;} --m
39
40/*
41//////////////////////////////////////////////////////////////////////////////
42// Here follows a sample program using this two macros
43//////////////////////////////////////////////////////////////////////////////
44// #include <iostream.h>
45// #include "lagrange.h"
46//
47// void main(void)
48// {
49// float data[2][20];
50// int i, number;
51// float x, y;
52//
53// for (i=0; i<20; ++i) {
54// data[0][i] = i*10.;
55// data[1][i] = 3.0*data[0][i]*data[0][i];
56// cout << data[0][i] << ' ' << data[1][i] << '\n';
57// }
58//
59// while (1==1) {
60// cout << "Enter x = ";
61// cin >> x;
62// FindLagrange(data,number,x);
63// y = Lagrange(data,number,x);
64// cout << x << ' ' << y << '\n';
65// }
66// }
67////////////////////////////////////////////////////////////////////////////// */
68
Note: See TracBrowser for help on using the repository browser.