Advertisement
Guest User

8.2

a guest
Dec 11th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4. using namespace std;
  5. double Y(double x)
  6. {
  7. double Y = (1+2*x*x)*exp(x*x);
  8. return Y;
  9. }
  10.  
  11. double Sum(int n, double x)
  12. {
  13. double Sum=1;
  14. double v=1;
  15. for (int k=1;k<=n;k++)
  16. {
  17. v=v*x*x*(2*k+1)/(k*(2*k-1));
  18. Sum+=v;
  19. }
  20. return Sum;
  21. }
  22. void Out_Rez(int vibor, double a, double b, double h, double (*pY)(double x) ,double (*pSum)(int n, double x))
  23. {
  24. double x, modul;
  25. int i = 0;
  26. switch (vibor)
  27. {
  28. case 1:
  29. cout << "\nШаг\tx:\tY(x):" << endl;
  30. for (x=a; x<=b; x+= h)
  31. {
  32. cout << 1 + i << "\t" << setprecision(2) << x << "\t" << setprecision(6) << Y(x) << endl;
  33. i++;
  34. }
  35. break;
  36. case 2:
  37. int n;
  38. cout << "Введите значение параметра n: " << endl;
  39. cout << "n = ";
  40. cin >> n;
  41. cout << "\nШаг\tx:\tS(x):" << endl;
  42. for (x=a; x<=b; x+= h)
  43. {
  44. cout << 1 + i << "\t" << setprecision(2) << x << "\t" << setprecision(6) << Sum(n,x) << endl;
  45. i++;
  46. }
  47. break;
  48. case 3:
  49. cout << "Введите значение параметра n: " << endl;
  50. cout << "n = ";
  51. cin >> n;
  52. cout << "\nШаг\tx:\t\tY(x):\t\tS(x):\t\t|Y(x) - S(x)|:" << endl;
  53. for (x=a; x<=b; x+= h)
  54. {
  55. modul = fabs(Y(x) - Sum(n,x));
  56. cout << 1 + i << "\t" << setw(2) << x << "\t\t" << setw(6) << Y(x) << "\t\t" << Sum(n,x) << "\t\t" << modul << endl;
  57. i++;
  58. }
  59. break;
  60. }
  61. }
  62. int main()
  63. {
  64. setlocale(LC_ALL, "rus");
  65. int vibor;
  66. double a, b, h;
  67. cout << "Выберите функцию: \n 1) Y(x) \n 2) S(x) \n 3) |Y(x) - S(x)|" << endl;
  68. cin >> vibor;
  69. while (vibor<0 || vibor>3)
  70. {
  71. cout<<"Ошибка! Неправильный ввод."<<endl;
  72. cout << "Выберите функцию: \n 1) Y(x) \n 2) S(x) \n 3) |Y(x) - S(x)|" << endl;
  73. cin >> vibor;
  74. }
  75. cout << "Введите h: "; cin>>h;
  76. cout<< "Введите a: "; cin>>a;
  77. cout<< "Введите b: "; cin>>b;
  78. while(a==0 && b==0 || h==0)
  79. {
  80. cout<<"Результат не может быть достигнут!"<<endl;
  81. cout<<"Ошибка! Неправильный ввод."<<endl;
  82. cout << "Введите h: "; cin>>h;
  83. cout<< "Введите a: "; cin>>a;
  84. cout<< "Введите b: "; cin>>b;
  85. }
  86. Out_Rez(vibor, a, b, h, &Y, &Sum);
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement