-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
40 lines (36 loc) · 1007 Bytes
/
main.cc
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
#include <iostream>
#include "gmres.h"
#include "newton.h"
#include "rhs_fn.h"
#include "jacobian_fn.h"
class F : public RHS_Fn {
public:
F(){}
~F(){}
Eigen::VectorXd operator()(const Eigen::VectorXd& x) const {
return x.array().square() - 1;
}
};
class DF : public Jacobian_Fn {
public:
DF(){}
~DF(){}
Eigen::MatrixXd operator()(const Eigen::VectorXd& x) const {
return Eigen::MatrixXd((2*x).asDiagonal());
}
};
#include <Eigen/Dense>
int main(int argc, char* argv[]) {
const int n = 10;
Eigen::MatrixXd A = Eigen::MatrixXd::Random(10, 10);
Eigen::VectorXd b = Eigen::VectorXd::Random(10);
Eigen::VectorXd x0 = Eigen::VectorXd::Ones(10);
Newton newton(1e-8, 1e-9, 100);
GMRES gmres(1e-8, n);
Eigen::VectorXd x_sln = gmres.solve_linear_system(A, b, x0);
std::cout << "||Ax -b|| = " << (A*x_sln - b).norm() << std::endl;
const double dx = 0.01;
F f;
x_sln = newton.find_zero(f, b, dx, gmres);
std::cout << "||F(x*)|| = " << f(x_sln).norm() << std::endl;
}