fork download
  1. //0JvQtdGJ0LXQvdC60L4=
  2. #include <iostream>
  3. #include <math.h>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. double eps = 0.0001;
  9.  
  10. double f(const double x) {
  11. return log(2 * x) - 9 * x + 4;
  12. }
  13. double PodilVidrizkaNaDva(double p, double q){
  14. double a{};
  15. while (fabs(p - q) >= eps) {
  16. a = (p + q) / 2;
  17.  
  18. if (f(p) * f(a) < 0)
  19. q = a;
  20.  
  21. else
  22. p = a;
  23. }
  24. return a;
  25. }
  26. double ProstyhIteracyyFormula(const double x) {
  27. // log2x = 9x - 4
  28. // => x = (log2x + 4) / 9
  29. return (log(2 * x) + 4) / 9;
  30. }
  31.  
  32. double NewtonFormula(const double x) {
  33. // x n+1 = x - f(xn)/f`(xn)
  34. return x - f(x) / (1 / (2 * x) - 9);
  35. }
  36.  
  37. double Iterations(double(*f)(double x), const double a) {
  38. if (fabs(f(a) - f(f(a))) <= eps)
  39. return f(a);
  40. return Iterations(f, f(a));
  41. }
  42.  
  43. double MethodHordFormula(const double a, const double b) {
  44. return b - (f(b) * (b - a) / (f(b) - f(a)));
  45. }
  46. double Iterations(const double a, const double b) {
  47.  
  48. if (fabs(MethodHordFormula(a,b) - MethodHordFormula(b, MethodHordFormula(a,b))) <= eps)
  49. return MethodHordFormula(a,b);
  50.  
  51. return Iterations(MethodHordFormula(a,b), MethodHordFormula(b, MethodHordFormula(a, b)));
  52. }
  53. int main()
  54. {
  55. cout << setprecision(10);
  56. cout << "metod podilu navpil\t-> "<< PodilVidrizkaNaDva(0.1,1)<< endl;
  57. cout << "metod hord\t-> " << Iterations(0.1, 0.5) << endl;
  58. cout << "metod prostyh iteracy\t-> " << Iterations(ProstyhIteracyyFormula, 0.1) << endl;
  59. cout << "Newton method\t-> " << Iterations(NewtonFormula, 0.1) << endl;
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
metod podilu navpil	-> 0.4268981934
metod hord	-> 0.4268853061
metod prostyh iteracy	-> 0.4268074023
Newton method	-> 0.4268986886