Subversion Repositories programming

Rev

Rev 145 | Rev 147 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 ira 1
#include <iostream>
2
#include <iomanip>
3
#include <cmath>
4
using namespace std;
5
 
6
enum {IDX0, IDX1, IDX5, IDX10, IDX15, IDX19, IDX20};
7
 
8
#define ARRAY_SIZE 21
9
float x1_const[7] = { 1.266065978,     5.651591040E-01, 2.714631560E-04,
10
                      2.752948040E-10, 2.370463051E-17, 1.587678369E-23,
11
                      3.966835986E-25 };
12
 
13
float x2_const[7] = { 2.279585302,     1.590636855,     9.825679323E-03,
14
                      3.016963879E-07, 8.139432531E-13, 8.641603385E-18,
15
                      4.310560576E-19 };
16
 
17
float x5_const[7] = { 2.723987182E+01, 2.433564214E+01, 2.157974547,
18
                      4.580044419E-03, 1.047977675E-6 , 4.078415017E-10,
19
                      5.024239358E-11 };
20
 
21
/* Find the Bessel Function.
22
 * Algorithm: I[n+1](x) = I[n-1](x) - (2*n/x) * I[n](x)
23
 * Which is equvalent to: I[n](x) = I[n-2](x) - (2 * (n-1)/x) * I[n-1](x)
24
 * by simple substitution.
25
 *
26
 * This is the recurrence relation for PART #1.
27
 */
145 ira 28
float Bessel_BottomUp (int n, float x, float *constants)
144 ira 29
{
30
    int i = 0;
31
    float  answer;
32
    float *storage = new float[ARRAY_SIZE];
33
 
34
    // Prime the array
145 ira 35
    storage[0] = constants[IDX0];
36
    storage[1] = constants[IDX1];
144 ira 37
 
38
    // Calculate each value in turn
39
    for (i=2; i<=20; i++)
40
        storage[i] = storage[i-2] - (2.0 * (n-1) / x) * storage[i-1];
41
 
42
    // Store the answer, delete the memory
43
    answer = storage[n];
44
    delete[] storage;
45
 
46
    // Return the result
47
    return answer;
48
}
49
 
50
/* Find the Bessel Function, from the Top -> Down.
51
 * Algorithm: I[n-1](x) = (2*n/x)*I[n](x) + I[n+1](x)
52
 * Which is equivalent to: I[n](x) = (2 * (n+1) / x) * I[n+1](x) + I[n+2](x)
53
 * by simple substitution.
54
 *
55
 * This is the recurrence relation for PART #2.
56
 */
145 ira 57
float Bessel_TopDown (int n, float x, float *constants)
144 ira 58
{
59
    int i = 18;
60
    float answer;
61
    float *storage = new float[ARRAY_SIZE];
62
 
63
    // Prime the array
145 ira 64
    storage[20] = constants[IDX20];
65
    storage[19] = constants[IDX19];
144 ira 66
 
67
    // Calculate each value in turn
68
    for (i=18; i>=0; i--)
69
        storage[i] = (2.0 * (n+1) / x) * storage[i+1] + storage[i+2];
70
 
71
    // Store the answer, free the memory
72
    answer = storage[n];
73
    delete[] storage;
74
 
75
    // Return the result
76
    return answer;
77
}
78
 
79
/* Print out a table for a given x value, and function.
80
 * Call like this: print_table (1.0, &BesselFunc);
81
 *
82
 * It chooses the correct constants appropriately.
83
 */
145 ira 84
void print_table (float x, float (*bessel_func)(int, float, float*))
144 ira 85
{
86
    int n;
87
    float true_val, calc_val, abs_err, rel_err;
88
    float *constants;
89
 
145 ira 90
    if (x == 1.0) constants = x1_const;
91
    if (x == 2.0) constants = x2_const;
92
    if (x == 5.0) constants = x5_const;
144 ira 93
 
94
    // Set the output flags
95
    cout << setiosflags (ios_base::scientific | ios_base::showpoint);
96
 
145 ira 97
    // A line of equal signs
98
    for (n=0; n<(20+20+20+20+10); n++)
99
        cout << "=";
100
 
101
    cout << endl;
102
 
103
    // Print the x value we're using
104
    cout << "Using x = " << x << endl;
105
 
144 ira 106
    // Print the table header
107
    cout << setw(10) << "n Val."
108
         << setw(20) << "TRUE"
109
         << setw(20) << "COMPUTED"
110
         << setw(20) << "ABS ERROR"
111
         << setw(20) << "REL ERROR"
112
         << endl;
113
 
114
    // A line of equal signs
115
    for (n=0; n<(20+20+20+20+10); n++)
116
        cout << "=";
117
 
118
    cout << endl;
119
 
145 ira 120
    // Run once for n = 5,10,15,20
121
    for (n=5; n<=20; n+=5)
122
    {
123
        if (n==5 ) true_val = constants[IDX5];
124
        if (n==10) true_val = constants[IDX10];
125
        if (n==15) true_val = constants[IDX15];
126
        if (n==20) true_val = constants[IDX20];
144 ira 127
 
145 ira 128
        calc_val = bessel_func(n, x, constants);
129
        abs_err  = abs (true_val - calc_val);
130
        rel_err  = abs_err / abs (true_val);
144 ira 131
 
145 ira 132
        cout << setw(10) << n
133
             << setw(20) << true_val
134
             << setw(20) << calc_val
135
             << setw(20) << abs_err
136
             << setw(20) << rel_err
137
             << endl;
138
    }
146 ira 139
 
140
    cout << endl;
144 ira 141
}
142
 
143
int main (void)
144
{
145
    print_table (1.0, &Bessel_BottomUp);
146
    print_table (2.0, &Bessel_BottomUp);
147
    print_table (5.0, &Bessel_BottomUp);
146 ira 148
    cout << endl << endl;
149
    print_table (1.0, &Bessel_TopDown);
150
    print_table (2.0, &Bessel_TopDown);
151
    print_table (5.0, &Bessel_TopDown);
144 ira 152
 
153
    return 0;
154
}
155