Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- final int MAX_ITER = 100;
- final double FirstCoef = 9.33;
- final double SecondCoef = 6.977;
- final double ThirdCoef = 7.25;
- double a;
- double b;
- double Accuracy;
- double Difference;
- double x0;
- double fFromDeltaMinus;
- double fFromDeltaPlus;
- double fFromx0;
- double Convergence;
- int k = 0;
- boolean IsCorrect;
- System.out.println("Subject: Find the roots of the equation 9.33sin(6.977x) - 7.25x = 0 with Epsilon accuracy by a simple iteration");
- do {
- System.out.printf("Enter the span boundaries [a;b] \n");
- a = input();
- b = input();
- Convergence = (FirstCoef * Math.sin(SecondCoef * a) / 7.25) * (FirstCoef * Math.sin(SecondCoef * b) / ThirdCoef);
- if (Convergence < 0) {
- System.out.printf("Iteration process converges \n");
- IsCorrect = true;
- } else {
- System.out.printf("Iteration process does not converge \n");
- IsCorrect = false;
- }
- } while (!IsCorrect);
- System.out.printf("Enter accuracy value (Epsilon) \n");
- Accuracy = input();
- if ((Accuracy > 0) && (Accuracy < 1)) {
- IsCorrect = true;
- } else {
- IsCorrect = false;
- }
- while (!IsCorrect) {
- System.out.printf("Accuracy value must lie between (0;1) \n");
- Accuracy = input();
- }
- x0 = ((a + b) / 2);
- while ((Accuracy < Math.abs(a - x0)) || (k > MAX_ITER)) {
- fFromDeltaMinus = FirstCoef * Math.sin(SecondCoef * (a - Accuracy)) / ThirdCoef;
- fFromDeltaPlus = FirstCoef * Math.sin(SecondCoef * (a + Accuracy)) / ThirdCoef;
- Difference = -(fFromDeltaPlus - fFromDeltaMinus) / (2 * Accuracy);
- x0 = a;
- fFromx0 = FirstCoef * Math.sin(SecondCoef * x0) / ThirdCoef;
- a = x0 + fFromx0 / Difference;
- k = k + 1;
- System.out.printf("C= %.6f ", a);
- System.out.printf("f(c)= %.6f \n", fFromx0);
- }
- if (Accuracy > Math.abs(a - x0)) {
- System.out.printf("C= %.6f ", a);
- System.out.printf("Number of iterations: %d \n ", k);
- } else {
- System.out.printf("Answer not found!");
- }
- }
- public static float input() {
- Scanner scan = new Scanner(System.in);
- float value = 1;
- try {
- value = scan.nextFloat();
- return value;
- } catch (Throwable error) {
- System.out.println("Ошибка! Введите число: ");
- return input();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement