Skip to content

Instantly share code, notes, and snippets.

@sverhoeven
Last active October 7, 2020 12:09
Show Gist options
  • Select an option

  • Save sverhoeven/0af8436cb2e81f6089a7cd2f98e83533 to your computer and use it in GitHub Desktop.

Select an option

Save sverhoeven/0af8436cb2e81f6089a7cd2f98e83533 to your computer and use it in GitHub Desktop.
run-cpp-on-web: vega
#include "newtonraphson.hpp"
#include "problem.hpp"
#include <cmath>
// Define the constructor method of NewtonRaphson instances
NewtonRaphson::NewtonRaphson(float tolerance_in) : tolerance(tolerance_in) {}
// Define the 'solve' method of NewtonRaphson instances
float NewtonRaphson::solve(float initial_guess) {
float x = initial_guess;
float delta_x = 0.0;
int i = 0;
do {
delta_x = equation(x) / derivative(x);
iterations.push_back({i++, x, equation(x), derivative(x), delta_x});
x = x - delta_x;
} while (std::abs(delta_x) >= tolerance);
return x;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment