Advertisement
Guest User

Untitled

a guest
May 28th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. public class Main{
  2.  
  3.     public static void main(String[] args) {
  4.         int a = -1;
  5.         int b = 1;
  6.         int c = -1;
  7.         int d = 1;
  8.  
  9.         PoissonEquation equation = new PoissonEquation(a, b, c, d);
  10.         equation.calculateSolution();
  11.         equation.printSolution();
  12.     }
  13. }
  14.  
  15. class PoissonEquation{
  16.  
  17.     private int a;
  18.     private int b;
  19.     private int c;
  20.     private int d;
  21.  
  22.     private double h1 = 0.05;
  23.     private int N1;
  24.     private double h2 = 0.15;
  25.     private int N2;
  26.  
  27.     private double h2plus;
  28.     private double h2minus;
  29.  
  30.     private double[] nodesX1;
  31.     private double[] nodesX2;
  32.  
  33.     private double accuracy;
  34.  
  35.     private double[][] solution;
  36.  
  37.     public PoissonEquation(int a, int b, int c, int d) {
  38.         this.a = a;
  39.         this.b = b;
  40.         this.c = c;
  41.         this.d = d;
  42.  
  43.         N1 = (int)((b - a) / h1);
  44.         N2 = (int)((d - c) / h2) + 1;
  45.  
  46.         nodesX1 = new double[N1 + 1];
  47.         fillNodesX1();
  48.         nodesX2 = new double[N2 + 1];
  49.         fillNodesX2();
  50.  
  51.         h2plus = (d - c) - (N2 - 1) * h2;
  52.         h2minus = h2;
  53.  
  54.         accuracy = h1 * h1 * h1;
  55.     }
  56.  
  57.     private void fillNodesX1(){
  58.         for(int i = 0; i < N1 + 1; i++){
  59.             nodesX1[i] = a + i * h1;
  60.         }
  61.     }
  62.  
  63.     private void fillNodesX2(){
  64.         for(int j = 0; j < N2; j++){
  65.             nodesX2[j] = c + j * h2;
  66.         }
  67.         nodesX2[N2] = d;
  68.     }
  69.  
  70.     private double computationF(double x1, double x2){
  71.         return Math.abs(Math.pow(Math.sin(Math.PI * x1 * x2), 3));
  72.     }
  73.  
  74.     private double computationPsi1(double x2){
  75.         return 1 - x2 * x2;
  76.     }
  77.  
  78.     private double computationPsi2(double x2){
  79.         return 1 - x2 * x2;
  80.     }
  81.  
  82.     private double computationPsi3(double x1){
  83.         return Math.abs(Math.sin(Math.PI * x1));
  84.     }
  85.  
  86.     private double computationPsi4(double x1){
  87.         return Math.abs(Math.sin(Math.PI * x1));
  88.     }
  89.  
  90.     public void calculateSolution(){
  91.         double[][] prevSolution;
  92.         double[][] nextSolution = new double[N2 + 1][N1 + 1];
  93.         for(int j = 0; j < N2 + 1; j++){
  94.             nextSolution[j][0] = computationPsi1(nodesX2[j]);
  95.             nextSolution[j][N1] = computationPsi2(nodesX2[j]);
  96.         }
  97.         for(int i = 0; i < N1 + 1; i++){
  98.             nextSolution[0][i] = computationPsi3(nodesX1[i]);
  99.             nextSolution[N2][i] = computationPsi4(nodesX1[i]);
  100.         }
  101.         for(int j = 1; j < N2; j++){
  102.             for(int i = 1; i < N1; i++){
  103.                 nextSolution[j][i] = computationF(nodesX1[i], nodesX2[j]);
  104.             }
  105.         }
  106.  
  107.         do{
  108.             prevSolution = copyMatrix(nextSolution);
  109.  
  110.             for(int j = 1; j < N2 - 1; j++){
  111.                 for(int i = 1; i < N1; i++){
  112.                     nextSolution[j][i] = Math.pow(2.0 / (h1 * h1) + 2.0 / (h2 * h2), -1) *
  113.                             (
  114.                                     (prevSolution[j][i + 1] + prevSolution[j][i - 1]) / (h1 * h1) +
  115.                                     (prevSolution[j + 1][i] + prevSolution[j - 1][i]) / (h2 * h2) +
  116.                                     computationF(nodesX1[i], nodesX2[j])
  117.                             );
  118.                 }
  119.             }
  120.             for(int i = 1; i < N1; i++){
  121.                 nextSolution[N2 - 1][i] =
  122.                         Math.pow(2.0 / (h1 * h1) + 1.0 / ((h2plus + h2minus) / 2) * (1.0 / h2plus + 1.0 / h2minus), -1) *
  123.                                 (
  124.                                         (prevSolution[N2 - 1][i + 1] + prevSolution[N2 - 1][i - 1]) / (h1 * h1) +
  125.                                         (1.0 / ((h2plus + h2minus) / 2)) * (prevSolution[N2][i] / h2plus + prevSolution[N2 - 2][i] / h2minus) +
  126.                                                 computationF(nodesX1[i], nodesX2[N2 - 1])
  127.                                 );
  128.             }
  129.         }while(normMatrix(differenceMatrix(nextSolution, prevSolution)) >= accuracy);
  130.  
  131.         solution = nextSolution;
  132.     }
  133.  
  134.     public void printSolution(){
  135.         for(int j = 0; j < N2 + 1; j++){
  136.             for(int i = 0; i < N1 + 1; i++){
  137.                 System.out.printf("%.3f ", solution[j][i]);
  138.             }
  139.             System.out.println();
  140.         }
  141.     }
  142.  
  143.     private double[][] copyMatrix(double[][] matrix){
  144.         double[][] tempMatrix = new double[N2 + 1][N1 + 1];
  145.         for (int j = 0; j < N2 + 1; j++) {
  146.             for (int i = 0; i < N1 + 1; i++) {
  147.                 tempMatrix[j][i] = matrix[j][i];
  148.             }
  149.         }
  150.         return tempMatrix;
  151.     }
  152.  
  153.     private double[][] differenceMatrix(double[][] matrixA, double[][] matrixB){
  154.         double[][] answer = new double[N2 + 1][N1 + 1];
  155.         for (int j = 0; j < N2 + 1; j++) {
  156.             for (int i = 0; i < N2 + 1; i++) {
  157.                 answer[i][j] = matrixA[j][i] - matrixB[j][i];
  158.             }
  159.         }
  160.         return answer;
  161.     }
  162.  
  163.     private double normMatrix(double[][] matrix){
  164.         double answer = 0;
  165.         double sum;
  166.         for (int j = 0; j < N2 + 1; j++) {
  167.             sum = 0;
  168.             for (int i = 0; i < N1 + 1; i++) {
  169.                 sum += Math.abs(matrix[j][i]);
  170.             }
  171.             answer = Math.max(answer, sum);
  172.         }
  173.         return answer;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement