Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class Newton {
- public static double eps, xo;
- public static final int MAXIT = 200;
- public static int counter;
- public Newton() {
- }
- public static void main(String[] args) {
- eps = Double.parseDouble(JOptionPane.showInputDialog("enter eps please:"));
- xo = Double.parseDouble(JOptionPane.showInputDialog("enter first try please:"));
- counter = 0;
- System.out.println(newton(xo));
- System.out.print("found in " + counter + " eterations");
- }
- public static double newton(double x) {
- double newX = x - (f(x) / df(x));
- if (Math.abs(newX - x)< eps) {
- return newX;
- }
- counter++;
- if (counter == MAXIT) {
- System.out.print("function has no roots");
- return 0.0;
- }
- return newton(newX);
- }
- public static double f(double x) {
- return x*x*x - x*x - 2*x +1;
- }
- public static double df(double x) {
- return 3*x*x - 2*x - 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement