Skip to content

Instantly share code, notes, and snippets.

@jpthompson23
Last active November 26, 2020 20:46
Show Gist options
  • Save jpthompson23/d63415852e2ead56fe74 to your computer and use it in GitHub Desktop.
Save jpthompson23/d63415852e2ead56fe74 to your computer and use it in GitHub Desktop.
Newton's method in C++
#include <iostream>
#include <cmath>
#include <functional>
struct NewtonSolver {
std::function<float(const float)> f;
std::function<float(const float)> fprime;
const float tol;
NewtonSolver(
float(& arg_f)(const float), float(& arg_fprime)(const float), const float arg_tol):
f(arg_f), fprime(arg_fprime), tol(arg_tol) {
}
void operator()(float& x) const {
float old_x = x;
x -= f(x)/fprime(x);
std::cout << x << std::endl;
if (std::abs(x - old_x) < std::abs(x)*tol) {
return;
}
else {
(*this)(x);
}
}
};
float f(const float x) {
return x*x + 2*x - 3;
}
float fprime(const float x) {
return 2*x + 2;
}
int main() {
const float tol = 0.001;
float x = -1000;
NewtonSolver newton(f, fprime, tol);
newton(x);
std::cout << "Final answer: " << x << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment