ModestGenius

BoundaryValueProblem

Mar 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.EmptyBorder;
  5. import javax.swing.border.LineBorder;
  6. import java.awt.*;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.awt.geom.Arc2D;
  11. import java.util.Set;
  12.  
  13. /**
  14.  * Created by Максим on 06.03.2016.
  15.  */
  16. public class BoundaryValueProblem extends JFrame {
  17.     double a,b,c,d,e,f,g,h,H;
  18.     int N;
  19.     double[][] y;
  20.     BoundaryValueProblem()
  21.     {
  22.         super("Решение краевой задачи");
  23.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  24.         Box box0 = Box.createVerticalBox();
  25.         JPanel panel = new JPanel();
  26.         panel.setLayout(new GridLayout(1,3,10,10));
  27.         JLabel label1 = new JLabel("<html>Дана краевая задача:<br>"+
  28.                 "y\"-p(x)y'-q(x)y=g(x)<br>"+"y(a)=A<br>"+
  29.                 "y(b)=B,<br>"+"где <br>"+"p(x)=cx+d<br>"+
  30.                 "q(x)=ex+f<br>"+"g(x)=gx+h<br>");
  31.         label1.setFont(new Font("Courier New", Font.PLAIN, 22));
  32.  
  33.         Box box = Box.createVerticalBox();
  34.         Box box2 = Box.createVerticalBox();
  35.  
  36.         Box[] smallBoxes = new Box[11];
  37.         JTextField[] textFields = new JTextField[11];
  38.         int k=0;
  39.         Character[] chars = new Character[11];
  40.         for(char i='a';i<='h';i++,k++) {
  41.             chars[k]=i;
  42.         }
  43.         chars[8]='A';
  44.         chars[9]='B';
  45.         chars[10]='N';
  46.         for(int i=0; i<11;i++) {
  47.             smallBoxes[i] = Box.createHorizontalBox();
  48.             smallBoxes[i].setBorder(new EmptyBorder(12,12,12,12));
  49.             JLabel label = new JLabel(Character.toString(chars[i])+" = ");
  50.             label.setFont(new Font("Courier New", Font.PLAIN, 18));
  51.             smallBoxes[i].add(label);
  52.             textFields[i] = new JTextField(2);
  53.             textFields[i].setFont(new Font("Courier New", Font.PLAIN, 18));
  54.             textFields[i].setText("0");
  55.             smallBoxes[i].add(textFields[i]);
  56.             if(i<=5)
  57.             box.add(smallBoxes[i]);
  58.             else box2.add(smallBoxes[i]);
  59.         }
  60.         JButton ok = new JButton("Рассчитать");
  61.         ok.addMouseListener(new MouseAdapter() {
  62.             @Override
  63.             public void mouseClicked(MouseEvent mouseEvent) {
  64.                 try {
  65.                     a = Double.parseDouble(textFields[0].getText());
  66.                     b = Double.parseDouble(textFields[1].getText());
  67.                     c = Double.parseDouble(textFields[2].getText());
  68.                     d = Double.parseDouble(textFields[3].getText());
  69.                     e = Double.parseDouble(textFields[4].getText());
  70.                     f = Double.parseDouble(textFields[5].getText());
  71.                     g = Double.parseDouble(textFields[6].getText());
  72.                     h = Double.parseDouble(textFields[7].getText());
  73.                     N = Integer.parseInt(textFields[10].getText());
  74.                     y = new double[N][N + 1];
  75.                     y[0][N] = Double.parseDouble(textFields[8].getText());
  76.                     y[N - 1][N] = Double.parseDouble(textFields[9].getText());
  77.                     Solution();
  78.                } catch (ArrayIndexOutOfBoundsException l) {JOptionPane.showMessageDialog(null, "Неверные данные");}
  79.  
  80.             }
  81.         });
  82.         box2.add(ok);
  83.         panel.add(label1);
  84.         panel.add(box);
  85.         panel.add(box2);
  86.         setContentPane(panel);
  87.         pack();
  88.     }
  89.     double P(double x)
  90.     {
  91.         return c*x+d;
  92.     }
  93.     double Q(double x)
  94.     {
  95.         return e*x+f;
  96.     }
  97.     double F(double x)
  98.     {
  99.         return g*x+h;
  100.     }
  101.     void Solution() {
  102.         H = (double) (b-a)/N;
  103.         double[] x = new double[N];
  104.         double[] X = new double[N+1];
  105.         double[] P = new double[N+1];
  106.         double[] Q = new double[N+1];
  107.  
  108.  
  109.         for (int i = 0; i < N; i++)
  110.         {
  111.             x[i]=a+i*H;
  112.         }
  113.  
  114.         for(int i=1;i<N-1;i++)
  115.             y[i][N]=F(x[i]);
  116.  
  117.  
  118.     //решения начальное и конечное
  119.         X[0]=y[0][N];
  120.         X[N]=y[N-1][N];
  121.  
  122.         y[0][0]=-2-H*H*Q(x[1]);
  123.         y[0][1]=1+(P(x[1])*H)/2;
  124.         y[0][N]=H*H*F(x[1]) - (1-(P(x[1])*H)/2);
  125.  
  126.         y[N-1][N-1]=1;
  127.         int l = 1;
  128.         while(l<N-1)
  129.         {
  130.             y[l][l-1] = 1 - (P(x[l+1])*H)/2;
  131.             y[l][l]=-2 - H*H*Q(x[l+1]);
  132.             y[l][l+1]= 1 + (P(x[l+1])*H)/2;
  133.             l++;
  134.         }
  135.         P[0]=-y[0][1]/y[0][0];
  136.         Q[0]=y[0][N]/y[0][0];
  137.  
  138.         for(int i=1;i<N-1;i++) {
  139.             P[i] = -y[i][i + 1] / (y[i][i] + y[i][i - 1] * P[i - 1]);
  140.             Q[i] = (y[i][i+2]-y[i][i-1]*Q[i-1])/(y[i][i] + y[i][i - 1] * P[i - 1]);
  141.         }
  142.         for(int i=N-2;i>=0;i--) {
  143.             X[i+1] = P[i] * X[i + 2] + Q[i];
  144.         }
  145.  
  146.        /* for (int i=0;i<N;i++)
  147.         {
  148.             for(int k=0;k<=N;k++)
  149.             {
  150.                 System.out.printf("%.2f \t",y[i][k] );
  151.             }
  152.             System.out.println();
  153.         }*/
  154.         Box box = Box.createHorizontalBox();
  155.         box.setBorder(new EmptyBorder(12,12,12,12));
  156.         for (int i=0;i<=N;i++)
  157.         {
  158.             X[i] = X[i] * 1000;
  159.             int m = (int) Math.round(X[i]);
  160.             X[i] = (double)m / 1000;
  161.             System.out.println(X[i]);
  162.             JLabel label = new JLabel(new Double(X[i]).toString());
  163.             box.add(label);
  164.             box.add(Box.createHorizontalStrut(15));
  165.         }
  166.         setContentPane(box);
  167.         pack();
  168.         box.setVisible(true);
  169.  
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment