-
Notifications
You must be signed in to change notification settings - Fork 0
/
lagrangeInterpolator.cpp
177 lines (139 loc) · 5.26 KB
/
lagrangeInterpolator.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
#include <Eigen/Core>
#include "lagrangeInterpolator.h"
LagrangeInterpolator::LagrangeInterpolator(
Eigen::Matrix< double, Eigen::Dynamic, 2 > pointsToBeInterpolated ):
pointsToBeInterpolated_( pointsToBeInterpolated ) {}
Eigen::Matrix< double, Eigen::Dynamic, 1> LagrangeInterpolator::computeCoefficientsOfInterpolatingPolynomial( )
{
/* This function creates a Lagrange polynomial that interpolates the vector
of input data. It builds a polynomial of order n, in which n is the
number of data points in the input vector. The output of the function is
the vector containing the coefficients of the Lagrange polynomial in
decreasing order of x.
Input:
PointsToBeInterpolated n x 2 matrix filled with x and y
coordinates of the points the Lagrange
polynomial must go through. The order of
the polynomial depends on n.
Currently, n can be either 3 or 4.
Output:
lagrangeCoefficient n x 1 vector filled with the coefficients of
the Lagrange polynomial. The first
coefficient corresponds to the highest order
term of the Lagrange polynomial.
Changelog
Date Name Comments
29/09/2016 M. Van den Broeck Creation.
09/12/2016 M. Van den Broeck Added third order Lagrangian interpolation
Michael Van den Broeck ----- 25/08/2016
*/
/// Check input
// Currently, only 3 points are allowed
Eigen::Vector2i allowedSize_order2;
allowedSize_order2 << 3, 2;
Eigen::Vector2i allowedSize_order3;
allowedSize_order3 << 4, 2;
if( (pointsToBeInterpolated_.rows() == allowedSize_order2( 0 ))
&& (pointsToBeInterpolated_.cols() == allowedSize_order2( 1 )) )
{
// Define x and y coordinates
Eigen::Vector3d x,y;
x = pointsToBeInterpolated_.col(0);
y = pointsToBeInterpolated_.col(1);
// Simplify notation so that it matches mathematical description
double x0, x1, x2;
x0 = x(0);
x1 = x(1);
x2 = x(2);
double y0, y1, y2;
y0 = y(0);
y1 = y(1);
y2 = y(2);
// Calculation terms t
double t01, t02, t11, t12, t21, t22;
t01 = - (x1 + x2);
t02 = x1*x2;
t11 = - (x0 + x2);
t12 = x0*x2;
t21 = - (x0 + x1);
t22 = x0*x1;
// Calculate denominators
double d0, d1, d2;
d0 = (x0-x1) * (x0-x2);
d1 = (x1-x0) * (x1-x2);
d2 = (x2-x0) * (x2-x1);
// Finally calculate L(x)
double T1, T2, T3;
T1 = y0/d0 + y1/d1 + y2/d2;
T2 = y0*t01/d0 + y1*t11/d1 + y2*t21/d2;
T3 = y0*t02/d0 + y1*t12/d1 + y2*t22/d2;
// Give output
Eigen::Matrix< double, 3, 1> lagrangeCoefficients;
lagrangeCoefficients << T1,
T2,
T3;
return lagrangeCoefficients;
}
else if( (pointsToBeInterpolated_.rows() == allowedSize_order3( 0 ))
&& (pointsToBeInterpolated_.cols() == allowedSize_order3( 1 )) )
{
// Define x and y coordinates
Eigen::Vector4d x,y;
x = pointsToBeInterpolated_.col(0);
y = pointsToBeInterpolated_.col(1);
// Simplify notation so that it matches mathematical description
double x0, x1, x2, x3;
x0 = x(0);
x1 = x(1);
x2 = x(2);
x3 = x(3);
double y0, y1, y2, y3;
y0 = y(0);
y1 = y(1);
y2 = y(2);
y3 = y(3);
// Calculation terms t
double t01, t02, t03, t11, t12, t13, t21, t22, t23, t31, t32, t33;
t01 = - (x1 + x2 + x3);
t02 = x1*x2 + x1*x3 + x2*x3;
t03 = - (x1*x2*x3);
t11 = - (x0 + x2 + x3);
t12 = x0*x2 + x0*x3 + x2*x3;
t13 = - (x0*x2*x3);
t21 = - (x0 + x1 + x3);
t22 = x0*x1 + x0*x3 + x1*x3;
t23 = - (x0*x1*x3);
t31 = - (x0 + x1 + x2);
t32 = x0*x1 + x0*x2 + x1*x2;
t33 = - (x0*x1*x2);
// Calculate denominators
double d0, d1, d2, d3;
d0 = (x0-x1) * (x0-x2) * (x0-x3);
d1 = (x1-x0) * (x1-x2) * (x1-x3);
d2 = (x2-x0) * (x2-x1) * (x2-x3);
d3 = (x3-x0) * (x3-x1) * (x3-x2);
// Finally calculate L(x)
double T1, T2, T3, T4;
T1 = y0/d0 + y1/d1 + y2/d2 + y3/d3;
T2 = y0*t01/d0 + y1*t11/d1 + y2*t21/d2 + y3*t31/d3;
T3 = y0*t02/d0 + y1*t12/d1 + y2*t22/d2 + y3*t32/d3;
T4 = y0*t03/d0 + y1*t13/d1 + y2*t23/d2 + y3*t33/d3;
// Give output
Eigen::Matrix< double, 4, 1> lagrangeCoefficients;
lagrangeCoefficients << T1,
T2,
T3,
T4;
return lagrangeCoefficients;
}
else
{
std::stringstream errorMessage;
errorMessage << "Incorrect input size. Size of input should either be [3,2] or [4,2]." << std::endl;
// Throw exception.
boost::throw_exception( std::runtime_error( errorMessage.str( ) ) );
}
}
LagrangeInterpolator::~LagrangeInterpolator()
{
}