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