-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpwl_approx_1d_prb.cpp
170 lines (150 loc) · 3.3 KB
/
pwl_approx_1d_prb.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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;
# include "test_interp_1d.hpp"
# include "pwl_approx_1d.hpp"
# include "r8lib.hpp"
int main ( );
void test01 ( int prob, int nc, int nd );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for PWL_APPROX_1D_PRB.
//
// Discussion:
//
// PWL_APPROX_1D_PRB tests the PWL_APPROX_1D library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2012
//
// Author:
//
// John Burkardt
//
{
int j;
int k;
int nc;
int nc_test[4] = { 2, 4, 8, 16 };
int nc_test_num = 4;
int nd;
int nd_test[2] = { 16, 64 };
int nd_test_num = 2;
int prob;
int prob_num;
timestamp ( );
cout << "\n";
cout << "PWL_APPROX_1D_PRB:\n";
cout << " C++ version\n";
cout << " Test the PWL_APPROX_1D library.\n";
cout << " The QR_SOLVE library is needed.\n";
cout << " The R8LIB library is needed.\n";
cout << " The test also needs the TEST_INTERP_1D library.\n";
prob_num = p00_prob_num ( );
for ( prob = 1; prob <= prob_num; prob++ )
{
for ( j = 0; j < nc_test_num; j++ )
{
nc = nc_test[j];
for ( k = 0; k < nd_test_num; k++ )
{
nd = nd_test[k];
test01 ( prob, nc, nd );
}
}
}
//
// Terminate.
//
cout << "\n";
cout << "PWL_APPROX_1D_PRB:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void test01 ( int prob, int nc, int nd )
//****************************************************************************80
//
// Purpose:
//
// TEST01 tests PWL_APPROX_1D.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 October 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int PROB, the problem index.
//
// Input, int NC, the number of control points.
//
// Input, int ND, the number of data points.
//
{
double app_error;
int ni;
double *xc;
double *xd;
double *xi;
double *yc;
double *yd;
double *yi;
double xmax;
double xmin;
cout << "\n";
cout << "TEST01:\n";
cout << " Approximate data from TEST_INTERP_1D problem #" << prob << "\n";
cout << " Number of control points = " << nc << "\n";
cout << " Number of data points = " << nd << "\n";
xmin = 0.0;
xmax = 1.0;
xd = r8vec_linspace_new ( nd, xmin, xmax );
yd = p00_f ( prob, nd, xd );
if ( nd < 10 )
{
r8vec2_print ( nd, xd, yd, " Data array:" );
}
//
// Determine control values.
//
xc = r8vec_linspace_new ( nc, xmin, xmax );
yc = pwl_approx_1d ( nd, xd, yd, nc, xc );
//
// #1: Does approximant come close to function at data points?
//
ni = nd;
xi = r8vec_copy_new ( ni, xd );
yi = pwl_interp_1d ( nc, xc, yc, ni, xi );
app_error = r8vec_norm_affine ( ni, yi, yd ) / ( double ) ( ni );
cout << "\n";
cout << " L2 approximation error averaged per data node = " << app_error << "\n";
delete [] xc;
delete [] xd;
delete [] xi;
delete [] yc;
delete [] yd;
delete [] yi;
return;
}