Created
December 28, 2017 06:47
-
-
Save jianminchen/d99be5cb98101919c2c05709fe9d5aa6 to your computer and use it in GitHub Desktop.
Root of number - newton method
This file contains 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
private static double newtonMethod(int n, double target, double delta) { | |
double root = 1; | |
do { | |
double fx = fx(root, n, target); | |
double df = df(root, n); | |
root = root - fx/df; | |
} while (Math.abs(fx(root, n, target)) > delta); | |
return root; | |
} | |
private static double fx(double root, int n, double target) { | |
return Math.pow(root, n) - target; | |
} | |
private static double df(double root, int n) { | |
return n * Math.pow(root, n - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment