cosmotool/src/newton.hpp

31 lines
507 B
C++
Raw Normal View History

2011-02-09 20:31:45 +01:00
#ifndef _COSMOTOOL_NEWTON_HPP
#define _COSMOTOOL_NEWTON_HPP
2011-02-11 04:07:24 +01:00
#include <cmath>
2011-02-09 20:31:45 +01:00
namespace CosmoTool
{
template<typename T, typename FunT>
T newtonSolver(T x0, FunT function, double residual = 1e-3)
{
T x, xold = x0;
T f_x = function.eval(x0);
T df_x = function.derivative(x0);
x = xold - f_x/df_x;
2011-02-11 04:07:24 +01:00
while (std::abs(xold-x) > residual)
2011-02-09 20:31:45 +01:00
{
xold = x;
f_x = function.eval(x);
df_x = function.derivative(x);
x = xold - f_x/df_x;
}
return x;
}
};
#endif