Natalia__krkrkr

Метод золотого сечения

Sep 15th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         int startA = 1;
  4.         int startB = 3;
  5.         double e = 0.001;
  6.         double eN = 0;
  7.         int n = 1;
  8.         double a = startA;
  9.         double b = startB;
  10.         double x;
  11.         do{
  12.  
  13.             eN = end_e(n, startA, startB);
  14.  
  15.             double xOne = xOne(a,b);
  16.             double formulsXOne = formula(xOne);
  17.             double xTwo = xTwo(a,b);
  18.             double formulXTwo = formula(xTwo);
  19.             if(formulsXOne>formulXTwo) {
  20.                 System.out.print("Функция убывающая:  ");
  21.                 a = xOne;
  22.                 System.out.print("n: " + n + " ");
  23.                 System.out.print("E: " + Math.round(eN * 10000.0) / 10000.0 + " ");
  24.                 System.out.print("a: " + Math.round(a * 100.0) / 100.0 + " ");
  25.                 System.out.print("b: " + Math.round(b * 100.0) / 100.0 + " ");
  26.                 x = xTwo;
  27.                 System.out.print("x1: " +Math.round(xOne * 100.0) / 100.0  + " ");
  28.                 System.out.print("x2: " + Math.round(xTwo * 100.0) / 100.0 + " ");
  29.                 System.out.print("x: " + Math.round(x * 100.0)/ 100.0  + " ");
  30.                 System.out.print("formulsXOne: " + Math.round(formulsXOne * 100.0)/ 100.0  + " ");
  31.                 System.out.println("formulXTwo: " + Math.round(formulXTwo * 100.0)/ 100.0  + " ");
  32.             }
  33.             if (formulsXOne<formulXTwo){
  34.                 System.out.print("Функция возрастает:  ");
  35.                 b = xTwo;
  36.                 System.out.print("n: " + n + " ");
  37.                 System.out.print("E: " + Math.round(eN * 10000.0) / 10000.0 + " ");
  38.                 System.out.print("a: " + Math.round(a * 100.0) / 100.0 + " ");
  39.                 System.out.print("b: " + Math.round(b * 100.0) / 100.0 + " ");
  40.                 x = xOne;
  41.                 System.out.print("x1: " +Math.round(xOne * 100.0) / 100.0  + " ");
  42.                 System.out.print("x2: " + Math.round(xTwo * 100.0) / 100.0 + " ");
  43.                 System.out.print("x: " + Math.round(x * 100.0)/ 100.0  + " ");
  44.                 System.out.print("formulsXOne: " + Math.round(formulsXOne * 100.0)/ 100.0  + " ");
  45.                 System.out.println("formulXTwo: " + Math.round(formulXTwo * 100.0)/ 100.0  + " ");
  46.                 }
  47.             n++;
  48.         }while (eN>e);
  49.  
  50.     }
  51.     public static double formula (double x){
  52.         double end_x;
  53.         return  end_x = 10*Math.pow((3-x),4)+5*x*x;
  54.     }
  55.  
  56.     public static double xOne (double a, double b){
  57.         double end_x1;
  58.         return end_x1 = a + 0.38*(b-a);
  59.     }
  60.  
  61.     public static double xTwo (double a, double b){
  62.         double end_x1;
  63.         return end_x1 = a + 0.62*(b-a);
  64.     }
  65.  
  66.     public static double end_e(int n, double a, double b){
  67.         double end_e;
  68.         return end_e = Math.pow(((Math.sqrt(5)-1)/2),n)*(b-a);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment