Last active
October 7, 2020 12:09
-
-
Save sverhoeven/0af8436cb2e81f6089a7cd2f98e83533 to your computer and use it in GitHub Desktop.
run-cpp-on-web: vega
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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