Skip to content

Instantly share code, notes, and snippets.

@sverhoeven
Last active October 7, 2020 11:57
Show Gist options
  • Save sverhoeven/b34819cb297fac2e84a3a53bf18c73d0 to your computer and use it in GitHub Desktop.
Save sverhoeven/b34819cb297fac2e84a3a53bf18c73d0 to your computer and use it in GitHub Desktop.
run-cpp-on-web: webassembly
#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;
do {
delta_x = equation(x) / derivative(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