Subversion Repositories programming

Rev

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

Rev 154 Rev 155
Line 7... Line 7...
7
 *
7
 *
8
 * 2005-11-13
8
 * 2005-11-13
9
 * - Implemented Functions 1-3 from the Project #3 Handout.
9
 * - Implemented Functions 1-3 from the Project #3 Handout.
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.
-
 
13
 * - Implemented GenChebychevPts() which will generate Chebychev points.
12
 *
14
 *
13
 */
15
 */
14
 
16
 
15
#include <cstdio>
17
#include <cstdio>
16
#include <cmath>
18
#include <cmath>
17
using namespace std;
19
using namespace std;
18
 
20
 
19
#define X_MIN = -5.0
21
#define X_MIN = -5.0
20
#define X_MAX = 5.0
22
#define X_MAX = 5.0
21
 
23
 
-
 
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
 */
22
double func1 (double x)
30
double func1 (double x)
23
{
31
{
24
    return 1.0 / (1.0 + pow(x, 2));
32
    return 1.0 / (1.0 + pow(x, 2));
25
}
33
}
26
 
34
 
-
 
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
 */
27
double func2 (double x)
41
double func2 (double x)
28
{
42
{
29
    // NOTE: M_E is the value of e defined by the math.h header
43
    // NOTE: M_E is the value of e defined by the math.h header
30
    return (pow(M_E, x) + pow(M_E, -x)) / 2.0;
44
    return (pow(M_E, x) + pow(M_E, -x)) / 2.0;
31
}
45
}
32
 
46
 
-
 
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
 */
33
double func3 (double x)
53
double func3 (double x)
34
{
54
{
35
    return pow(x, 14) + pow(x, 10) + pow(x, 4) + x + 1;
55
    return pow(x, 14) + pow(x, 10) + pow(x, 4) + x + 1;
36
}
56
}
37
 
57
 
-
 
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
 */
38
double LaGrangeMethod (double *x, double *y, int n, double point)
67
double LaGrangeMethod (double *x, double *y, int n, double point)
39
{
68
{
40
    double value = 0;
69
    double value = 0;
41
    double term  = 0;
70
    double term  = 0;
42
    int i, j;
71
    int i, j;
Line 44... Line 73...
44
    for (i=0; i<n; i++)
73
    for (i=0; i<n; i++)
45
    {
74
    {
46
        term = y[i];
75
        term = y[i];
47
 
76
 
48
        for (j=0; j<n; j++)
77
        for (j=0; j<n; j++)
49
        {
-
 
50
            if (i != j)
78
            if (i != j)
51
                term *= (point - x[j])/(x[i] - x[j]);
79
                term *= (point - x[j])/(x[i] - x[j]);
52
        }
-
 
53
 
80
 
54
        value += term;
81
        value += term;
55
    }
82
    }
56
 
83
 
57
    return value;
84
    return value;
58
}
85
}
59
 
86
 
-
 
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
 */
60
double NewtonMethod (double *x, double *y, int n, double point)
97
double NewtonMethod (double *x, double *y, int n, double point)
61
{
98
{
62
    int i, j;
99
    int i, j;
63
    double a[n];
100
    double a[n];
64
 
101
 
Line 82... Line 119...
82
    }
119
    }
83
 
120
 
84
    return value;
121
    return value;
85
}
122
}
86
 
123
 
-
 
124
/**
-
 
125
 * Generate evenly spaced points for the function given.
-
 
126
 * The algorithm is taken from the Project #3 Handout.
-
 
127
 *
87
int main (void)
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[])
88
{
134
{
-
 
135
    int i;
89
    double x[] = {-2, -1, 0, 2, 3};
136
    double h = 10.0 / (double)num_pts;
90
    double y[] = {15, -3, -5, 15, 85};
137
    double xtemp = -5.0;
91
 
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
    {
-
 
162
        x[i] = -5.0 * cos((float)i * M_PI / (float)num_pts);
-
 
163
        y[i] = func(x[i]);
-
 
164
    }
-
 
165
}
-
 
166
 
-
 
167
int main (void)
-
 
168
{
92
    int size = 5;
169
    const int size = 11;
-
 
170
    double x[size];
-
 
171
    double y[size];
93
    double point = 2.0;
172
    double point = 2.0;
94
 
173
 
-
 
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
 
95
    printf ("LaGrange = %e\n", LaGrangeMethod(x, y, size, point));
180
    printf ("LaGrange = %e\n", LaGrangeMethod(x, y, size, point));
96
    printf ("Newton = %e\n", NewtonMethod(x, y, size, point));
181
    printf ("Newton = %e\n", NewtonMethod(x, y, size, point));
97
 
182
 
98
    return 0;
183
    return 0;
99
}
184
}