void invInterpol std vector double knotsX std vector double knotsY dou

 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
void invInterpol(std::vector<double> knotsX, std::vector<double> knotsY, double &x)
{
int n = knotsX.size();
std::vector<std::vector<double> > diffs;
std::vector<double> temp;
for (int i = 0; i < n-1; ++i)
{
temp.push_back((knotsY[i] - knotsY[i+1]) / (knotsX[i] - knotsX[i + 1]));
}
diffs.push_back(temp);
n = temp.size();
temp.clear();
for (int i = 1, count = 2; i < knotsX.size() - 1; ++i, count++)
{
for (int j = 0; j < n-1; j++)
{
temp.push_back((diffs[i - 1][j+1] - diffs[i - 1][j]) / (knotsX[j+count] - knotsX[j]));
}
diffs.push_back(temp);
n = temp.size();
temp.clear();
}
x = knotsX[0];
double norm;
do
{
double t = knotsY[0];
int count = 2;
for (int i = 1; i < diffs.size(); ++i)
{
double prod = 1;
for (int j = 0; j < count; ++j)
{
prod *= (x - knotsX[j]);
}
t += diffs[i][0] * prod;
count++;
}
double xNext = knotsX[0] - t / diffs[0][0];
norm = fabs(xNext - x);
x = xNext;
} while (norm > 1e-5);
}