constk

Lab5_Task2

Nov 13th, 2019
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <math.h>
  5.  
  6. int main() {
  7. setlocale(0, "Russian");
  8.  
  9. short n = 1;
  10. float leftLimit = 2, rightLimit = 3, eps = pow(10, -5), x, yLeft, yRight, y;
  11. puts("Изначальное положение.");
  12. printf("Левая граница: %f\n", leftLimit);
  13. printf("Правая граница: %f\n", rightLimit);
  14. puts("########################################");
  15.  
  16. x = (leftLimit + rightLimit) / 2;
  17. yLeft = 3 * sin(sqrt(leftLimit)) + 0.35*leftLimit - 3.8;
  18. yRight = 3 * sin(sqrt(rightLimit)) + 0.35*rightLimit - 3.8;
  19. y = 3 * sin(sqrt(x)) + 0.35*x - 3.8;
  20.  
  21. while ( fabs(yLeft - yRight) > eps ) {
  22. if ( y*yRight < 0 )
  23. leftLimit = x;
  24. else
  25. rightLimit = x;
  26. printf("Итерация номер %d\n", n);
  27. printf("Левая граница: %f\n", leftLimit);
  28. printf("Правая граница: %f\n", rightLimit);
  29. puts("########################################");
  30.  
  31. x = (leftLimit + rightLimit) / 2;
  32. y = 3 * sin(sqrt(x)) + 0.35*x - 3.8;
  33. yLeft = 3 * sin(sqrt(leftLimit)) + 0.35*leftLimit - 3.8;
  34. yRight = 3 * sin(sqrt(rightLimit)) + 0.35*rightLimit - 3.8;
  35. n++;
  36. }
  37.  
  38. puts("");
  39. y = 3 * sin(sqrt(x)) + 0.35*x - 3.8;
  40. if (fabs(y) > eps)
  41. printf("Корень найден неверно\n");
  42. else
  43. printf("Корень = %f\nНайден после %d итераций\n", x, n-1);
  44. printf("y(%2.2f) = %f\n", x, y);
  45.  
  46. puts("");
  47. system("pause");
  48. return 0;
  49. }
RAW Paste Data