Subversion Repositories programming

Rev

Rev 169 | Rev 171 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 169 Rev 170
Line 1... Line 1...
1
/* Copyright: Ira W. Snyder
1
/* Copyright: Ira W. Snyder
2
 * Start Date: 2005-11-28
2
 * Start Date: 2005-11-28
3
 * End Date:
3
 * End Date: 2005-11-29
4
 * License: Public Domain
4
 * License: Public Domain
5
 *
5
 *
6
 * Changelog Follows:
6
 * Changelog Follows:
7
 * 2005-11-28
7
 * 2005-11-28
8
 * - Implement eval_euler() and eval_trapezoid().
8
 * - Implement eval_euler() and eval_trapezoid().
Line 11... Line 11...
11
 * - Implement eval_romberg() and eval_simpson().
11
 * - Implement eval_romberg() and eval_simpson().
12
 *
12
 *
13
 */
13
 */
14
 
14
 
15
#include <cmath>
15
#include <cmath>
16
#include <cstdlib>
-
 
17
#include <cstdio>
16
#include <cstdio>
18
#include <iostream>
-
 
19
using namespace std;
17
using namespace std;
20
 
18
 
21
/**
19
/**
22
 * The function given in the Project #5 Handout.
20
 * The function given in the Project #5 Handout.
23
 * This is 1 + x^28
21
 * This is 1 + x^28
Line 152... Line 150...
152
 
150
 
153
int main (void)
151
int main (void)
154
{
152
{
155
    const float a = 0.0;
153
    const float a = 0.0;
156
    const float b = 3.0;
154
    const float b = 3.0;
-
 
155
    const float real = 3.0 + (pow(3.0, 29.0) / 29.0);
-
 
156
    float h, comp, abs, rel;
157
    int n = 0;
157
    int i, n;
-
 
158
 
-
 
159
    /* Euler Method Header */
-
 
160
    printf ("Euler's Method\n");
-
 
161
    printf ("n       h               Computed        Abs Error       Rel Error\n");
-
 
162
    printf ("====================================================================\n");
-
 
163
    
-
 
164
    /* Euler Method Table */
-
 
165
    for (n=1; n<=25; n++)
-
 
166
    {
-
 
167
        h = (b - a) / n;
-
 
168
        comp = eval_euler (a, b, n, &func);
-
 
169
        abs = fabs(real - comp);
-
 
170
        rel = fabs(real - comp) / fabs(real);
-
 
171
 
-
 
172
        printf ("%.2d\t%e\t%e\t%e\t%e\n", n, h, comp, abs, rel);
-
 
173
    }
-
 
174
 
-
 
175
    /* Trapezoid Method Header */
-
 
176
    printf ("\n\nTrapezoid Method\n");
-
 
177
    printf ("n       h               Computed        Abs Error       Rel Error\n");
-
 
178
    printf ("====================================================================\n");
-
 
179
    
-
 
180
    /* Trapezoid Method Table */
-
 
181
    for (n=1; n<=25; n++)
-
 
182
    {
-
 
183
        h = (b - a) / n;
-
 
184
        comp = eval_trapezoid (a, b, n, &func);
-
 
185
        abs = fabs(real - comp);
-
 
186
        rel = fabs(real - comp) / fabs(real);
-
 
187
 
-
 
188
        printf ("%.2d\t%e\t%e\t%e\t%e\n", n, h, comp, abs, rel);
-
 
189
    }
-
 
190
 
-
 
191
    /* Romberg Method Header */
-
 
192
    printf ("\n\nRomberg's Method\n");
-
 
193
    printf ("n       h               Computed        Abs Error       Rel Error\n");
-
 
194
    printf ("====================================================================\n");
-
 
195
    
-
 
196
    /* Romberg Method Table */
-
 
197
    for (n=1; n<=25; n++)
-
 
198
    {
-
 
199
        h = (b - a) / n;
-
 
200
        comp = eval_romberg (a, b, n, &func);
-
 
201
        abs = fabs(real - comp);
-
 
202
        rel = fabs(real - comp) / fabs(real);
-
 
203
 
-
 
204
        printf ("%.2d\t%e\t%e\t%e\t%e\n", n, h, comp, abs, rel);
-
 
205
    }
-
 
206
 
-
 
207
    /* Simpson Method Header */
-
 
208
    printf ("\n\nSimpson's Method\n");
-
 
209
    printf ("n       h               Computed        Abs Error       Rel Error\n");
-
 
210
    printf ("====================================================================\n");
-
 
211
    
-
 
212
    /* Simpson Method Table */
-
 
213
    for (n=1; n<=25; n++)
-
 
214
    {
-
 
215
        h = (b - a) / n;
-
 
216
        comp = eval_simpson (a, b, n, &func);
-
 
217
        abs = fabs(real - comp);
-
 
218
        rel = fabs(real - comp) / fabs(real);
-
 
219
 
-
 
220
        printf ("%.2d\t%e\t%e\t%e\t%e\n", n, h, comp, abs, rel);
-
 
221
    }
158
 
222
 
159
    return 0;
223
    return 0;
160
}
224
}
161
 
225