Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package metoda_gaussa.seidla;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class Metoda_gaussaSeidla {
  8.    
  9.     static double h = 0.00001;
  10.    
  11.     static double funkcja(double x, double y) {
  12.         // Math.pow(y, 3) + Math.pow(x, 2) - 7 * x * y - 3 * x + 9;
  13.         return 10*x*x+12*x*y+10*y*y;
  14.         //return Math.pow(x,2) * Math.pow(y,2);
  15.     }
  16.    
  17.     static double pochodna_x(double x, double y) {
  18.         return (funkcja(x + h, y) - funkcja(x, y)) / h;
  19.     }
  20.    
  21.     static double pochodna_y(double x, double y) {
  22.         return (funkcja(x, y + h) - funkcja(x, y)) / h;
  23.     }
  24.    
  25.     static double pochodna_x2(double x, double y) {        
  26.         return pochodna_x(pochodna_x(x, y), y);
  27.     }
  28.    
  29.     static double pochodna_y2(double x, double y) {
  30.         return pochodna_y(x, pochodna_y(x, y));
  31.     }
  32.    
  33.     static double pochodna_xy(double x, double y) {
  34.         return (funkcja(x + h, y + h) - funkcja(x + h, y) - funkcja(x, y + h) + funkcja(x,y)) / Math.pow(h, 2);
  35.     }
  36.    
  37.     static double pochodna_x3(double x, double y) {
  38.         return pochodna_x2(pochodna_x(x, y), y);
  39.     }
  40.    
  41.     static double pochodna_y3(double x, double y) {
  42.         return pochodna_y2(x, pochodna_y(x,y));
  43.     }
  44.    
  45.     static double metoda_stycznych(double XY, String xy, double epsilon) {
  46.         double a = -100, b = 100;    
  47.         double x, y;
  48.         int iter = 0;
  49.         ArrayList<Double> xn = new ArrayList<>();
  50.         xn.add(100.0);
  51.         ArrayList<Double> yn = new ArrayList<>();
  52.         yn.add(100.0);
  53.        
  54.        
  55.         if(xy == "x") {
  56.             y = XY;
  57.             x = 0;
  58.             if((pochodna_x(a, y) >= 0 && pochodna_x3(b, y) >= 0) || (pochodna_x(a, y) < 0 && pochodna_x(b, y) < 0)) {
  59.                 xn.add(a);
  60.             } else {
  61.                 xn.add(b);
  62.             }
  63.            
  64.             while(Math.abs(pochodna_x(xn.get(iter+1), y)) >= epsilon || Math.abs(xn.get(iter+1) - xn.get(iter)) >= epsilon) {
  65.                 xn.add(xn.get(iter) - (pochodna_x(xn.get(iter), y) / pochodna_x2(xn.get(iter), y)));
  66.                
  67.                 System.out.println("xn1 = " + xn.get(iter));
  68.                 iter++;
  69.             }
  70.         }
  71.        
  72.         if(xy == "y") {
  73.             x = XY;
  74.             y = 0;
  75.             if((pochodna_y(a, y) >= 0 && pochodna_y3(b, y) >= 0) || (pochodna_y(a, y) < 0 && pochodna_y3(b, y) < 0)) {
  76.                 yn.add(a);
  77.             } else {
  78.                 yn.add(b);
  79.             }
  80.            
  81.             while(Math.abs(pochodna_y(x, yn.get(iter + 1))) >= epsilon || Math.abs(yn.get(iter+1) - yn.get(iter)) >= epsilon) {
  82.                 yn.add(yn.get(iter) - (pochodna_y(x, yn.get(iter)) / pochodna_y2(x, yn.get(iter))));
  83.                 System.out.println("yn1 = " + yn.get(iter));
  84.                 iter++;
  85.             }
  86.         }
  87.         if(xy == "x") return xn.get(iter);
  88.         else return yn.get(iter);
  89.     }
  90.    
  91.     public static void main(String[] args) {
  92.         double x = 10, y = 10;
  93.         double EPSILON = 0.001;
  94.         double epsilon = 0.000001;        
  95.         int iter = 1;
  96.            
  97.         while(Math.abs(pochodna_xy(x,y)) > EPSILON) {
  98.            
  99.             System.out.println("x=" + x);
  100.             x = metoda_stycznych(y, "x", epsilon);
  101.            
  102.             System.out.println("x=" + x);
  103.             y = metoda_stycznych(x, "y", epsilon);
  104.            
  105.             System.out.println(x + " | " + y);
  106.            
  107.         }
  108.         System.out.println(x + " | " + y);
  109.     }
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement