Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import com.sun.org.apache.xpath.internal.SourceTree;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- /**
- * Created by Максим on 22.04.2016.
- */
- public class ExplicitSchemeParabolic extends JFrame {
- double a, b, c, d, e, f, g, T, l, h, tau;
- int nx, nt;
- double[][] u;
- double[] x, t;
- ExplicitSchemeParabolic() {
- super("Решение уравнения параболического типа (явная схема)");
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- JPanel panel = new JPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
- JLabel label1 = new JLabel("<html>Дано уравнение параболического типа:");
- label1.setFont(new Font("Courier New", Font.PLAIN, 22));
- JLabel label2 = new JLabel("∂u/∂t = (");
- label2.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel label3 = new JLabel("^2) *(∂^2u/∂x^2)<br>");
- label3.setFont(new Font("Courier New", Font.PLAIN, 18));
- Box firstBox = Box.createHorizontalBox();
- firstBox.add(label1);
- Box addBox = Box.createHorizontalBox();
- addBox.add(label2);
- JTextField A = new JTextField(2);
- A.setText("0");
- A.setMaximumSize(new Dimension(30, 20));
- addBox.add(A);
- addBox.add(label3);
- JTextField B = new JTextField(2);
- B.setText("0");
- B.setMaximumSize(new Dimension(30, 20));
- JTextField C = new JTextField(2);
- C.setText("0");
- C.setMaximumSize(new Dimension(30, 20));
- JTextField L = new JTextField(2);
- L.setText("0");
- L.setMaximumSize(new Dimension(30, 20));
- JTextField D = new JTextField(2);
- D.setText("0");
- D.setMaximumSize(new Dimension(30, 20));
- JTextField E = new JTextField(2);
- E.setText("0");
- E.setMaximumSize(new Dimension(30, 20));
- JTextField F = new JTextField(2);
- F.setText("0");
- F.setMaximumSize(new Dimension(30, 20));
- JTextField G = new JTextField(2);
- G.setText("0");
- G.setMaximumSize(new Dimension(30, 20));
- JLabel borderConditions = new JLabel("Граничные условия:");
- borderConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
- Box secondBox = Box.createHorizontalBox();
- secondBox.add(borderConditions);
- JLabel labels[] = new JLabel[7];
- labels[0] = new JLabel("u(0,t) = ");
- labels[1] = new JLabel("t + ");
- labels[2] = new JLabel("u(");
- labels[3] = new JLabel(",t) = ");
- labels[4] = new JLabel("t + ");
- JLabel initialConditions = new JLabel("Начальные условия:");
- initialConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
- Box thirdBox = Box.createHorizontalBox();
- thirdBox.add(initialConditions);
- labels[5] = new JLabel("u(x,0) = ");
- labels[6] = new JLabel("x + ");
- JLabel lCountOX = new JLabel("Количество узлов для x:");
- lCountOX.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel lCountOT = new JLabel("Количество узлов для t:");
- lCountOT.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel lRightBorder = new JLabel("Правая граница для t:");
- lRightBorder.setFont(new Font("Courier New", Font.PLAIN, 18));
- JTextField countOX = new JTextField(2);
- countOX.setMaximumSize(new Dimension(30, 20));
- countOX.setText("0");
- JTextField countOT = new JTextField(2);
- countOT.setMaximumSize(new Dimension(30, 20));
- countOT.setText("0");
- JTextField rightBorder = new JTextField(2);
- rightBorder.setMaximumSize(new Dimension(30, 20));
- rightBorder.setText("0");
- for (int i = 0; i < 7; i++) {
- labels[i].setFont(new Font("Courier New", Font.PLAIN, 18));
- }
- borderConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
- initialConditions.setFont(new Font("Courier New", Font.PLAIN, 18));
- lRightBorder.setFont(new Font("Courier New", Font.PLAIN, 18));
- lCountOT.setFont(new Font("Courier New", Font.PLAIN, 18));
- lCountOX.setFont(new Font("Courier New", Font.PLAIN, 18));
- Box box1 = Box.createHorizontalBox();
- box1.add(labels[0]);
- box1.add(B);
- box1.add(labels[1]);
- box1.add(C);
- Box box2 = Box.createHorizontalBox();
- box2.add(labels[2]);
- box2.add(L);
- box2.add(labels[3]);
- box2.add(D);
- box2.add(labels[4]);
- box2.add(E);
- Box box3 = Box.createHorizontalBox();
- box3.add(labels[5]);
- box3.add(F);
- box3.add(labels[6]);
- box3.add(G);
- Box box4 = Box.createHorizontalBox();
- box4.add(lCountOX);
- box4.add(countOX);
- Box box5 = Box.createHorizontalBox();
- box5.add(lCountOT);
- box5.add(countOT);
- Box box6 = Box.createHorizontalBox();
- box6.add(lRightBorder);
- box6.add(rightBorder);
- JButton ok = new JButton("Рассчитать");
- ok.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent mouseEvent) {
- try {
- a = Double.parseDouble(A.getText());
- b = Double.parseDouble(B.getText());
- c = Double.parseDouble(C.getText());
- l = Double.parseDouble(L.getText());
- d = Double.parseDouble(D.getText());
- e = Double.parseDouble(E.getText());
- f = Double.parseDouble(F.getText());
- g = Double.parseDouble(G.getText());
- nx = Integer.parseInt(countOX.getText());
- nt = Integer.parseInt(countOT.getText());
- T = Double.parseDouble(rightBorder.getText());
- Solution();
- } catch (ArrayIndexOutOfBoundsException l) {
- JOptionPane.showMessageDialog(null, "Неверные данные");
- l.printStackTrace();
- }
- }
- });
- firstBox.setAlignmentX(Component.LEFT_ALIGNMENT);
- addBox.setAlignmentX(Component.LEFT_ALIGNMENT);
- secondBox.setAlignmentX(Component.LEFT_ALIGNMENT);
- thirdBox.setAlignmentX(Component.LEFT_ALIGNMENT);
- box1.setAlignmentX(Component.LEFT_ALIGNMENT);
- box2.setAlignmentX(Component.LEFT_ALIGNMENT);
- box3.setAlignmentX(Component.LEFT_ALIGNMENT);
- box4.setAlignmentX(Component.LEFT_ALIGNMENT);
- box5.setAlignmentX(Component.LEFT_ALIGNMENT);
- box6.setAlignmentX(Component.LEFT_ALIGNMENT);
- panel.add(firstBox);
- panel.add(addBox);
- panel.add(secondBox);
- panel.add(box1);
- panel.add(box2);
- panel.add(thirdBox);
- panel.add(box3);
- panel.add(box4);
- panel.add(box6);
- panel.add(box5);
- panel.add(ok);
- setContentPane(panel);
- pack();
- }
- public double phi0 (double x, double t)
- {
- return 4 * Math.exp(-25 * t);
- }
- public double phil (double x, double t)
- {
- return 4 * Math.exp(-25 * t);
- }
- public double psi (double x)
- {
- return 3 * Math.sin(x) + 4 * Math.cos(5 * x);
- }
- public double sol (double x, double t)
- {
- return 3 * Math.exp(-t) * Math.sin(x) + 4 * Math.exp(-25 * t) * Math.cos(5 * x);
- }
- double P(double t) { //для 0
- return b * t*t + c;
- }
- double Q(double t) { // для l
- return d * t*t + e;
- }
- double F(double x) {
- return f * x * x + g;
- }
- ExplicitSchemeParabolic(double l, double T, int nx, int nt, double a, double b,double c,double d,double e, double f,double g) {
- super("Решение уравнения параболического типа (явная схема)");
- this.l=l;
- this.T=T;
- this.nx=nx;
- this.nt=nt;
- this.a=a;
- this.b=b;
- this.c=c;
- this.d=d;
- this.e=e;
- this.f=f;
- this.g=g;
- Solution();
- }
- void Solution() {
- double a1, a2, a3;
- int i, j;
- h = l/nx;
- tau = T/nt;
- x = new double[nx + 1];
- t = new double[nt + 1];
- u = new double[nx + 1][nt + 1];
- x[0] = 0;
- t[0] = 0;
- for (i = 1; i <= nx; i++) {
- x[i] = x[i - 1] + h;
- }
- for (i = 1; i <= nt; i++)
- t[i] = t[i - 1] + tau;
- for (j = 0; j <= nt; j++) {
- u[0][j] = phi0(0, t[j]);
- u[nx][j] = phil(l, t[j]);
- }
- for (i = 0; i <= nx; i++)
- u[i][0] = psi(x[i]);
- double sigma = a * a * tau / (h * h);
- for (i = 0; i < nt; i++)
- for (j = 1; j < nx; j++) {
- 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);
- }
- JPanel jpanel = new JPanel(new GridLayout(nt/50+1,nx+1));
- /* for (j = 0; j <= nt; j++) {
- for (i = 0; i <= nx; i++) {
- System.out.printf("%.3f \t", u[i][j]);
- System.out.print(" ");
- }
- System.out.println();
- }*/
- for (j = 0; j <= nt; j++) {
- if (j % 10000 == 0) {
- for (i = 0; i <= nx; i++) {
- jpanel.add(new JLabel(String.format("%.3f", u[i][j])));
- jpanel.add(Box.createHorizontalStrut(15));
- //System.out.printf("%.3f \t", u[i][j]);
- }
- // System.out.println();
- }
- }
- double mod = 0;
- int k=0;
- System.out.println("_________________________________________");
- for (j = 0; j <= nt; j++) {
- for (i = 0; i <= nx; i++) {
- u[i][j] = u[i][j]-sol(x[i],t[j]);
- mod += Math.abs(u[i][j]);
- k++;
- }
- }
- for (j = 0; j <= nt; j++) {
- if (j % 10000 == 0) {
- for (i = 0; i <= nx; i++) {
- jpanel.add(new JLabel(String.format("%.3f", u[i][j])));
- jpanel.add(Box.createHorizontalStrut(15));
- //System.out.printf("%.3f \t", u[i][j]);
- }
- //System.out.println();
- }
- }
- System.out.println("DIFFERNCE:" + mod/k);
- setContentPane(jpanel);
- pack();
- jpanel.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment