void newton std vector double std vector std vector double iters std v

 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
void newton(std::vector<double> &x, std::vector<std::vector<double> > &iters)
{
std::vector<double> xNext;
std::vector<std::vector<double> > J(3, std::vector<double>(3, 0));
double(*P[3][3])(double, double, double) = { { f11, f12, f13 }, { f21, f22, f23 }, {f31, f32, f33} };
double norm = 0;
do
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
J[i][j] = P[i][j](x[0], x[1], x[2]);
}
}
std::vector<std::vector<double> > invJ;
inverseMatrix(J, invJ);
std::vector < std::vector<double>> F(3, std::vector<double>(1, 0));
F[0][0] = f1(x[0], x[1], x[2]);
F[1][0] = f2(x[0], x[1], x[2]);
F[2][0] = f3(x[0], x[1], x[2]);
std::vector < std::vector<double>> temp(3, std::vector<double>(1, 0));
multiply(invJ, F, temp);
xNext = { temp[0][0], temp[1][0], temp[2][0] };
for (int i = 0; i < 3; ++i)
{
xNext[i] = x[i] - xNext[i];
}
norm = 0;
for (int i = 0; i < 3; ++i)
{
norm = std::max(fabs(xNext[i] - x[i]), norm);
}
x = xNext;
iters.push_back(x);
} while (norm > eps);
}