Subversion Repositories programming

Rev

Rev 144 | Rev 146 | 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
/* Copied from the Project #2 Handout.
22
 *
23
 * FOR TESTING ONLY
24
 */
25
float Bessel_Recursive_BU (int n, float x, float base0, float base1)
26
{
27
    if (n == 0)
28
        return base0;
29
    else if (n == 1)
30
        return base1;
31
 
32
    return Bessel_Recursive_BU (n-2, x, base0, base1)
33
           + 2.0F * (float)(n-1) * Bessel_Recursive_BU (n-1, x, base0, base1) / x;
34
}
35
 
36
float Bessel_Recursive_TD (int n, float x, float base19, float base20)
37
{
38
    if (n == 19)
39
        return base19;
40
    else if (n == 20)
41
        return base20;
42
 
43
    return (2.0F * (float)(n+1) / x) * Bessel_Recursive_TD (n+1, x, base19, base20)
44
           + Bessel_Recursive_TD (n+2, x, base19, base20);
45
}
46
 
47
 
48
/* Find the Bessel Function.
49
 * Algorithm: I[n+1](x) = I[n-1](x) - (2*n/x) * I[n](x)
50
 * Which is equvalent to: I[n](x) = I[n-2](x) - (2 * (n-1)/x) * I[n-1](x)
51
 * by simple substitution.
52
 *
53
 * This is the recurrence relation for PART #1.
54
 */
145 ira 55
float Bessel_BottomUp (int n, float x, float *constants)
144 ira 56
{
57
    int i = 0;
58
    float  answer;
59
    float *storage = new float[ARRAY_SIZE];
60
 
61
    // Prime the array
145 ira 62
    storage[0] = constants[IDX0];
63
    storage[1] = constants[IDX1];
144 ira 64
 
65
    // Calculate each value in turn
66
    for (i=2; i<=20; i++)
67
        storage[i] = storage[i-2] - (2.0 * (n-1) / x) * storage[i-1];
68
 
69
    // Store the answer, delete the memory
70
    answer = storage[n];
71
    delete[] storage;
72
 
73
    // Return the result
74
    return answer;
75
}
76
 
77
/* Find the Bessel Function, from the Top -> Down.
78
 * Algorithm: I[n-1](x) = (2*n/x)*I[n](x) + I[n+1](x)
79
 * Which is equivalent to: I[n](x) = (2 * (n+1) / x) * I[n+1](x) + I[n+2](x)
80
 * by simple substitution.
81
 *
82
 * This is the recurrence relation for PART #2.
83
 */
145 ira 84
float Bessel_TopDown (int n, float x, float *constants)
144 ira 85
{
86
    int i = 18;
87
    float answer;
88
    float *storage = new float[ARRAY_SIZE];
89
 
90
    // Prime the array
145 ira 91
    storage[20] = constants[IDX20];
92
    storage[19] = constants[IDX19];
144 ira 93
 
94
    // Calculate each value in turn
95
    for (i=18; i>=0; i--)
96
        storage[i] = (2.0 * (n+1) / x) * storage[i+1] + storage[i+2];
97
 
98
    // Store the answer, free the memory
99
    answer = storage[n];
100
    delete[] storage;
101
 
102
    // Return the result
103
    return answer;
104
}
105
 
106
/* Print out a table for a given x value, and function.
107
 * Call like this: print_table (1.0, &BesselFunc);
108
 *
109
 * It chooses the correct constants appropriately.
110
 */
145 ira 111
void print_table (float x, float (*bessel_func)(int, float, float*))
144 ira 112
{
113
    int n;
114
    float true_val, calc_val, abs_err, rel_err;
115
    float *constants;
116
 
145 ira 117
    if (x == 1.0) constants = x1_const;
118
    if (x == 2.0) constants = x2_const;
119
    if (x == 5.0) constants = x5_const;
144 ira 120
 
121
    // Set the output flags
122
    cout << setiosflags (ios_base::scientific | ios_base::showpoint);
123
 
145 ira 124
    // A line of equal signs
125
    for (n=0; n<(20+20+20+20+10); n++)
126
        cout << "=";
127
 
128
    cout << endl;
129
 
130
    // Print the x value we're using
131
    cout << "Using x = " << x << endl;
132
 
144 ira 133
    // Print the table header
134
    cout << setw(10) << "n Val."
135
         << setw(20) << "TRUE"
136
         << setw(20) << "COMPUTED"
137
         << setw(20) << "ABS ERROR"
138
         << setw(20) << "REL ERROR"
139
         << endl;
140
 
141
    // A line of equal signs
142
    for (n=0; n<(20+20+20+20+10); n++)
143
        cout << "=";
144
 
145
    cout << endl;
146
 
145 ira 147
    // Run once for n = 5,10,15,20
148
    for (n=5; n<=20; n+=5)
149
    {
150
        if (n==5 ) true_val = constants[IDX5];
151
        if (n==10) true_val = constants[IDX10];
152
        if (n==15) true_val = constants[IDX15];
153
        if (n==20) true_val = constants[IDX20];
144 ira 154
 
145 ira 155
        calc_val = bessel_func(n, x, constants);
156
        abs_err  = abs (true_val - calc_val);
157
        rel_err  = abs_err / abs (true_val);
144 ira 158
 
145 ira 159
        cout << setw(10) << n
160
             << setw(20) << true_val
161
             << setw(20) << calc_val
162
             << setw(20) << abs_err
163
             << setw(20) << rel_err
164
             << endl;
165
    }
144 ira 166
}
167
 
168
int main (void)
169
{
170
    print_table (1.0, &Bessel_BottomUp);
171
    cout << endl;
172
    print_table (2.0, &Bessel_BottomUp);
173
    cout << endl;
174
    print_table (5.0, &Bessel_BottomUp);
175
    cout << endl;
176
 
177
 
178
    return 0;
179
}
180