ModestGenius

Explicit[OLD]

May 13th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.84 KB | None | 0 0
  1. package com.company;
  2.  
  3. import com.sun.org.apache.xpath.internal.SourceTree;
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9.  
  10. /**
  11.  * Created by Максим on 22.04.2016.
  12.  */
  13. public class ExplicitSchemeParabolic extends JFrame {
  14.     double a, b, c, d, e, f, g, T, l, h, tau;
  15.     int nx, nt;
  16.     double[][] u;
  17.     double[] x, t;
  18.  
  19.     ExplicitSchemeParabolic() {
  20.         super("Решение уравнения параболического типа (явная схема)");
  21.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  22.         JPanel panel = new JPanel();
  23.         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  24.         JLabel label1 = new JLabel("<html>Дано уравнение параболического типа:");
  25.         label1.setFont(new Font("Courier New", Font.PLAIN, 22));
  26.         JLabel label2 = new JLabel("∂u/∂t = (");
  27.         label2.setFont(new Font("Courier New", Font.PLAIN, 18));
  28.         JLabel label3 = new JLabel("^2) *(∂^2u/∂x^2)<br>");
  29.         label3.setFont(new Font("Courier New", Font.PLAIN, 18));
  30.  
  31.         Box firstBox = Box.createHorizontalBox();
  32.         firstBox.add(label1);
  33.         Box addBox = Box.createHorizontalBox();
  34.         addBox.add(label2);
  35.         JTextField A = new JTextField(2);
  36.         A.setText("0");
  37.         A.setMaximumSize(new Dimension(30, 20));
  38.         addBox.add(A);
  39.         addBox.add(label3);
  40.         JTextField B = new JTextField(2);
  41.         B.setText("0");
  42.         B.setMaximumSize(new Dimension(30, 20));
  43.         JTextField C = new JTextField(2);
  44.         C.setText("0");
  45.         C.setMaximumSize(new Dimension(30, 20));
  46.         JTextField L = new JTextField(2);
  47.         L.setText("0");
  48.         L.setMaximumSize(new Dimension(30, 20));
  49.         JTextField D = new JTextField(2);
  50.         D.setText("0");
  51.         D.setMaximumSize(new Dimension(30, 20));
  52.         JTextField E = new JTextField(2);
  53.         E.setText("0");
  54.         E.setMaximumSize(new Dimension(30, 20));
  55.         JTextField F = new JTextField(2);
  56.         F.setText("0");
  57.         F.setMaximumSize(new Dimension(30, 20));
  58.         JTextField G = new JTextField(2);
  59.         G.setText("0");
  60.         G.setMaximumSize(new Dimension(30, 20));
  61.  
  62.         JLabel borderConditions = new JLabel("Граничные условия:");
  63.         borderConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
  64.         Box secondBox = Box.createHorizontalBox();
  65.         secondBox.add(borderConditions);
  66.  
  67.         JLabel labels[] = new JLabel[7];
  68.         labels[0] = new JLabel("u(0,t) = ");
  69.         labels[1] = new JLabel("t + ");
  70.         labels[2] = new JLabel("u(");
  71.         labels[3] = new JLabel(",t) = ");
  72.         labels[4] = new JLabel("t + ");
  73.         JLabel initialConditions = new JLabel("Начальные условия:");
  74.         initialConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
  75.         Box thirdBox = Box.createHorizontalBox();
  76.         thirdBox.add(initialConditions);
  77.         labels[5] = new JLabel("u(x,0) = ");
  78.         labels[6] = new JLabel("x + ");
  79.  
  80.  
  81.         JLabel lCountOX = new JLabel("Количество узлов для x:");
  82.         lCountOX.setFont(new Font("Courier New", Font.PLAIN, 18));
  83.  
  84.         JLabel lCountOT = new JLabel("Количество узлов для t:");
  85.         lCountOT.setFont(new Font("Courier New", Font.PLAIN, 18));
  86.  
  87.         JLabel lRightBorder = new JLabel("Правая граница для t:");
  88.         lRightBorder.setFont(new Font("Courier New", Font.PLAIN, 18));
  89.  
  90.         JTextField countOX = new JTextField(2);
  91.         countOX.setMaximumSize(new Dimension(30, 20));
  92.         countOX.setText("0");
  93.         JTextField countOT = new JTextField(2);
  94.         countOT.setMaximumSize(new Dimension(30, 20));
  95.         countOT.setText("0");
  96.         JTextField rightBorder = new JTextField(2);
  97.         rightBorder.setMaximumSize(new Dimension(30, 20));
  98.         rightBorder.setText("0");
  99.  
  100.         for (int i = 0; i < 7; i++) {
  101.             labels[i].setFont(new Font("Courier New", Font.PLAIN, 18));
  102.         }
  103.         borderConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
  104.         initialConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
  105.         lRightBorder.setFont(new Font("Courier New", Font.PLAIN, 18));
  106.         lCountOT.setFont(new Font("Courier New", Font.PLAIN, 18));
  107.         lCountOX.setFont(new Font("Courier New", Font.PLAIN, 18));
  108.  
  109.  
  110.         Box box1 = Box.createHorizontalBox();
  111.         box1.add(labels[0]);
  112.         box1.add(B);
  113.         box1.add(labels[1]);
  114.         box1.add(C);
  115.  
  116.         Box box2 = Box.createHorizontalBox();
  117.         box2.add(labels[2]);
  118.         box2.add(L);
  119.         box2.add(labels[3]);
  120.         box2.add(D);
  121.         box2.add(labels[4]);
  122.         box2.add(E);
  123.  
  124.         Box box3 = Box.createHorizontalBox();
  125.         box3.add(labels[5]);
  126.         box3.add(F);
  127.         box3.add(labels[6]);
  128.         box3.add(G);
  129.  
  130.         Box box4 = Box.createHorizontalBox();
  131.         box4.add(lCountOX);
  132.         box4.add(countOX);
  133.  
  134.         Box box5 = Box.createHorizontalBox();
  135.         box5.add(lCountOT);
  136.         box5.add(countOT);
  137.  
  138.         Box box6 = Box.createHorizontalBox();
  139.         box6.add(lRightBorder);
  140.         box6.add(rightBorder);
  141.  
  142.  
  143.         JButton ok = new JButton("Рассчитать");
  144.         ok.addMouseListener(new MouseAdapter() {
  145.             @Override
  146.             public void mouseClicked(MouseEvent mouseEvent) {
  147.                 try {
  148.                     a = Double.parseDouble(A.getText());
  149.                     b = Double.parseDouble(B.getText());
  150.                     c = Double.parseDouble(C.getText());
  151.                     l = Double.parseDouble(L.getText());
  152.                     d = Double.parseDouble(D.getText());
  153.                     e = Double.parseDouble(E.getText());
  154.                     f = Double.parseDouble(F.getText());
  155.                     g = Double.parseDouble(G.getText());
  156.                     nx = Integer.parseInt(countOX.getText());
  157.                     nt = Integer.parseInt(countOT.getText());
  158.                     T = Double.parseDouble(rightBorder.getText());
  159.                     Solution();
  160.                 } catch (ArrayIndexOutOfBoundsException l) {
  161.                     JOptionPane.showMessageDialog(null, "Неверные данные");
  162.                     l.printStackTrace();
  163.                 }
  164.             }
  165.         });
  166.         firstBox.setAlignmentX(Component.LEFT_ALIGNMENT);
  167.         addBox.setAlignmentX(Component.LEFT_ALIGNMENT);
  168.         secondBox.setAlignmentX(Component.LEFT_ALIGNMENT);
  169.         thirdBox.setAlignmentX(Component.LEFT_ALIGNMENT);
  170.         box1.setAlignmentX(Component.LEFT_ALIGNMENT);
  171.         box2.setAlignmentX(Component.LEFT_ALIGNMENT);
  172.         box3.setAlignmentX(Component.LEFT_ALIGNMENT);
  173.         box4.setAlignmentX(Component.LEFT_ALIGNMENT);
  174.         box5.setAlignmentX(Component.LEFT_ALIGNMENT);
  175.         box6.setAlignmentX(Component.LEFT_ALIGNMENT);
  176.         panel.add(firstBox);
  177.         panel.add(addBox);
  178.         panel.add(secondBox);
  179.         panel.add(box1);
  180.         panel.add(box2);
  181.         panel.add(thirdBox);
  182.         panel.add(box3);
  183.         panel.add(box4);
  184.         panel.add(box6);
  185.         panel.add(box5);
  186.         panel.add(ok);
  187.         setContentPane(panel);
  188.         pack();
  189.     }
  190.     public double phi0 (double x, double t)
  191.     {
  192.         return 4 * Math.exp(-25 * t);
  193.     }
  194.  
  195.     public double phil (double x, double t)
  196.     {
  197.         return 4 * Math.exp(-25 * t);
  198.     }
  199.  
  200.     public double psi (double x)
  201.     {
  202.         return 3 * Math.sin(x) + 4 * Math.cos(5 * x);
  203.     }
  204.     public double sol (double x, double t)
  205.     {
  206.         return 3 * Math.exp(-t) * Math.sin(x) + 4 * Math.exp(-25 * t) * Math.cos(5 * x);
  207.     }
  208.     double P(double t) { //для 0
  209.         return b * t*t + c;
  210.     }
  211.  
  212.     double Q(double t) { // для l
  213.         return d * t*t  + e;
  214.     }
  215.  
  216.     double F(double x) {
  217.         return f * x * x + g;
  218.     }
  219.     ExplicitSchemeParabolic(double l, double T, int nx, int nt, double a, double b,double c,double d,double e, double f,double g) {
  220.         super("Решение уравнения параболического типа (явная схема)");
  221.         this.l=l;
  222.         this.T=T;
  223.         this.nx=nx;
  224.         this.nt=nt;
  225.         this.a=a;
  226.         this.b=b;
  227.         this.c=c;
  228.         this.d=d;
  229.         this.e=e;
  230.         this.f=f;
  231.         this.g=g;
  232.         Solution();
  233.     }
  234.     void Solution() {
  235.         double a1, a2, a3;
  236.         int i, j;
  237.         h = l/nx;
  238.         tau = T/nt;
  239.         x = new double[nx + 1];
  240.         t = new double[nt + 1];
  241.         u = new double[nx + 1][nt + 1];
  242.         x[0] = 0;
  243.         t[0] = 0;
  244.         for (i = 1; i <= nx; i++) {
  245.             x[i] = x[i - 1] + h;
  246.         }
  247.         for (i = 1; i <= nt; i++)
  248.             t[i] = t[i - 1] + tau;
  249.  
  250.         for (j = 0; j <= nt; j++) {
  251.             u[0][j] = phi0(0, t[j]);
  252.             u[nx][j] = phil(l, t[j]);
  253.         }
  254.         for (i = 0; i <= nx; i++)
  255.             u[i][0] = psi(x[i]);
  256.         double sigma = a * a * tau / (h * h);
  257.         for (i = 0; i < nt; i++)
  258.             for (j = 1; j < nx; j++) {
  259.                 u[j][i + 1] = sigma * u[j + 1][i] + (1-2*sigma) * u[j][i] + sigma * u[j - 1][i]+tau*(2*t[i]-2);
  260.             }
  261.         JPanel jpanel = new JPanel(new GridLayout(nt/50+1,nx+1));
  262.  
  263.        /* for (j = 0; j <= nt; j++) {
  264.             for (i = 0; i <= nx; i++) {
  265.                 System.out.printf("%.3f \t", u[i][j]);
  266.                 System.out.print(" ");
  267.             }
  268.             System.out.println();
  269.         }*/
  270.  
  271.         for (j = 0; j <= nt; j++) {
  272.             if (j % 10000 == 0) {
  273.                 for (i = 0; i <= nx; i++) {
  274.                     jpanel.add(new JLabel(String.format("%.3f", u[i][j])));
  275.                     jpanel.add(Box.createHorizontalStrut(15));
  276.                     //System.out.printf("%.3f \t", u[i][j]);
  277.                 }
  278.                // System.out.println();
  279.             }
  280.         }
  281.  
  282.         double mod = 0;
  283.         int k=0;
  284.         System.out.println("_________________________________________");
  285.         for (j = 0; j <= nt; j++) {
  286.                 for (i = 0; i <= nx; i++) {
  287.                     u[i][j] = u[i][j]-sol(x[i],t[j]);
  288.                     mod += Math.abs(u[i][j]);
  289.                     k++;
  290.             }
  291.         }
  292.         for (j = 0; j <= nt; j++) {
  293.             if (j % 10000 == 0) {
  294.                 for (i = 0; i <= nx; i++) {
  295.                     jpanel.add(new JLabel(String.format("%.3f", u[i][j])));
  296.                     jpanel.add(Box.createHorizontalStrut(15));
  297.                     //System.out.printf("%.3f \t", u[i][j]);
  298.                 }
  299.                 //System.out.println();
  300.             }
  301.         }
  302.         System.out.println("DIFFERNCE:" + mod/k);
  303.         setContentPane(jpanel);
  304.         pack();
  305.         jpanel.setVisible(true);
  306.     }
  307. }
Add Comment
Please, Sign In to add comment