Subversion Repositories programming

Rev

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

Rev 157 Rev 158
Line 10... Line 10...
10
 * - Implemented Algorithm 7 (LaGrange) from Notes Set #3.
10
 * - Implemented Algorithm 7 (LaGrange) from Notes Set #3.
11
 * - Implemented Algorithm 8-9 (Newton Divided Diff) from Notes Set #3.
11
 * - Implemented Algorithm 8-9 (Newton Divided Diff) from Notes Set #3.
12
 * - Implemented GenEvenPts() which will generate evenly spaced points.
12
 * - Implemented GenEvenPts() which will generate evenly spaced points.
13
 * - Implemented GenChebychevPts() which will generate Chebychev points.
13
 * - Implemented GenChebychevPts() which will generate Chebychev points.
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
-
 
16
 *   worst values. This is taken from the Project #3 Handout.
-
 
17
 * - Added each needed call to main().
15
 *
18
 *
16
 */
19
 */
17
 
20
 
18
#include <cstdio>
21
#include <cstdio>
19
#include <cmath>
22
#include <cmath>
20
using namespace std;
23
using namespace std;
21
 
24
 
22
#define MAX_POINTS 26
25
#define MAX_POINTS 26
23
 
26
 
24
// Global Variables for easier return values
27
// Global Variables for easier return values from the brute_search functions.
25
double absmax, xsave;
28
double absmax, xsave;
26
 
29
 
27
/**
30
/**
28
 * Function #1 from the Project #3 Handout.
31
 * Function #1 from the Project #3 Handout.
29
 *
32
 *
Line 246... Line 249...
246
 
249
 
247
        xval += h;
250
        xval += h;
248
    }
251
    }
249
}
252
}
250
 
253
 
-
 
254
/**
-
 
255
 * Print a nice error line in the format we need.
-
 
256
 *
-
 
257
 * @param points the number of points
-
 
258
 * @param absmax the value of absmax (redundant, I know)
-
 
259
 * @param xsave the value of xsave (redundant)
-
 
260
 */
-
 
261
void print_error_line (int points, double absmax, double xsave)
-
 
262
{
-
 
263
    printf ("For %-2d Points, MAX ERROR is: %e"
-
 
264
            " -- OCCURS at x = %e\n", points, absmax, xsave);
-
 
265
}
-
 
266
 
251
int main (void)
267
int main (void)
252
{
268
{
253
    int size, i;
269
    int i; // number of points
254
    double x[MAX_POINTS];
270
    double x[MAX_POINTS];
255
    double y[MAX_POINTS];
271
    double y[MAX_POINTS];
256
 
272
 
257
    // In the following code, the correct functions are run for each of the
273
    // In the following code, the correct functions are run for each of the
258
    // 12 times this needs to be run.
274
    // 12 times this needs to be run.
259
    
275
    
260
    printf ("Newton Polynomial, Equal Points, Function #1\n");
276
    printf ("Newton Polynomial, Equal Points, Function #1\n");
261
    printf ("============================================================\n");
277
    printf ("============================================================\n");
262
    for (i=6; i<=26; i+=5)
278
    for (i=6; i<=26; i+=5)
263
    {
279
    {
264
        size = i;
-
 
265
        GenEvenPts (size, &func1, x, y);
280
        GenEvenPts (i, &func1, x, y);
266
        brute_search_newton (x, y, size, &func1);
281
        brute_search_newton (x, y, i, &func1);
-
 
282
        print_error_line (i, absmax, xsave);
267
 
283
 
268
        printf ("For %-2d Points, MAX ERROR is: %e"
-
 
269
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
270
    }
284
    }
271
    
285
    
272
    printf ("\n\n");
286
    printf ("\n\n");
273
    printf ("Newton Polynomial, Equal Points, Function #2\n");
287
    printf ("Newton Polynomial, Equal Points, Function #2\n");
274
    printf ("============================================================\n");
288
    printf ("============================================================\n");
275
    for (i=6; i<=26; i+=5)
289
    for (i=6; i<=26; i+=5)
276
    {
290
    {
277
        size = i;
-
 
278
        GenEvenPts (size, &func2, x, y);
291
        GenEvenPts (i, &func2, x, y);
279
        brute_search_newton (x, y, size, &func2);
292
        brute_search_newton (x, y, i, &func2);
280
 
-
 
281
        printf ("For %-2d Points, MAX ERROR is: %e"
293
        print_error_line (i, absmax, xsave);
282
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
283
    }
294
    }
284
    
295
    
285
    printf ("\n\n");
296
    printf ("\n\n");
286
    printf ("Newton Polynomial, Equal Points, Function #3\n");
297
    printf ("Newton Polynomial, Equal Points, Function #3\n");
287
    printf ("============================================================\n");
298
    printf ("============================================================\n");
288
    for (i=6; i<=26; i+=5)
299
    for (i=6; i<=26; i+=5)
289
    {
300
    {
290
        size = i;
-
 
291
        GenEvenPts (size, &func3, x, y);
301
        GenEvenPts (i, &func3, x, y);
292
        brute_search_newton (x, y, size, &func3);
302
        brute_search_newton (x, y, i, &func3);
293
 
-
 
294
        printf ("For %-2d Points, MAX ERROR is: %e"
303
        print_error_line (i, absmax, xsave);
295
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
296
    }
304
    }
297
    
305
    
298
    printf ("\n\n");
306
    printf ("\n\n");
299
    printf ("LaGrange Polynomial, Equal Points, Function #1\n");
307
    printf ("LaGrange Polynomial, Equal Points, Function #1\n");
300
    printf ("============================================================\n");
308
    printf ("============================================================\n");
301
    for (i=6; i<=26; i+=5)
309
    for (i=6; i<=26; i+=5)
302
    {
310
    {
303
        size = i;
-
 
304
        GenEvenPts (size, &func1, x, y);
311
        GenEvenPts (i, &func1, x, y);
305
        brute_search_lagrange (x, y, size, &func1);
312
        brute_search_lagrange (x, y, i, &func1);
306
 
-
 
307
        printf ("For %-2d Points, MAX ERROR is: %e"
313
        print_error_line (i, absmax, xsave);
308
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
309
    }
314
    }
310
    
315
    
311
    printf ("\n\n");
316
    printf ("\n\n");
312
    printf ("LaGrange Polynomial, Equal Points, Function #2\n");
317
    printf ("LaGrange Polynomial, Equal Points, Function #2\n");
313
    printf ("============================================================\n");
318
    printf ("============================================================\n");
314
    for (i=6; i<=26; i+=5)
319
    for (i=6; i<=26; i+=5)
315
    {
320
    {
316
        size = i;
-
 
317
        GenEvenPts (size, &func2, x, y);
321
        GenEvenPts (i, &func2, x, y);
318
        brute_search_lagrange (x, y, size, &func2);
322
        brute_search_lagrange (x, y, i, &func2);
319
 
-
 
320
        printf ("For %-2d Points, MAX ERROR is: %e"
323
        print_error_line (i, absmax, xsave);
321
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
322
    }
324
    }
323
    
325
    
324
    printf ("\n\n");
326
    printf ("\n\n");
325
    printf ("LaGrange Polynomial, Equal Points, Function #3\n");
327
    printf ("LaGrange Polynomial, Equal Points, Function #3\n");
326
    printf ("============================================================\n");
328
    printf ("============================================================\n");
327
    for (i=6; i<=26; i+=5)
329
    for (i=6; i<=26; i+=5)
328
    {
330
    {
329
        size = i;
-
 
330
        GenEvenPts (size, &func3, x, y);
331
        GenEvenPts (i, &func3, x, y);
331
        brute_search_lagrange (x, y, size, &func3);
332
        brute_search_lagrange (x, y, i, &func3);
332
 
-
 
333
        printf ("For %-2d Points, MAX ERROR is: %e"
333
        print_error_line (i, absmax, xsave);
334
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
335
    }
334
    }
336
    
335
    
337
    printf ("\n\n");
336
    printf ("\n\n");
338
    printf ("Newton Polynomial, Chebychev Points, Function #1\n");
337
    printf ("Newton Polynomial, Chebychev Points, Function #1\n");
339
    printf ("============================================================\n");
338
    printf ("============================================================\n");
340
    for (i=6; i<=26; i+=5)
339
    for (i=6; i<=26; i+=5)
341
    {
340
    {
342
        size = i;
-
 
343
        GenChebychevPts (size, &func1, x, y);
341
        GenChebychevPts (i, &func1, x, y);
344
        brute_search_newton (x, y, size, &func1);
342
        brute_search_newton (x, y, i, &func1);
345
 
-
 
346
        printf ("For %-2d Points, MAX ERROR is: %e"
343
        print_error_line (i, absmax, xsave);
347
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
348
    }
344
    }
349
    
345
    
350
    printf ("\n\n");
346
    printf ("\n\n");
351
    printf ("Newton Polynomial, Chebychev Points, Function #2\n");
347
    printf ("Newton Polynomial, Chebychev Points, Function #2\n");
352
    printf ("============================================================\n");
348
    printf ("============================================================\n");
353
    for (i=6; i<=26; i+=5)
349
    for (i=6; i<=26; i+=5)
354
    {
350
    {
355
        size = i;
-
 
356
        GenChebychevPts (size, &func2, x, y);
351
        GenChebychevPts (i, &func2, x, y);
357
        brute_search_newton (x, y, size, &func2);
352
        brute_search_newton (x, y, i, &func2);
358
 
-
 
359
        printf ("For %-2d Points, MAX ERROR is: %e"
353
        print_error_line (i, absmax, xsave);
360
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
361
    }
354
    }
362
    
355
    
363
    printf ("\n\n");
356
    printf ("\n\n");
364
    printf ("Newton Polynomial, Chebychev Points, Function #3\n");
357
    printf ("Newton Polynomial, Chebychev Points, Function #3\n");
365
    printf ("============================================================\n");
358
    printf ("============================================================\n");
366
    for (i=6; i<=26; i+=5)
359
    for (i=6; i<=26; i+=5)
367
    {
360
    {
368
        size = i;
-
 
369
        GenChebychevPts (size, &func3, x, y);
361
        GenChebychevPts (i, &func3, x, y);
370
        brute_search_newton (x, y, size, &func3);
362
        brute_search_newton (x, y, i, &func3);
371
 
-
 
372
        printf ("For %-2d Points, MAX ERROR is: %e"
363
        print_error_line (i, absmax, xsave);
373
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
374
    }
364
    }
375
    
365
    
376
    printf ("\n\n");
366
    printf ("\n\n");
377
    printf ("LaGrange Polynomial, Chebychev Points, Function #1\n");
367
    printf ("LaGrange Polynomial, Chebychev Points, Function #1\n");
378
    printf ("============================================================\n");
368
    printf ("============================================================\n");
379
    for (i=6; i<=26; i+=5)
369
    for (i=6; i<=26; i+=5)
380
    {
370
    {
381
        size = i;
-
 
382
        GenChebychevPts (size, &func1, x, y);
371
        GenChebychevPts (i, &func1, x, y);
383
        brute_search_lagrange (x, y, size, &func1);
372
        brute_search_lagrange (x, y, i, &func1);
384
 
-
 
385
        printf ("For %-2d Points, MAX ERROR is: %e"
373
        print_error_line (i, absmax, xsave);
386
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
387
    }
374
    }
388
    
375
    
389
    printf ("\n\n");
376
    printf ("\n\n");
390
    printf ("LaGrange Polynomial, Chebychev Points, Function #2\n");
377
    printf ("LaGrange Polynomial, Chebychev Points, Function #2\n");
391
    printf ("============================================================\n");
378
    printf ("============================================================\n");
392
    for (i=6; i<=26; i+=5)
379
    for (i=6; i<=26; i+=5)
393
    {
380
    {
394
        size = i;
-
 
395
        GenChebychevPts (size, &func2, x, y);
381
        GenChebychevPts (i, &func2, x, y);
396
        brute_search_lagrange (x, y, size, &func2);
382
        brute_search_lagrange (x, y, i, &func2);
397
 
-
 
398
        printf ("For %-2d Points, MAX ERROR is: %e"
383
        print_error_line (i, absmax, xsave);
399
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
400
    }
384
    }
401
    
385
    
402
    printf ("\n\n");
386
    printf ("\n\n");
403
    printf ("LaGrange Polynomial, Chebychev Points, Function #3\n");
387
    printf ("LaGrange Polynomial, Chebychev Points, Function #3\n");
404
    printf ("============================================================\n");
388
    printf ("============================================================\n");
405
    for (i=6; i<=26; i+=5)
389
    for (i=6; i<=26; i+=5)
406
    {
390
    {
407
        size = i;
-
 
408
        GenChebychevPts (size, &func3, x, y);
391
        GenChebychevPts (i, &func3, x, y);
409
        brute_search_lagrange (x, y, size, &func3);
392
        brute_search_lagrange (x, y, i, &func3);
410
 
-
 
411
        printf ("For %-2d Points, MAX ERROR is: %e"
393
        print_error_line (i, absmax, xsave);
412
                " -- OCCURS at x = %e\n", size, absmax, xsave);
-
 
413
    }
394
    }
414
    
395
    
415
    return 0;
396
    return 0;
416
}
397
}
417
 
398