SHARE
TWEET

Untitled

a guest Apr 26th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Optimization {
  4.  
  5.     public static void main(String[] args) {
  6.         double x, y, e, alfa;
  7.         Scanner in = new Scanner(System.in);
  8.         System.out.println("Podaj x: ");
  9.         x = in.nextDouble();
  10.         System.out.println("Podaj y: ");
  11.         y = in.nextDouble();
  12.         System.out.println("Podaj alfa: ");
  13.         alfa = in.nextDouble();
  14.         System.out.println("Podaj dokładność wyniku: ");
  15.         e = in.nextDouble();
  16.         iterative_ekstremum(x, y, e, alfa);
  17.     }
  18.  
  19.     public static double derivative_x(double x, double y) {
  20.         return -2 * x * Math.exp(-x * x + y * y) * ((x * x + y * y) - 1);
  21.     }
  22.  
  23.     public static double derivative_y(double x, double y) {
  24.         return -2 * y * Math.exp(-x * x + y * y) * ((x * x + y * y) - 1);
  25.     }
  26.  
  27.     public static double function(double x, double y) {
  28.         return ((x * x + y * y) * Math.exp(-x * x + -y * y));
  29.     }
  30.  
  31.     public static void iterative_ekstremum(double x, double y, double e, double alfa) {
  32.         double new_x, new_y;
  33.         double a = alfa;
  34.         double func_x_previous;
  35.         double func_x_next;
  36.         int countLoop = 0;
  37.         do {
  38.             countLoop++;
  39.             new_x = x - a * derivative_x(x, y);
  40.             new_y = y - a * derivative_y(x, y);
  41.             func_x_previous = function(x, y);
  42.             func_x_next = function(new_x, new_y);
  43.             if (func_x_next >= func_x_previous) {
  44.                 a -= 0.000005;
  45.             } else {
  46.                 x = new_x;
  47.                 y = new_y;
  48.                 System.out.println("Loop: "+countLoop + "\tx: " + new_x + "\ty: " + new_y + "\tz: " + func_x_next);
  49.             }
  50.         } while (Math.abs(func_x_next - func_x_previous) > e);
  51.         System.out.println(countLoop + "\t" + new_x + "\t" + new_y + "\t" + func_x_next);
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top