Subversion Repositories programming

Rev

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

Rev Author Line No. Line
154 ira 1
/* Copyright: Ira W. Snyder
2
 * Start Date: 2005-11-13
3
 * End Date: 2005-11-13
4
 * License: Public Domain
5
 *
6
 * Changelog Follows:
7
 *
8
 * 2005-11-13
9
 * - Implemented Functions 1-3 from the Project #3 Handout.
10
 * - Implemented Algorithm 7 (LaGrange) from Notes Set #3.
11
 * - Implemented Algorithm 8-9 (Newton Divided Diff) from Notes Set #3.
155 ira 12
 * - Implemented GenEvenPts() which will generate evenly spaced points.
13
 * - Implemented GenChebychevPts() which will generate Chebychev points.
154 ira 14
 *
15
 */
16
 
17
#include <cstdio>
18
#include <cmath>
19
using namespace std;
20
 
21
#define X_MIN = -5.0
22
#define X_MAX = 5.0
23
 
155 ira 24
/**
25
 * Function #1 from the Project #3 Handout.
26
 *
27
 * @param x the place to calculate the value of this function
28
 * @return the value of the function at x
29
 */
154 ira 30
double func1 (double x)
31
{
32
    return 1.0 / (1.0 + pow(x, 2));
33
}
34
 
155 ira 35
/**
36
 * Function #2 from the Project #3 Handout.
37
 *
38
 * @param x the place to calculate the value of this function
39
 * @return the value of the function at x
40
 */
154 ira 41
double func2 (double x)
42
{
43
    // NOTE: M_E is the value of e defined by the math.h header
44
    return (pow(M_E, x) + pow(M_E, -x)) / 2.0;
45
}
46
 
155 ira 47
/**
48
 * Function #3 from the Project #3 Handout.
49
 *
50
 * @param x the place to calculate the value of this function
51
 * @return the value of the function at x
52
 */
154 ira 53
double func3 (double x)
54
{
55
    return pow(x, 14) + pow(x, 10) + pow(x, 4) + x + 1;
56
}
57
 
155 ira 58
/**
59
 * Find the value of the LaGrange Interpolating Polynomial at a point.
60
 *
61
 * @param x the x[i] values
62
 * @param y the y[i] values
63
 * @param n the number of points
64
 * @param point the point to evaluate at
65
 * @return the value of the Interpolating Poly at point
66
 */
154 ira 67
double LaGrangeMethod (double *x, double *y, int n, double point)
68
{
69
    double value = 0;
70
    double term  = 0;
71
    int i, j;
72
 
73
    for (i=0; i<n; i++)
74
    {
75
        term = y[i];
76
 
77
        for (j=0; j<n; j++)
78
            if (i != j)
79
                term *= (point - x[j])/(x[i] - x[j]);
80
 
81
        value += term;
82
    }
83
 
84
    return value;
85
}
86
 
155 ira 87
/**
88
 * Find the value of the Newton Interpolating Polynomial at a point.
89
 * The a[i]'s are found using a divided difference table.
90
 *
91
 * @param x the x[i] values
92
 * @param y the y[i] values
93
 * @param n the number of points
94
 * @param point the point to evaluate at
95
 * @return the value of the Interpolating Poly at point
96
 */
154 ira 97
double NewtonMethod (double *x, double *y, int n, double point)
98
{
99
    int i, j;
100
    double a[n];
101
 
102
    for (i=0; i<n; i++)
103
        a[i] = y[i];
104
 
105
    for (i=1; i<n; i++)
106
        for (j=n-1; j>=i; j--)
107
            a[j] = (a[j] - a[j-1]) / (x[j] - x[j-i]);
108
 
109
    // At this point, all of the a[i]'s have been calculated,
110
    // using a Divided Difference Table.
111
 
112
    double xterm = 1.0;
113
    double value = 0.0;
114
 
115
    for (i=0; i<n; i++)
116
    {
117
        value += a[i] * xterm;
118
        xterm *= (point - x[i]);
119
    }
120
 
121
    return value;
122
}
123
 
155 ira 124
/**
125
 * Generate evenly spaced points for the function given.
126
 * The algorithm is taken from the Project #3 Handout.
127
 *
128
 * @param num_pts the number of points
129
 * @param *func the function that you want to use to find y[i]
130
 * @param x[] the array that will hold the x[i] values
131
 * @param y[] the array that will hold the y[i] values
132
 */
133
void GenEvenPts (int num_pts, double(*func)(double), double x[], double y[])
134
{
135
    int i;
156 ira 136
    double h = 10.0 / (double)(num_pts-1);
155 ira 137
    double xtemp = -5.0;
138
 
139
    for (i=0; i<num_pts; i++)
140
    {
141
        x[i] = xtemp;
142
        y[i] = func(x[i]);
143
        xtemp += h;
144
    }
145
}
146
 
147
/**
148
 * Generate Chebychev points for the function given.
149
 * The algorithm is taken from the Project #3 Handout.
150
 *
151
 * @param num_pts the number of points
152
 * @param *func the function that you want to use to find y[i]
153
 * @param x[] the array that will hold the x[i] values
154
 * @param y[] the array that will hold the y[i] values
155
 */
156
void GenChebychevPts (int num_pts, double(*func)(double), double x[], double y[])
157
{
158
    int i;
159
 
160
    for (i=0; i<num_pts; i++)
161
    {
156 ira 162
        x[i] = -5.0 * cos((float)i * M_PI / (float)(num_pts-1));
155 ira 163
        y[i] = func(x[i]);
164
    }
165
}
166
 
154 ira 167
int main (void)
168
{
155 ira 169
    const int size = 11;
170
    double x[size];
171
    double y[size];
154 ira 172
    double point = 2.0;
173
 
155 ira 174
    //GenEvenPts (size, &func1, x, y);
175
    GenChebychevPts (size, &func1, x, y);
176
 
177
    for (int i=0; i<size; i++)
178
        printf("x[%d] = %e -- y[%d] = %e\n", i, x[i], i, y[i]);
179
 
154 ira 180
    printf ("LaGrange = %e\n", LaGrangeMethod(x, y, size, point));
181
    printf ("Newton = %e\n", NewtonMethod(x, y, size, point));
182
 
183
    return 0;
184
}
185