Subversion Repositories programming

Rev

Rev 158 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 158 Rev 159
Line 14... Line 14...
14
 * - Split the Newton algorithm into two parts: NewtonMethod() and NewtonCoef().
14
 * - Split the Newton algorithm into two parts: NewtonMethod() and NewtonCoef().
15
 * - Implemented brute_search_lagrange() and brute_search_newton() to find the
15
 * - Implemented brute_search_lagrange() and brute_search_newton() to find the
16
 *   worst values. This is taken from the Project #3 Handout.
16
 *   worst values. This is taken from the Project #3 Handout.
17
 * - Added each needed call to main().
17
 * - Added each needed call to main().
18
 *
18
 *
-
 
19
 * 2005-11-17
-
 
20
 * - Changed stupid global variables used to return values from a function to
-
 
21
 *   a new type (called bsf_type) and use that instead. What was I thinking???
-
 
22
 *
19
 */
23
 */
20
 
24
 
21
#include <cstdio>
25
#include <cstdio>
22
#include <cmath>
26
#include <cmath>
23
using namespace std;
27
using namespace std;
24
 
28
 
25
#define MAX_POINTS 26
29
#define MAX_POINTS 26
26
 
30
 
27
// Global Variables for easier return values from the brute_search functions.
31
// A convienent brute-force-search return type
28
double absmax, xsave;
32
typedef struct { double absmax, xsave; } bsf_type;
29
 
33
 
30
/**
34
/**
31
 * Function #1 from the Project #3 Handout.
35
 * Function #1 from the Project #3 Handout.
32
 *
36
 *
33
 * @param x the place to calculate the value of this function
37
 * @param x the place to calculate the value of this function
Line 182... Line 186...
182
 
186
 
183
/**
187
/**
184
 * Brute-force search the Newton Interpolating Poly for the x and y values.
188
 * Brute-force search the Newton Interpolating Poly for the x and y values.
185
 * The error calculations will be for the given function (func1, func2, or func3).
189
 * The error calculations will be for the given function (func1, func2, or func3).
186
 *
190
 *
187
 * NOTE: The "return values" are stored in two global variables: absmax and xsave.
-
 
188
 *
-
 
189
 * @param x the x[i] values
191
 * @param x the x[i] values
190
 * @param y the y[i] values
192
 * @param y the y[i] values
191
 * @param n the number of points for this polynomial
193
 * @param n the number of points for this polynomial
192
 * @param func the function to use for the error calculation
194
 * @param func the function to use for the error calculation
-
 
195
 * @return a struct of the max abs error and the x value where it occurred
193
 */
196
 */
194
void brute_search_newton (double *x, double *y, int n, double(*func)(double))
197
bsf_type brute_search_newton (double *x, double *y, int n, double(*func)(double))
195
{
198
{
-
 
199
    bsf_type answer;
196
    absmax = 0.0;
200
    answer.absmax = 0.0;
-
 
201
    answer.xsave = 0.0;
-
 
202
 
197
    double xval = -5.0;
203
    double xval = -5.0;
198
    double h = 10.0/500.0;
204
    double h = 10.0/500.0;
199
    double temp, error;
205
    double temp, error;
200
    double a[n];
206
    double a[n];
201
    int i;
207
    int i;
Line 205... Line 211...
205
    for (i=0; i<=500; i++)
211
    for (i=0; i<=500; i++)
206
    {
212
    {
207
        temp = NewtonMethod(x, y, a, n, xval);
213
        temp = NewtonMethod(x, y, a, n, xval);
208
        error = abs(temp - func(xval));
214
        error = abs(temp - func(xval));
209
 
215
 
210
        if (error > absmax)
216
        if (error > answer.absmax)
211
        {
217
        {
212
            absmax = error;
218
            answer.absmax = error;
213
            xsave = xval;
219
            answer.xsave = xval;
214
        }
220
        }
215
 
221
 
216
        xval += h;
222
        xval += h;
217
    }
223
    }
-
 
224
 
-
 
225
    return answer;
218
}
226
}
219
 
227
 
220
/**
228
/**
221
 * Brute-force search the LaGrange Interpolating Poly for the x and y values.
229
 * Brute-force search the LaGrange Interpolating Poly for the x and y values.
222
 * The error calculations will be for the given function (func1, func2, or func3).
230
 * The error calculations will be for the given function (func1, func2, or func3).
223
 *
231
 *
224
 * NOTE: The "return values" are stored in two global variables: absmax and xsave.
-
 
225
 *
-
 
226
 * @param x the x[i] values
232
 * @param x the x[i] values
227
 * @param y the y[i] values
233
 * @param y the y[i] values
228
 * @param n the number of points for this polynomial
234
 * @param n the number of points for this polynomial
229
 * @param func the function to use for the error calculation
235
 * @param func the function to use for the error calculation
-
 
236
 * @return a struct of the max abs error and the x value where it occurred
230
 */
237
 */
231
void brute_search_lagrange (double *x, double *y, int n, double(*func)(double))
238
bsf_type brute_search_lagrange (double *x, double *y, int n, double(*func)(double))
232
{
239
{
-
 
240
    bsf_type answer;
233
    absmax = 0.0;
241
    answer.absmax = 0.0;
-
 
242
    answer.xsave = 0.0;
-
 
243
 
234
    double xval = -5.0;
244
    double xval = -5.0;
235
    double h = 10.0/500.0;
245
    double h = 10.0/500.0;
236
    double temp, error;
246
    double temp, error;
237
    int i;
247
    int i;
238
 
248
 
239
    for (i=0; i<=500; i++)
249
    for (i=0; i<=500; i++)
240
    {
250
    {
241
        temp = LaGrangeMethod(x, y, n, xval);
251
        temp = LaGrangeMethod(x, y, n, xval);
242
        error = abs(temp - func(xval));
252
        error = abs(temp - func(xval));
243
 
253
 
244
        if (error > absmax)
254
        if (error > answer.absmax)
245
        {
255
        {
246
            absmax = error;
256
            answer.absmax = error;
247
            xsave = xval;
257
            answer.xsave = xval;
248
        }
258
        }
249
 
259
 
250
        xval += h;
260
        xval += h;
251
    }
261
    }
-
 
262
 
-
 
263
    return answer;
252
}
264
}
253
 
265
 
254
/**
266
/**
255
 * Print a nice error line in the format we need.
267
 * Print a nice error line in the format we need.
256
 *
268
 *
257
 * @param points the number of points
269
 * @param points the number of points
258
 * @param absmax the value of absmax (redundant, I know)
270
 * @param error a struct of the max abs error and the x value where it occurred
259
 * @param xsave the value of xsave (redundant)
-
 
260
 */
271
 */
261
void print_error_line (int points, double absmax, double xsave)
272
void print_error_line (int points, bsf_type error)
262
{
273
{
263
    printf ("For %-2d Points, MAX ERROR is: %e"
274
    printf ("For %-2d Points, MAX ERROR is: %e"
264
            " -- OCCURS at x = %e\n", points, absmax, xsave);
275
            " -- OCCURS at x = %e\n", points, error.absmax, error.xsave);
265
}
276
}
266
 
277
 
267
int main (void)
278
int main (void)
268
{
279
{
269
    int i; // number of points
280
    int i; // number of points
270
    double x[MAX_POINTS];
281
    double x[MAX_POINTS];
271
    double y[MAX_POINTS];
282
    double y[MAX_POINTS];
-
 
283
    bsf_type error;
272
 
284
 
273
    // In the following code, the correct functions are run for each of the
285
    // In the following code, the correct functions are run for each of the
274
    // 12 times this needs to be run.
286
    // 12 times this needs to be run.
275
    
287
    
276
    printf ("Newton Polynomial, Equal Points, Function #1\n");
288
    printf ("Newton Polynomial, Equal Points, Function #1\n");
277
    printf ("============================================================\n");
289
    printf ("============================================================\n");
278
    for (i=6; i<=26; i+=5)
290
    for (i=6; i<=26; i+=5)
279
    {
291
    {
280
        GenEvenPts (i, &func1, x, y);
292
        GenEvenPts (i, &func1, x, y);
281
        brute_search_newton (x, y, i, &func1);
293
        error = brute_search_newton (x, y, i, &func1);
282
        print_error_line (i, absmax, xsave);
294
        print_error_line (i, error);
283
 
295
 
284
    }
296
    }
285
    
297
    
286
    printf ("\n\n");
298
    printf ("\n\n");
287
    printf ("Newton Polynomial, Equal Points, Function #2\n");
299
    printf ("Newton Polynomial, Equal Points, Function #2\n");
288
    printf ("============================================================\n");
300
    printf ("============================================================\n");
289
    for (i=6; i<=26; i+=5)
301
    for (i=6; i<=26; i+=5)
290
    {
302
    {
291
        GenEvenPts (i, &func2, x, y);
303
        GenEvenPts (i, &func2, x, y);
292
        brute_search_newton (x, y, i, &func2);
304
        error = brute_search_newton (x, y, i, &func2);
293
        print_error_line (i, absmax, xsave);
305
        print_error_line (i, error);
294
    }
306
    }
295
    
307
    
296
    printf ("\n\n");
308
    printf ("\n\n");
297
    printf ("Newton Polynomial, Equal Points, Function #3\n");
309
    printf ("Newton Polynomial, Equal Points, Function #3\n");
298
    printf ("============================================================\n");
310
    printf ("============================================================\n");
299
    for (i=6; i<=26; i+=5)
311
    for (i=6; i<=26; i+=5)
300
    {
312
    {
301
        GenEvenPts (i, &func3, x, y);
313
        GenEvenPts (i, &func3, x, y);
302
        brute_search_newton (x, y, i, &func3);
314
        error = brute_search_newton (x, y, i, &func3);
303
        print_error_line (i, absmax, xsave);
315
        print_error_line (i, error);
304
    }
316
    }
305
    
317
    
306
    printf ("\n\n");
318
    printf ("\n\n");
307
    printf ("LaGrange Polynomial, Equal Points, Function #1\n");
319
    printf ("LaGrange Polynomial, Equal Points, Function #1\n");
308
    printf ("============================================================\n");
320
    printf ("============================================================\n");
309
    for (i=6; i<=26; i+=5)
321
    for (i=6; i<=26; i+=5)
310
    {
322
    {
311
        GenEvenPts (i, &func1, x, y);
323
        GenEvenPts (i, &func1, x, y);
312
        brute_search_lagrange (x, y, i, &func1);
324
        error = brute_search_lagrange (x, y, i, &func1);
313
        print_error_line (i, absmax, xsave);
325
        print_error_line (i, error);
314
    }
326
    }
315
    
327
    
316
    printf ("\n\n");
328
    printf ("\n\n");
317
    printf ("LaGrange Polynomial, Equal Points, Function #2\n");
329
    printf ("LaGrange Polynomial, Equal Points, Function #2\n");
318
    printf ("============================================================\n");
330
    printf ("============================================================\n");
319
    for (i=6; i<=26; i+=5)
331
    for (i=6; i<=26; i+=5)
320
    {
332
    {
321
        GenEvenPts (i, &func2, x, y);
333
        GenEvenPts (i, &func2, x, y);
322
        brute_search_lagrange (x, y, i, &func2);
334
        error = brute_search_lagrange (x, y, i, &func2);
323
        print_error_line (i, absmax, xsave);
335
        print_error_line (i, error);
324
    }
336
    }
325
    
337
    
326
    printf ("\n\n");
338
    printf ("\n\n");
327
    printf ("LaGrange Polynomial, Equal Points, Function #3\n");
339
    printf ("LaGrange Polynomial, Equal Points, Function #3\n");
328
    printf ("============================================================\n");
340
    printf ("============================================================\n");
329
    for (i=6; i<=26; i+=5)
341
    for (i=6; i<=26; i+=5)
330
    {
342
    {
331
        GenEvenPts (i, &func3, x, y);
343
        GenEvenPts (i, &func3, x, y);
332
        brute_search_lagrange (x, y, i, &func3);
344
        error = brute_search_lagrange (x, y, i, &func3);
333
        print_error_line (i, absmax, xsave);
345
        print_error_line (i, error);
334
    }
346
    }
335
    
347
    
336
    printf ("\n\n");
348
    printf ("\n\n");
337
    printf ("Newton Polynomial, Chebychev Points, Function #1\n");
349
    printf ("Newton Polynomial, Chebychev Points, Function #1\n");
338
    printf ("============================================================\n");
350
    printf ("============================================================\n");
339
    for (i=6; i<=26; i+=5)
351
    for (i=6; i<=26; i+=5)
340
    {
352
    {
341
        GenChebychevPts (i, &func1, x, y);
353
        GenChebychevPts (i, &func1, x, y);
342
        brute_search_newton (x, y, i, &func1);
354
        error = brute_search_newton (x, y, i, &func1);
343
        print_error_line (i, absmax, xsave);
355
        print_error_line (i, error);
344
    }
356
    }
345
    
357
    
346
    printf ("\n\n");
358
    printf ("\n\n");
347
    printf ("Newton Polynomial, Chebychev Points, Function #2\n");
359
    printf ("Newton Polynomial, Chebychev Points, Function #2\n");
348
    printf ("============================================================\n");
360
    printf ("============================================================\n");
349
    for (i=6; i<=26; i+=5)
361
    for (i=6; i<=26; i+=5)
350
    {
362
    {
351
        GenChebychevPts (i, &func2, x, y);
363
        GenChebychevPts (i, &func2, x, y);
352
        brute_search_newton (x, y, i, &func2);
364
        error = brute_search_newton (x, y, i, &func2);
353
        print_error_line (i, absmax, xsave);
365
        print_error_line (i, error);
354
    }
366
    }
355
    
367
    
356
    printf ("\n\n");
368
    printf ("\n\n");
357
    printf ("Newton Polynomial, Chebychev Points, Function #3\n");
369
    printf ("Newton Polynomial, Chebychev Points, Function #3\n");
358
    printf ("============================================================\n");
370
    printf ("============================================================\n");
359
    for (i=6; i<=26; i+=5)
371
    for (i=6; i<=26; i+=5)
360
    {
372
    {
361
        GenChebychevPts (i, &func3, x, y);
373
        GenChebychevPts (i, &func3, x, y);
362
        brute_search_newton (x, y, i, &func3);
374
        error = brute_search_newton (x, y, i, &func3);
363
        print_error_line (i, absmax, xsave);
375
        print_error_line (i, error);
364
    }
376
    }
365
    
377
    
366
    printf ("\n\n");
378
    printf ("\n\n");
367
    printf ("LaGrange Polynomial, Chebychev Points, Function #1\n");
379
    printf ("LaGrange Polynomial, Chebychev Points, Function #1\n");
368
    printf ("============================================================\n");
380
    printf ("============================================================\n");
369
    for (i=6; i<=26; i+=5)
381
    for (i=6; i<=26; i+=5)
370
    {
382
    {
371
        GenChebychevPts (i, &func1, x, y);
383
        GenChebychevPts (i, &func1, x, y);
372
        brute_search_lagrange (x, y, i, &func1);
384
        error = brute_search_lagrange (x, y, i, &func1);
373
        print_error_line (i, absmax, xsave);
385
        print_error_line (i, error);
374
    }
386
    }
375
    
387
    
376
    printf ("\n\n");
388
    printf ("\n\n");
377
    printf ("LaGrange Polynomial, Chebychev Points, Function #2\n");
389
    printf ("LaGrange Polynomial, Chebychev Points, Function #2\n");
378
    printf ("============================================================\n");
390
    printf ("============================================================\n");
379
    for (i=6; i<=26; i+=5)
391
    for (i=6; i<=26; i+=5)
380
    {
392
    {
381
        GenChebychevPts (i, &func2, x, y);
393
        GenChebychevPts (i, &func2, x, y);
382
        brute_search_lagrange (x, y, i, &func2);
394
        error = brute_search_lagrange (x, y, i, &func2);
383
        print_error_line (i, absmax, xsave);
395
        print_error_line (i, error);
384
    }
396
    }
385
    
397
    
386
    printf ("\n\n");
398
    printf ("\n\n");
387
    printf ("LaGrange Polynomial, Chebychev Points, Function #3\n");
399
    printf ("LaGrange Polynomial, Chebychev Points, Function #3\n");
388
    printf ("============================================================\n");
400
    printf ("============================================================\n");
389
    for (i=6; i<=26; i+=5)
401
    for (i=6; i<=26; i+=5)
390
    {
402
    {
391
        GenChebychevPts (i, &func3, x, y);
403
        GenChebychevPts (i, &func3, x, y);
392
        brute_search_lagrange (x, y, i, &func3);
404
        error = brute_search_lagrange (x, y, i, &func3);
393
        print_error_line (i, absmax, xsave);
405
        print_error_line (i, error);
394
    }
406
    }
395
    
407
    
396
    return 0;
408
    return 0;
397
}
409
}
398
 
410