14#include "../lin_alg/newton.h"
31 size_t tDim = tInterval.size();
32 size_t elem_size = y0.
columns();
34 auto TOL = option.
TOL;
36 auto Jac = option.
Jac;
37 if(h == 0) { h = (tInterval[tDim - 1] - tInterval[0]) / 1000.0; }
40 size_t result_dim = ((tInterval[tDim - 1] - tInterval[0]) / h) + 1;
42 size_t n = result_dim;
43 auto y =
zeros(n, elem_size);
46 t(0, 0) = tInterval[0];
47 t(1, 0) = t(0, 0) + h;
49 auto y1 =
ODETrapez(fun, { t(0, 0), t(1, 0) }, y0, option).Y;
52 auto E =
eye(elem_size);
54 for(
size_t l = 1; l < n - 1; l++) {
55 auto y_act = y(l - 1).Transpose();
57 auto delta =
ones(elem_size);
59 while(
norm(delta) > TOL && k < maxIter) {
60 auto current_fun = fun(t(l, 0) + h, y_act);
62 -1.0 * ((-1.0 / 2.0) * y(l - 1).Transpose() + h * current_fun + 2.0 * y(l).Transpose() - 3.0 / 2.0 * y_act);
63 auto J = h * Jac(t(l, 0), y_act) - (3.0 / 2.0) * E;
68 if(k >= maxIter) { std::cout <<
"Warning: Max number iterations reached." << std::endl; }
70 t(l + 1, 0) = t(l, 0) + h;
73 return { y, t, iter };
void SetRow(size_t index, const Matrix< T > &other)
Definition: Matrix.h:541
size_t columns() const
Definition: Matrix.h:198
Matrix< double > gaussSeidel(const Matrix< double > &A, const Matrix< double > &b)
Definition: gaussSeidel.h:29
Matrix< double > eye(size_t rows, size_t columns=0)
double norm(const Matrix< double > &in)
Matrix< double > ones(size_t rows, size_t columns=1, size_t elements=1)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
ODEResult ODEBDF2(const ODE &fun, const std::vector< double > &tInterval, const Matrix< double > &y0, const ODEOption &option)
Definition: odeBDF2.h:30
ODEResult ODETrapez(const ODE &fun, const std::vector< double > &tInterval, const Matrix< double > &y0, const ODEOption &option)
Definition: odeTrapez.h:28
std::function< Matrix< double >(double, Matrix< double >)> ODE
alias for ODE
Definition: ode.h:17
double TOL
tolerance for the algorithm to determine convergence
Definition: ode.h:60
int maxIter
max iterations for integrated newton steps
Definition: ode.h:62
double h
step width of t
Definition: ode.h:58
ODEJac Jac
jacobian matrix of given function
Definition: ode.h:64