ModestGenius

Untitled

Apr 15th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.98 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7.  
  8. /**
  9.  * Created by Максим on 06.03.2016.
  10.  */
  11. public class BoundaryValueProblem extends JFrame {
  12.     double a,b,c,d,e,f,g,h,H;
  13.     int N;
  14.     double[] y;
  15.  
  16.     BoundaryValueProblem()
  17.     {
  18.         super("Решение краевой задачи");
  19.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  20.         JPanel panel = new JPanel();
  21.         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  22.         JLabel label1 = new JLabel("<html>Дана краевая задача:<br>"+
  23.                 "y\"-p(x)y'-q(x)y=g(x)<br>");
  24.         label1.setFont(new Font("Courier New", Font.PLAIN, 22));
  25.  
  26.         Box box = Box.createHorizontalBox();
  27.         box.add(label1);
  28.         int k=0;
  29.         Character[] chars = new Character[11];
  30.         for(char i='a';i<='h';i++,k++) {
  31.             chars[k]=i;
  32.         }
  33.         chars[8]='A';
  34.         chars[9]='B';
  35.         chars[10]='N';
  36.  
  37.         JTextField fields[] = new JTextField[10];
  38.  
  39.         for(int i=0; i<10;i++) {
  40.             fields[i] = new JTextField(2);
  41.             fields[i].setText("0");
  42.             fields[i].setMaximumSize(new Dimension(30,20));
  43.         }
  44.  
  45.         JLabel labels[] = new JLabel[10];
  46.         labels[0] = new JLabel("y(");
  47.         labels[1] = new JLabel(") = ");
  48.         labels[2] = new JLabel("y(");
  49.         labels[3] = new JLabel(") = ");
  50.         labels[4] = new JLabel("p(x) = ");
  51.         labels[5] = new JLabel("x + ");
  52.         labels[6] = new JLabel("q(x) = ");
  53.         labels[7] = new JLabel("x + ");
  54.         labels[8] = new JLabel("g(x) = ");
  55.         labels[9] = new JLabel("x + ");
  56.  
  57.         JLabel where = new JLabel("где");
  58.         where.setFont(new Font("Courier New", Font.PLAIN, 18));
  59.  
  60.         JLabel begin = new JLabel(" начальное условие");
  61.         begin.setFont(new Font("Courier New", Font.PLAIN, 18));
  62.  
  63.         JLabel end = new JLabel(" конечное условие");
  64.         end.setFont(new Font("Courier New", Font.PLAIN, 18));
  65.  
  66.         JLabel enterN = new JLabel(" Введите N:");
  67.         enterN.setFont(new Font("Courier New", Font.PLAIN, 18));
  68.  
  69.         JTextField fieldN = new JTextField(2);
  70.         fieldN.setMaximumSize(new Dimension(30,20));
  71.         fieldN.setText("0");
  72.  
  73.         for(int i=0; i<10;i++) {
  74.             labels[i].setFont(new Font("Courier New", Font.PLAIN, 18));
  75.         }
  76.  
  77.  
  78.         Box boxes[] = new Box[5];
  79.         for(int i=0; i<5;i++) {
  80.             boxes[i] = Box.createHorizontalBox();
  81.             boxes[i].add(labels[2*i]);
  82.             boxes[i].add(fields[2*i]);
  83.             boxes[i].add(labels[2*i+1]);
  84.             boxes[i].add(fields[2*i+1]);
  85.             boxes[i].setAlignmentX(Component.LEFT_ALIGNMENT);
  86.         }
  87.         Box Nbox = Box.createHorizontalBox();
  88.         Nbox.add(enterN);
  89.         Nbox.add(fieldN);
  90.         Nbox.setAlignmentX(Component.LEFT_ALIGNMENT);
  91.  
  92.         boxes[0].add(begin);
  93.         boxes[1].add(end);
  94.  
  95.         JButton ok = new JButton("Рассчитать");
  96.         ok.addMouseListener(new MouseAdapter() {
  97.             @Override
  98.             public void mouseClicked(MouseEvent mouseEvent) {
  99.                 try {
  100.                     a = Double.parseDouble(fields[0].getText());
  101.                     b = Double.parseDouble(fields[2].getText());
  102.                     c = Double.parseDouble(fields[4].getText());
  103.                     d = Double.parseDouble(fields[5].getText());
  104.                     e = Double.parseDouble(fields[6].getText());
  105.                     f = Double.parseDouble(fields[7].getText());
  106.                     g = Double.parseDouble(fields[8].getText());
  107.                     h = Double.parseDouble(fields[9].getText());
  108.                     N = Integer.parseInt(fieldN.getText());
  109.                     y = new double[N+1];
  110.                     y[0] = Double.parseDouble(fields[1].getText());
  111.                     y[N] = Double.parseDouble(fields[3].getText());
  112.                     Solution();
  113.                 } catch (ArrayIndexOutOfBoundsException l) {
  114.                     JOptionPane.showMessageDialog(null, "Неверные данные");
  115.                     l.printStackTrace();}
  116.             }
  117.         });
  118.         panel.add(label1);
  119.         panel.add(Box.createVerticalStrut(5));
  120.         panel.add(boxes[0]);
  121.         panel.add(boxes[1]);
  122.         panel.add(where);
  123.         panel.add(boxes[2]);
  124.         panel.add(boxes[3]);
  125.         panel.add(boxes[4]);
  126.         panel.add(Nbox);
  127.         panel.add(ok);
  128.         setContentPane(panel);
  129.         pack();
  130.     }
  131.     double P(double x)
  132.     {
  133.         return c*x+d;
  134.     }
  135.     double Q(double x)
  136.     {
  137.         return e*x+f;
  138.     }
  139.     double F(double x)
  140.     {
  141.         return g*x+h;
  142.     }
  143.  
  144.     void Solution() {
  145.         System.out.println(y[0] + " " + y[N]);
  146.         H = (double) (b-a)/N;
  147.         double[] x = new double[N+1];
  148.         double[] A = new double[N+1];
  149.         double[] B = new double[N+1];
  150.         double[] C = new double[N+1];
  151.         double[] F = new double[N+1];
  152.         double[] alf = new double[N+1];
  153.         double[] bet = new double[N+1];
  154.  
  155.         for (int i = 0; i <= N; i++)
  156.         {
  157.             x[i]=a+i*H;
  158.         }
  159.         alf[1]=y[0];
  160.         bet[1]=y[N];
  161.         for(int i=1;i<N;i++){
  162.             A[i]=0.5*(1-P(x[i])*(H/2));
  163.             B[i]=0.5*(1+P(x[i])*(H/2));
  164.             C[i]=1+((H*H)/2)*Q(x[i]);
  165.             F[i]=((H*H)/2)*F(x[i]);
  166.         }
  167.         for(int i=1;i<N;i++){
  168.  
  169.             alf[i+1]=(1/(C[i]-A[i]*alf[i]))*B[i];
  170.             bet[i+1]=(1/(C[i]-A[i]*alf[i]))*(F[i]+A[i]*bet[i]);
  171.         }
  172.         for(int i=N-1;i>=1;i--){
  173.             y[i]=alf[i+1]*y[i+1]+bet[i+1];
  174.         }
  175.        
  176.         for (int i=0;i<N;i++)
  177.         {
  178.             System.out.printf("%.2f \t",y[i] );
  179.             System.out.println();
  180.         }
  181.         Box mainBox = Box.createHorizontalBox();
  182.         Box[] boxes = new Box[N+2];
  183.         boxes[0] = Box.createVerticalBox();
  184.         boxes[0].add(new JLabel("i:"));
  185.         boxes[0].add(new JLabel("x[i]:"));
  186.         boxes[0].add(new JLabel("y[i]:"));
  187.         mainBox.add(boxes[0]);
  188.         mainBox.add(Box.createHorizontalStrut(15));
  189.         for(int i=1;i<=N+1;i++)
  190.         {
  191.             boxes[i] = Box.createVerticalBox();
  192.             boxes[i].add(new JLabel(new Integer(i-1).toString()));
  193.  
  194.             x[i-1] = x[i-1] * 10;
  195.             int m = (int) Math.round(x[i-1]);
  196.             x[i-1] = (double)m / 10;
  197.             boxes[i].add(new JLabel(new Double(x[i-1]).toString()));
  198.  
  199.             y[i-1] = y[i-1] * 1000;
  200.             m = (int) Math.round(y[i-1]);
  201.             y[i-1] = (double)m / 1000;
  202.             //System.out.println(X[i - 1]);
  203.             boxes[i].add(new JLabel(new Double(y[i-1]).toString()));
  204.             mainBox.add(boxes[i]);
  205.             mainBox.add(Box.createHorizontalStrut(15));
  206.         }
  207.         setContentPane(mainBox);
  208.         pack();
  209.         mainBox.setVisible(true);
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment