-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlagrange_approx_1d.cpp
201 lines (187 loc) · 4.53 KB
/
lagrange_approx_1d.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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;
# include "lagrange_approx_1d.hpp"
# include "qr_solve.hpp"
# include "r8lib.hpp"
//****************************************************************************80
double *lagrange_approx_1d ( int m, int nd, double xd[], double yd[],
int ni, double xi[] )
//****************************************************************************80
//
// Purpose:
//
// LAGRANGE_APPROX_1D evaluates the Lagrange approximant of degree M.
//
// Discussion:
//
// The Lagrange approximant L(M,ND,XD,YD)(X) is a polynomial of
// degree M which approximates the data (XD(I),YD(I)) for I = 1 to ND.
//
// We can represent any polynomial of degree M+1 as the sum of the Lagrange
// basis functions at the M+1 Chebyshev points.
//
// L(M)(X) = sum ( 1 <= I <= M+1 ) C(I) LB(M,XC)(X)
//
// Given our data, we can seek the M+1 unknown coefficients C which minimize
// the norm of || L(M)(XD(1:ND)) - YD(1:ND) ||.
//
// Given the coefficients, we can then evaluate the polynomial at the
// points XI.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the polynomial degree.
//
// Input, int ND, the number of data points.
// ND must be at least 1.
//
// Input, double XD[ND], the data points.
//
// Input, double YD[ND], the data values.
//
// Input, int NI, the number of interpolation points.
//
// Input, double XI[NI], the interpolation points.
//
// Output, double LAGRANGE_APPROX_1D[NI], the interpolated values.
//
{
double a;
double b;
double *ld;
double *li;
int nc;
double *xc;
double *yc;
double *yi;
nc = m + 1;
//
// Evaluate the Chebyshev points.
//
a = -1.0;
b = +1.0;
xc = r8vec_cheby_extreme_new ( nc, a, b );
//
// Evaluate the Lagrange basis functions for the Chebyshev points
// at the data points.
//
ld = lagrange_basis_1d ( nc, xc, nd, xd );
//
// The value of the Lagrange approximant at each data point should
// approximate the data value: LD * YC = YD, where YC are the unknown
// coefficients.
//
yc = qr_solve ( nd, nc, ld, yd );
//
// Now we want to evaluate the Lagrange approximant at the "interpolant
// points": LI * YC = YI
//
li = lagrange_basis_1d ( nc, xc, ni, xi );
yi = r8mat_mv_new ( ni, nc, li, yc );
delete [] ld;
delete [] li;
delete [] xc;
delete [] yc;
return yi;
}
//****************************************************************************80
double *lagrange_basis_1d ( int nd, double xd[], int ni, double xi[] )
//****************************************************************************80
//
// Purpose:
//
// LAGRANGE_BASIS_1D evaluates the Lagrange basis polynomials.
//
// Discussion:
//
// Given ND distinct abscissas, XD(1:ND),
// the I-th Lagrange basis polynomial LB(I)(T) is defined as the polynomial of
// degree ND - 1 which is 1 at XD(I) and 0 at the ND - 1
// other abscissas.
//
// A formal representation is:
//
// LB(I)(T) = Product ( 1 <= J <= ND, I /= J )
// ( T - T(J) ) / ( T(I) - T(J) )
//
// This routine accepts a set of NI values at which all the Lagrange
// basis polynomials should be evaluated.
//
// Given data values YD at each of the abscissas, the value of the
// Lagrange interpolating polynomial at each of the interpolation points
// is then simple to compute by matrix multiplication:
//
// YI(1:NI) = LB(1:NI,1:ND) * YD(1:ND)
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int ND, the number of data points.
// ND must be at least 1.
//
// Input, double XD[ND], the data points.
//
// Input, int NI, the number of interpolation points.
//
// Input, double XI[NI], the interpolation points.
//
// Output, double LAGRANGE_BASIS[NI*ND], the values
// of the Lagrange basis polynomials at the interpolation points.
//
{
int i;
int j;
int k;
double *lb;
//
// Evaluate the polynomial.
//
lb = new double[ni*nd];
for ( j = 0; j < nd; j++ )
{
for ( i = 0; i < ni; i++ )
{
lb[i+j*ni] = 1.0;
}
}
for ( i = 0; i < nd; i++ )
{
for ( j = 0; j < nd; j++ )
{
if ( j != i )
{
for ( k = 0; k < ni; k++ )
{
lb[k+i*ni] = lb[k+i*ni] * ( xi[k] - xd[j] ) / ( xd[i] - xd[j] );
}
}
}
}
return lb;
}