Subversion Repositories programming

Rev

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

Rev Author Line No. Line
168 ira 1
/* Copyright: Ira W. Snyder
2
 * Start Date: 2005-11-28
3
 * End Date:
4
 * License: Public Domain
5
 *
6
 * Changelog Follows:
7
 * 2005-11-28
8
 * - Implement eval_euler() and eval_trapezoid().
9
 *
10
 * 2005-11-29
169 ira 11
 * - Implement eval_romberg() and eval_simpson().
168 ira 12
 *
13
 */
14
 
15
#include <cmath>
16
#include <cstdlib>
17
#include <cstdio>
18
#include <iostream>
19
using namespace std;
20
 
169 ira 21
/**
22
 * The function given in the Project #5 Handout.
23
 * This is 1 + x^28
24
 *
25
 * @param x the point at which to evaluate the function
26
 */
27
float func (float x)
168 ira 28
{
29
    return 1.0 + pow (x, 28);
30
}
31
 
169 ira 32
/**
33
 * Evaluate the integral of the function given, over the interval given,
34
 * using the Euler method.
35
 *
36
 * @param a the lower bound of the integral
37
 * @param b the upper bound of the integral
38
 * @param n the number of intervals to use
39
 * @param f the function to evaluate
40
 */
41
float eval_euler (const float a, const float b, const int n, float(*f)(float))
168 ira 42
{
43
    const float h = (b - a) / n;
44
    float answer = 0.0, xi = 0.0;
45
    int i = 0;
46
 
47
    for (i=0; i<=n; i++)
48
    {
49
        xi = a + (i * h);
50
        answer += f(xi);
51
    }
52
 
53
    return h * answer;
54
}
55
 
169 ira 56
/**
57
 * Evaluate the integral of the function given, over the interval given,
58
 * using the trapezoid method.
59
 *
60
 * @param a the lower bound of the integral
61
 * @param b the upper bound of the integral
62
 * @param n the number of intervals to use
63
 * @param f the function to evaluate
64
 */
65
float eval_trapezoid (const float a, const float b, const int n, float(*f)(float))
168 ira 66
{
67
    const float h = (b - a) / n;
68
    float answer = 0.0, xi_last = 0.0, xi_now = 0.0;
69
    int i = 0;
70
 
71
    xi_last = a + (i * h); // i = 0 here
72
 
73
    for (i=1; i<=n; i++)
74
    {
75
        xi_now = a + (i * h);
76
        answer += (f (xi_last) + f (xi_now) * (xi_now - xi_last));
77
        xi_last = xi_now; // shift xi's
78
    }
79
 
80
    return answer;
81
}
82
 
169 ira 83
/**
84
 * Evaluate the integral of the function given, over the interval given,
85
 * using the Romberg method.
86
 *
87
 * This is directly based on the pseudocode on Pg 223-224 of the textbook.
88
 *
89
 * @param a the lower bound of the integral
90
 * @param b the upper bound of the integral
91
 * @param n the number of intervals to use
92
 * @param f the function to evaluate
93
 */
94
float eval_romberg (const float a, const float b, const int n, float(*f)(float))
168 ira 95
{
96
    float R[n+1][n+1];
97
    int i, j, k;
98
    float h, sum;
99
 
100
    h = b - a;
101
    R[0][0] = (h/2.0) * (f(a) + f(b));
102
 
103
    for (i=1; i<=n; i++)
104
    {
105
        h = h/2.0;
106
        sum = 0.0;
107
 
108
        for (k=1; k<=(pow(2.0, i) - 1); k+=2)
109
            sum = sum + f(a + k * h);
110
 
111
        R[i][0] = (1.0/2.0) * R[i-1][0] + sum * h;
112
 
113
        for (j=1; j<=i; j++)
114
            R[i][j] = R[i][j-1] + (R[i][j-1] - R[i-1][j-1]) / (pow(4.0, j) - 1);
115
    }
116
 
117
    return R[n][n];
118
}
119
 
169 ira 120
/**
121
 * Evaluate the integral of the function given, over the interval given,
122
 * using the Simpson method.
123
 *
124
 * This is derived from the formula on Pg 237-238
125
 *
126
 * @param a the lower bound of the integral
127
 * @param b the upper bound of the integral
128
 * @param n the number of intervals to use
129
 * @param f the function to evaluate
130
 */
131
float eval_simpson (const float a, const float b, const int n, float(*f)(float))
168 ira 132
{
169 ira 133
    const float h = (b - a) / n;
134
    float sum1=0.0, sum2=0.0;
135
    int i;
168 ira 136
 
169 ira 137
    /* Get the first sum in the formula on Pg 238 */
138
    for (i=1, sum1=0.0; i<=n/2; i++)
139
        sum1 += f (a + (2 * i - 1) * h);
140
 
141
    sum1 *= 4;
142
 
143
    /* Get the second sum in the formula on Pg 238 */
144
    for (i=1, sum2=0.0; i<=(n-2)/2; i++)
145
        sum2 += f (a + 2 * i * h);
146
 
147
    sum2 *= 2;
148
 
149
    /* Add it all together, and return it */
150
    return (h / 3.0) * ((f(a) + f(b)) + sum1 + sum2);
168 ira 151
}
152
 
153
int main (void)
154
{
169 ira 155
    const float a = 0.0;
156
    const float b = 3.0;
157
    int n = 0;
158
 
168 ira 159
    return 0;
160
}
161