-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFittingWithOrthoPoly.cpp
254 lines (218 loc) · 6.53 KB
/
FittingWithOrthoPoly.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2020 Andrey Kudryavtsev ([email protected])
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose and without fee is hereby granted, provided
// that the above copyright notice appears in all copies and that both the
// copyright notice and this permission notice appear in supporting
// documentation, and that the same name not be used in advertising or
// publicity pertaining to distribution of the software without specific,
// written prior permission.
// We make no representations about the suitability this software for any
// purpose. It is provided "as is" without express or implied warranty.
#include "OrthoPoly.h"
int main()
{
printf("Test 1 : horizontal curve, Legendre poly (0.0,0.0) and Gauss integration\n");
{
#define T float
#define DEGREE 4
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
T xmin = T(-10.0);
T xmax = T(+25.0);
int numpoints = 20;
T dx = (xmax - xmin) / T(numpoints - 1);
for (int i = 0; i < numpoints; i++)
{
T xx = xmin + dx * T(i);
x.push_back(xx);
y.push_back(11.5);
}
// fit
#if 1
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
assert(acc < 0.00001);
}
printf("Test 2 : horizontal curve, Legendre poly (0.0,0.0) and trapezoid integration, few points\n");
{
#define T float
#define DEGREE 4
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
T xmin = T(-10.0);
T xmax = T(+25.0);
int numpoints = 20;
T dx = (xmax - xmin) / T(numpoints - 1);
for (int i = 0; i < numpoints; i++)
{
T xx = xmin + dx * T(i);
x.push_back(xx);
y.push_back(11.5);
}
// fit
#if 0
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
// integration not accurate, too few points
assert(acc < 0.6);
}
printf("Test 3 : horizontal curve, Legendre poly (0.0,0.0) and trapezoid integration, many points\n");
{
#define T float
#define DEGREE 4
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
T xmin = T(-10.0);
T xmax = T(+25.0);
int numpoints = 2000;
T dx = (xmax - xmin) / T(numpoints - 1);
for (int i = 0; i < numpoints; i++)
{
T xx = xmin + dx * T(i);
x.push_back(xx);
y.push_back(11.5);
}
// fit
#if 0
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
// integration not accurate, too few points
assert(acc < 0.0005);
}
printf("Test 4 : half of circle, infinite derivative at ends, Legendre poly (0.0,0.0) and Gauss integration\n");
{
#define T float
#define DEGREE 4
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
// cos from 0 to Pi
T amin = T(0.0);
T amax = T(PI10);
int numpoints = 20;
T da = (amax - amin) / T(numpoints - 1);
T a = amin;
T R = 2.1;
T offset = 0.3;
for (int i = 0; i < numpoints; i++)
{
a = amin + da * T(i);
x.push_back(-R * cos(a) + offset);
y.push_back(R * sin(a) + offset);
}
// fit
#if 1
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
assert(acc < 0.5);
}
printf("Test 5 : half of circle, infinite derivative at ends, use Jacoby poly (0.5,0.5) and Gauss integration\n");
{
#define T float
#define DEGREE 4
JacobiPoly<T> f(0.5,0.5);
std::vector<T> x,y;
// cos from 0 to Pi
T amin = T(0.0);
T amax = T(PI10);
int numpoints = 20;
T da = (amax - amin) / T(numpoints - 1);
T a = amin;
T R = 2.1;
T offset = 0.3;
for (int i = 0; i < numpoints; i++)
{
a = amin + da * T(i);
x.push_back(-R * cos(a) + offset);
y.push_back(R * sin(a) + offset);
}
// fit
#if 1
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
assert(acc < 0.007);
}
printf("Test 6 : fitting cos curve from 0 to 4 Pi. Legendre poly. Trapezoidal integration on many points\n");
{
#define T float
#define DEGREE 16
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
// cos from 0 to Pi
T amin = T(0.0);
T amax = T(PI10) * T(4.0);
int numpoints = 200;
T da = (amax - amin) / T(numpoints - 1);
T a = amin;
T R = 1.0;
T offset = 0.0;
for (int i = 0; i < numpoints; i++)
{
a = amin + da * T(i);
x.push_back(a + offset);
y.push_back(R * sin(a) + offset);
}
// fit
#if 0
bool res = f.fit(DEGREE,x,y,GAUSSINT_8);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
assert(acc < 0.06);
}
printf("Test 7 : fitting cos curve from 0 to 4 Pi. Legendre poly. Gauss integration on many points\n");
{
#define T float
#define DEGREE 16
JacobiPoly<T> f(0.0,0.0);
std::vector<T> x,y;
// cos from 0 to Pi
T amin = T(0.0);
T amax = T(PI10) * T(4.0);
int numpoints = 200;
T da = (amax - amin) / T(numpoints - 1);
T a = amin;
T R = 1.0;
T offset = 0.0;
for (int i = 0; i < numpoints; i++)
{
a = amin + da * T(i);
x.push_back(a + offset);
y.push_back(R * sin(a) + offset);
}
// fit
#if 1
bool res = f.fit(DEGREE,x,y,GAUSSINT_20);
#else
bool res = f.fit(DEGREE,x,y,OTHER_INTEGRATION);
#endif
T acc = f.accuracy(x,y);
printf("Accuracy %f\n",acc);
assert(acc < 0.06);
}
printf("\n");
printf("Press [ENTER]\n");
getchar();
}