Rev 146 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <iostream>#include <iomanip>#include <cmath>using namespace std;// Global Variablesenum {IDX0, IDX1, IDX5, IDX10, IDX15, IDX19, IDX20}; // Constants' Indicesfloat x1_const[7] = { 1.266065978, 5.651591040E-01, 2.714631560E-04,2.752948040E-10, 2.370463051E-17, 1.587678369E-23,3.966835986E-25 };float x2_const[7] = { 2.279585302, 1.590636855, 9.825679323E-03,3.016963879E-07, 8.139432531E-13, 8.641603385E-18,4.310560576E-19 };float x5_const[7] = { 2.723987182E+01, 2.433564214E+01, 2.157974547,4.580044419E-03, 1.047977675E-6 , 4.078415017E-10,5.024239358E-11 };// Global Defines#define ARRAY_SIZE 21// Function Prototypesfloat Bessel_BottomUp (int n, float x, float *constants);float Bessel_TopDown (int n, float x, float *constants);void print_table_bottomup (float x);void print_table_topdown (float x);void print_equals (int num);/* Find the Bessel Function.* Algorithm: I[n+1](x) = I[n-1](x) - (2*n/x) * I[n](x)* Which is equvalent to: I[n](x) = I[n-2](x) - (2 * (n-1)/x) * I[n-1](x)* by simple substitution.** This is the recurrence relation for PART #1.*/float Bessel_BottomUp (int n, float x, float *constants){int i = 0;float answer;float *storage = new float[ARRAY_SIZE];// Prime the arraystorage[0] = constants[IDX0];storage[1] = constants[IDX1];// Calculate each value in turnfor (i=2; i<=n; i++)storage[i] = storage[i-2] - (2.0 * (n-1) / x) * storage[i-1];// Store the answer, delete the memoryanswer = storage[n];delete[] storage;// Return the resultreturn answer;}/* Find the Bessel Function, from the Top -> Down.* Algorithm: I[n-1](x) = (2*n/x)*I[n](x) + I[n+1](x)* Which is equivalent to: I[n](x) = (2 * (n+1) / x) * I[n+1](x) + I[n+2](x)* by simple substitution.** This is the recurrence relation for PART #2.*/float Bessel_TopDown (int n, float x, float *constants){int i = 18;float answer;float *storage = new float[ARRAY_SIZE];// Prime the arraystorage[20] = constants[IDX20];storage[19] = constants[IDX19];// Calculate each value in turnfor (i=18; i>=n; i--)storage[i] = (2.0 * (n+1) / x) * storage[i+1] + storage[i+2];// Store the answer, free the memoryanswer = storage[n];delete[] storage;// Return the resultreturn answer;}/* Print out a table for a given x value, and function.* Call like this: print_table (1.0, &BesselFunc);** It chooses the correct constants appropriately.*/void print_table_bottomup (float x){int n, tblwidth = 6+16+16+16+16+16+20;float true_val, calc_val, abs_err, rel_err, part1, part2;float *constants;if (x == 1.0) constants = x1_const;if (x == 2.0) constants = x2_const;if (x == 5.0) constants = x5_const;// Reset the output flagscout << resetiosflags (ios_base::scientific | ios_base::showpoint);// A line of equal signsprint_equals (tblwidth);// Print the x value we're usingcout << "Using x = " << x<< " "<< "Working Bottom-up" << endl;// Set the output flags we're usingcout << setiosflags (ios_base::scientific | ios_base::showpoint);// Print the table headercout << setw(6) << "n Val."<< setw(16) << "TRUE VAL"<< setw(16) << "COMPUTED"<< setw(16) << "ABS ERROR"<< setw(16) << "REL ERROR"<< setw(16) << "I[n-2](x)"<< setw(20) << "(2(n-1)/x)I[n-1](x)"<< setw(16) << "IraVal"<< endl;// A line of equal signsprint_equals (tblwidth);// Run once for n = 5,10,15,20for (n=5; n<=20; n+=5){if (n==5 ) true_val = constants[IDX5];if (n==10) true_val = constants[IDX10];if (n==15) true_val = constants[IDX15];if (n==20) true_val = constants[IDX20];calc_val = Bessel_BottomUp(n, x, constants);abs_err = abs (true_val - calc_val);rel_err = abs_err / abs (true_val);part1 = Bessel_BottomUp(n-2, x, constants);part2 = (2.0 * (n-1) / x) * Bessel_BottomUp(n-1, x, constants);cout << setw(6) << n<< setw(16) << true_val<< setw(16) << calc_val<< setw(16) << abs_err<< setw(16) << rel_err<< setw(16) << part1<< setw(20) << part2<< setw(16) << part1 - part2<< endl;}cout << endl;}void print_table_topdown (float x){int n, tblwidth = 6+16+16+16+16+16+20;float true_val, calc_val, abs_err, rel_err, part1, part2;float *constants;bool done = false;if (x == 1.0) constants = x1_const;if (x == 2.0) constants = x2_const;if (x == 5.0) constants = x5_const;// Reset the output flagscout << resetiosflags (ios_base::scientific | ios_base::showpoint);// A line of equal signsprint_equals (tblwidth);// Print the x value we're usingcout << "Using x = " << x<< " "<< "Working Top-down" << endl;// Set the output flags we're usingcout << setiosflags (ios_base::scientific | ios_base::showpoint);// Print the table headercout << setw(6) << "n Val."<< setw(16) << "TRUE VAL"<< setw(16) << "COMPUTED"<< setw(16) << "ABS ERROR"<< setw(16) << "REL ERROR"<< setw(16) << "I[n+2](x)"<< setw(20) << "(2(n+1)/x)I[n+1](x)"<< setw(16) << "IraVal"<< endl;// A line of equal signsprint_equals (tblwidth);n = 0;// Run once for n = 0,1,5,10,15while (!done){switch (n){case 0: true_val = constants[IDX0]; break;case 1: true_val = constants[IDX1]; break;case 5: true_val = constants[IDX5]; break;case 10: true_val = constants[IDX10]; break;case 15: true_val = constants[IDX15]; break;}calc_val = Bessel_TopDown(n, x, constants);abs_err = abs (true_val - calc_val);rel_err = abs_err / abs (true_val);part1 = Bessel_TopDown(n+2, x, constants);part2 = (2.0 * (n+1) / x) * Bessel_TopDown(n+1, x, constants);cout << setw(6) << n<< setw(16) << true_val<< setw(16) << calc_val<< setw(16) << abs_err<< setw(16) << rel_err<< setw(16) << part1<< setw(20) << part2<< setw(16) << (part1 + part2)<< endl;switch (n) // Set n for the next loop{case 0: n = 1; break;case 1: n = 5; break;case 5: n = 10; break;case 10: n = 15; break;case 15: done = true; break;}}cout << endl;}void print_equals (int num){int i;for (i=0; i<num; i++)cout << "=";cout << endl;}int main (void){print_table_bottomup (1.0);print_table_bottomup (2.0);print_table_bottomup (5.0);print_table_topdown (1.0);print_table_topdown (2.0);print_table_topdown (5.0);return 0;}