Advertisement
Guest User

Untitled

a guest
May 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. public class Main {
  2.  
  3. private static final double epsilon = 0.000001;
  4. private static double from;
  5. private static double to;
  6. private static double aprior;
  7.  
  8. public static void main(String[] args) {
  9. double xcur;
  10. double xnext = calculateApproximateX();
  11. int iterations = 0;
  12. System.out.println("Выполнение условий теоремы: " + checkTheoremConditions(xnext));
  13. System.out.println("Начальное приближение: " + xnext);
  14. do {
  15. xcur = xnext;
  16. xnext = calculateFi(xcur);
  17. iterations++;
  18. } while (Math.abs(xnext - xcur) >= epsilon);
  19. System.out.println("Решение: " + xnext);
  20. System.out.println("Невязка: " + Math.abs(calculateF(xnext)));
  21. System.out.println("Количество итераций: " + iterations);
  22. System.out.println("Априорное количество итераций: " + aprior);
  23. }
  24.  
  25. private static double calculateApproximateX() {
  26. double left = -0.5;
  27. double right = 0.5;
  28. double tmp;
  29. while (Math.abs(left - right) >= 10) {
  30. tmp = (left + right) / 2;
  31. if (calculateF(left) * calculateF(tmp) < 0) {
  32. right = tmp;
  33. } else {
  34. left = tmp;
  35. }
  36. }
  37. from = left;
  38. to = right;
  39. return (left + right) / 2;
  40. }
  41.  
  42. private static double calculateF(double x) {
  43. return Math.pow(x, 2) - Math.pow(Math.cos(Math.PI * x),2);
  44. }
  45.  
  46. private static double calculateFi(double x) {
  47. return Math.acos(Math.pow(Math.pow(x, 2), 0.5)) / Math.PI;
  48. }
  49.  
  50. private static double calculateFiDeriv(double x) {
  51. return - x / (Math.PI * Math.pow(Math.pow(x, 2) - Math.pow(x, 4), 0.5));
  52. }
  53.  
  54. private static boolean checkTheoremConditions(double xApprox) {
  55. double delta = (to - from) / 2;
  56. double m = Math.abs(xApprox - calculateFi(xApprox));
  57. double q = Math.max(calculateFiDeriv(to), calculateFiDeriv(from));
  58. aprior = Math.log(epsilon * (1 - q) / m) / Math.log(q);
  59. return (m / (1 - q)) <= delta;
  60. }
  61. }
  62. Выполнение условий теоремы: true
  63. Начальное приближение: 0.0
  64. Решение: 0.3769668917546923
  65. Невязка: 3.4676940013911306E-7
  66. Количество итераций: 14
  67. Априорное количество итераций: 14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement