fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double f(double co[], int n, double x)
  5. {
  6. double result = co[0];
  7. for (int i = 1; i <= n; i++)
  8. result = result * x + co[i];
  9. return result;
  10. }
  11.  
  12. double df(double co[], int n, double x)
  13. {
  14. double result = co[0] * (n);
  15. for (int i = 1; i < n; i++)
  16. result = result * x + co[i] * (n - i);
  17. return result;
  18. }
  19.  
  20. int main() {
  21. int n;
  22. double co[20];
  23. double x0, x1, fx, dfx, E, error;
  24. int iteration = 1, maxIter = 20;
  25.  
  26. cout << "ENTER THE TOTAL NO. OF POWER::: ";
  27. cin >> n;
  28.  
  29. cout << endl;
  30. for (int i = 0; i <= n; i++) {
  31. cout << "x^" << i << " :: ";
  32. cin >> co[i];
  33. }
  34.  
  35. cout << "\nTHE POLYNOMIAL IS ::: ";
  36. for (int i = n; i >= 0; i--) {
  37. cout << co[n - i] << "x^" << i << " ";
  38. }
  39. cout << endl;
  40.  
  41. cout << "\nEnter initial guess x0: ";
  42. cin >> x0;
  43. cout << "Enter tolerance (e.g. 0.0001): ";
  44. cin >> E;
  45.  
  46. cout << "\n***********************************************";
  47. cout << "\nIteration\t x\t\t f(x)\t\t f'(x)\t\t Error\t\t %Error";
  48. cout << "\n***********************************************\n";
  49.  
  50. do {
  51. fx = f(co, n, x0);
  52. dfx = df(co, n, x0);
  53.  
  54. if (dfx == 0) {
  55. cout << "Division by zero!.\n";
  56. return 0;
  57. }
  58.  
  59. x1 = x0 - fx / dfx;
  60. error = fabs((x1 - x0) / x1);
  61.  
  62. cout << fixed << setprecision(6);
  63. cout << setw(5) << iteration << "\t"
  64. << setw(10) << x1 << "\t"
  65. << setw(10) << fx << "\t"
  66. << setw(10) << dfx << "\t"
  67. << setw(10) << error << "\t"
  68. << setw(10) << error * 100 << endl;
  69.  
  70. if (error < E)
  71. break;
  72.  
  73. x0 = x1;
  74. iteration++;
  75.  
  76. } while (iteration <= maxIter);
  77.  
  78. cout << "\n***********************************************";
  79. cout << "\nTHE ROOT OF EQUATION IS " << x1 << endl;
  80. cout << "***********************************************\n";
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0.01s 5284KB
stdin
3
-3
-1
0
1
3
stdout
ENTER THE TOTAL NO. OF POWER::: 
x^0 :: x^1 :: x^2 :: x^3 :: 
THE POLYNOMIAL IS ::: -3x^3 -1x^2 0x^1 1x^0 

Enter initial guess x0: Enter tolerance (e.g. 0.0001): 
***********************************************
Iteration	 x		 f(x)		 f'(x)		 Error		 %Error
***********************************************
    1	  1.977011	-89.000000	-87.000000	  0.517442	 51.744186
    2	  1.310268	-26.090464	-39.131193	  0.508860	 50.886028
    3	  0.897181	 -7.465217	-18.071759	  0.460428	 46.042844
    4	  0.679071	 -1.971444	 -9.038759	  0.321189	 32.118908
    5	  0.606350	 -0.400570	 -5.508373	  0.119931	 11.993108
    6	  0.598288	 -0.036454	 -4.521647	  0.013475	  1.347541
    7	  0.598194	 -0.000418	 -4.418115	  0.000158	  0.015821
    8	  0.598193	 -0.000000	 -4.416906	  0.000000	  0.000002
    9	  0.598193	 -0.000000	 -4.416906	  0.000000	  0.000000
   10	  0.598193	  0.000000	 -4.416906	  0.000000	  0.000000

***********************************************
THE ROOT OF EQUATION IS 0.598193
***********************************************