fork download
  1. public class Main {
  2.  
  3.  
  4. static double f(double x) {
  5. return ((x - 2) * x - 1) * x - 3;
  6. }
  7.  
  8.  
  9. static double fPrime(double x) {
  10. return (3 * x - 4) * x - 1;
  11. }
  12.  
  13. public static void main(String[] args) {
  14.  
  15. double x = 3;
  16. double eps = 1e-6;
  17. int iter = 1;
  18.  
  19. System.out.println("ENTER THE TOTAL NO. OF POWER:::: 3");
  20. System.out.println("x^0:: -3");
  21. System.out.println("x^1:: -1");
  22. System.out.println("x^2:: -2");
  23. System.out.println("x^3:: 1");
  24. System.out.println("\nTHE POLYNOMIAL IS :: x^3 - 2x^2 - 1x - 3");
  25. System.out.println("INITIAL X1 ----> 3\n");
  26.  
  27. System.out.println("******************************");
  28. System.out.println("ITERATION X1 FX1 FPX1");
  29. System.out.println("*******************************");
  30.  
  31. while (true) {
  32.  
  33. double fx = f(x);
  34. double fpx = fPrime(x);
  35. double xNext = x - fx / fpx;
  36.  
  37. System.out.printf("%-10d %.6f %.6f %.6f\n",
  38. iter, x, fx, fpx);
  39.  
  40. if (Math.abs(xNext - x) < eps)
  41. break;
  42.  
  43. x = xNext;
  44. iter++;
  45. }
  46.  
  47. System.out.println("*******************");
  48. System.out.println("\nTHE ROOT OF EQUATION IS " + x);
  49. }
  50. }
  51.  
Success #stdin #stdout 0.17s 58072KB
stdin
Standard input is empty
stdout
ENTER THE TOTAL NO. OF POWER:::: 3
x^0:: -3
x^1:: -1
x^2:: -2
x^3::  1

THE POLYNOMIAL IS :: x^3 - 2x^2 - 1x - 3
INITIAL X1 ----> 3

******************************
ITERATION    X1        FX1        FPX1
*******************************
1           3.000000   3.000000   14.000000
2           2.785714   0.311589   11.137755
3           2.757738   0.004954   10.784409
4           2.757279   0.000001   10.778647
*******************

THE ROOT OF EQUATION IS  2.757279044183619