aLT22

Untitled

May 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. package logic;
  2.  
  3. /**
  4.  * Created by Alexey on 07.05.2016.
  5.  */
  6. public class GSM {
  7.     private double a, b, epsilon;
  8.  
  9.     public double getA() {
  10.         return a;
  11.     }
  12.  
  13.     public void setA(double a) {
  14.         this.a = a;
  15.     }
  16.  
  17.     public double getB() {
  18.         return b;
  19.     }
  20.  
  21.     public void setB(double b) {
  22.         this.b = b;
  23.     }
  24.  
  25.     public double getEpsilon() {
  26.         return epsilon;
  27.     }
  28.  
  29.     public void setEpsilon(double epsilon) {
  30.         this.epsilon = epsilon;
  31.     }
  32.  
  33.     private double f(double x){
  34.         return 3.0*x - 2.;
  35.     }
  36.  
  37.     public double algorithm(){
  38.         double y = a + ((3. - Math.sqrt(5.))/(2.))*(b - a);
  39.         double z = a + b - y;
  40.         while (Math.abs(b - a) > epsilon){
  41.             System.out.print(a + " a " + b + " b ");
  42.             System.out.print(epsilon + " epsilon ");
  43.             System.out.println(Math.abs(b-a) + " module");
  44.             if (f(y) <= f(z)){
  45.                 b = z;
  46.                 y = a + b - y;
  47.                 z = y;
  48.             } else {
  49.                 a = y;
  50.                 y = z;
  51.                 z = a + b - y;
  52.             }
  53.         }
  54.         return ((a + b) / 2.);
  55.     }
  56. }
  57.  
  58. package logic;
  59.  
  60. /**
  61.  * Created by Alexey on 07.05.2016.
  62.  */
  63. public class Main {
  64.     public static void main(String[] args) {
  65.         GSM gsm = new GSM();
  66.         gsm.setA(1.004);
  67.         gsm.setB(3.);
  68.         gsm.setEpsilon(0.000000001);
  69.         System.out.println(gsm.algorithm());
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment