Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.  
  6.         final int MAX_ITER = 100;
  7.         final double FirstCoef = 9.33;
  8.         final double SecondCoef = 6.977;
  9.         final double ThirdCoef = 7.25;
  10.         double a;
  11.         double b;
  12.         double Accuracy;
  13.         double Difference;
  14.         double x0;
  15.         double fFromDeltaMinus;
  16.         double fFromDeltaPlus;
  17.         double fFromx0;
  18.         double Convergence;
  19.         int k = 0;
  20.         boolean IsCorrect;
  21.         System.out.println("Subject: Find the roots of the equation 9.33sin(6.977x) - 7.25x = 0 with Epsilon accuracy by a simple iteration");
  22.         do {
  23.             System.out.printf("Enter the span boundaries [a;b] \n");
  24.             a = input();
  25.             b = input();
  26.             Convergence = (FirstCoef * Math.sin(SecondCoef * a) / 7.25) * (FirstCoef * Math.sin(SecondCoef * b) / ThirdCoef);
  27.             if (Convergence < 0) {
  28.                 System.out.printf("Iteration process converges \n");
  29.                 IsCorrect = true;
  30.             } else {
  31.                 System.out.printf("Iteration process does not converge \n");
  32.                 IsCorrect = false;
  33.             }
  34.         } while (!IsCorrect);
  35.         System.out.printf("Enter accuracy value (Epsilon) \n");
  36.         Accuracy = input();
  37.         if ((Accuracy > 0) && (Accuracy < 1)) {
  38.             IsCorrect = true;
  39.         } else {
  40.             IsCorrect = false;
  41.         }
  42.         while (!IsCorrect) {
  43.             System.out.printf("Accuracy value must lie between (0;1) \n");
  44.             Accuracy = input();
  45.         }
  46.         x0 = ((a + b) / 2);
  47.         while ((Accuracy < Math.abs(a - x0)) || (k > MAX_ITER)) {
  48.             fFromDeltaMinus = FirstCoef * Math.sin(SecondCoef * (a - Accuracy)) / ThirdCoef;
  49.             fFromDeltaPlus = FirstCoef * Math.sin(SecondCoef * (a + Accuracy)) / ThirdCoef;
  50.             Difference = -(fFromDeltaPlus - fFromDeltaMinus) / (2 * Accuracy);
  51.             x0 = a;
  52.             fFromx0 = FirstCoef * Math.sin(SecondCoef * x0) / ThirdCoef;
  53.             a = x0 + fFromx0 / Difference;
  54.             k = k + 1;
  55.             System.out.printf("C= %.6f ", a);
  56.             System.out.printf("f(c)= %.6f \n", fFromx0);
  57.         }
  58.         if (Accuracy > Math.abs(a - x0)) {
  59.             System.out.printf("C= %.6f ", a);
  60.             System.out.printf("Number of iterations: %d \n ", k);
  61.         } else {
  62.             System.out.printf("Answer not found!");
  63.         }
  64.     }
  65.  
  66.     public static float input() {
  67.         Scanner scan = new Scanner(System.in);
  68.         float value = 1;
  69.         try {
  70.             value = scan.nextFloat();
  71.             return value;
  72.         } catch (Throwable error) {
  73.             System.out.println("Ошибка! Введите число: ");
  74.             return input();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement