Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- /**
- * Created by Максим on 06.03.2016.
- */
- public class BoundaryValueProblem extends JFrame {
- double a,b,c,d,e,f,g,h,H;
- int N;
- double[] y;
- BoundaryValueProblem()
- {
- super("Решение краевой задачи");
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- JPanel panel = new JPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
- JLabel label1 = new JLabel("<html>Дана краевая задача:<br>"+
- "y\"-p(x)y'-q(x)y=g(x)<br>");
- label1.setFont(new Font("Courier New", Font.PLAIN, 22));
- Box box = Box.createHorizontalBox();
- box.add(label1);
- int k=0;
- Character[] chars = new Character[11];
- for(char i='a';i<='h';i++,k++) {
- chars[k]=i;
- }
- chars[8]='A';
- chars[9]='B';
- chars[10]='N';
- JTextField fields[] = new JTextField[10];
- for(int i=0; i<10;i++) {
- fields[i] = new JTextField(2);
- fields[i].setText("0");
- fields[i].setMaximumSize(new Dimension(30,20));
- }
- JLabel labels[] = new JLabel[10];
- labels[0] = new JLabel("y(");
- labels[1] = new JLabel(") = ");
- labels[2] = new JLabel("y(");
- labels[3] = new JLabel(") = ");
- labels[4] = new JLabel("p(x) = ");
- labels[5] = new JLabel("x + ");
- labels[6] = new JLabel("q(x) = ");
- labels[7] = new JLabel("x + ");
- labels[8] = new JLabel("g(x) = ");
- labels[9] = new JLabel("x + ");
- JLabel where = new JLabel("где");
- where.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel begin = new JLabel(" начальное условие");
- begin.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel end = new JLabel(" конечное условие");
- end.setFont(new Font("Courier New", Font.PLAIN, 18));
- JLabel enterN = new JLabel(" Введите N:");
- enterN.setFont(new Font("Courier New", Font.PLAIN, 18));
- JTextField fieldN = new JTextField(2);
- fieldN.setMaximumSize(new Dimension(30,20));
- fieldN.setText("0");
- for(int i=0; i<10;i++) {
- labels[i].setFont(new Font("Courier New", Font.PLAIN, 18));
- }
- Box boxes[] = new Box[5];
- for(int i=0; i<5;i++) {
- boxes[i] = Box.createHorizontalBox();
- boxes[i].add(labels[2*i]);
- boxes[i].add(fields[2*i]);
- boxes[i].add(labels[2*i+1]);
- boxes[i].add(fields[2*i+1]);
- boxes[i].setAlignmentX(Component.LEFT_ALIGNMENT);
- }
- Box Nbox = Box.createHorizontalBox();
- Nbox.add(enterN);
- Nbox.add(fieldN);
- Nbox.setAlignmentX(Component.LEFT_ALIGNMENT);
- boxes[0].add(begin);
- boxes[1].add(end);
- JButton ok = new JButton("Рассчитать");
- ok.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent mouseEvent) {
- try {
- a = Double.parseDouble(fields[0].getText());
- b = Double.parseDouble(fields[2].getText());
- c = Double.parseDouble(fields[4].getText());
- d = Double.parseDouble(fields[5].getText());
- e = Double.parseDouble(fields[6].getText());
- f = Double.parseDouble(fields[7].getText());
- g = Double.parseDouble(fields[8].getText());
- h = Double.parseDouble(fields[9].getText());
- N = Integer.parseInt(fieldN.getText());
- y = new double[N+1];
- y[0] = Double.parseDouble(fields[1].getText());
- y[N] = Double.parseDouble(fields[3].getText());
- Solution();
- } catch (ArrayIndexOutOfBoundsException l) {
- JOptionPane.showMessageDialog(null, "Неверные данные");
- l.printStackTrace();}
- }
- });
- panel.add(label1);
- panel.add(Box.createVerticalStrut(5));
- panel.add(boxes[0]);
- panel.add(boxes[1]);
- panel.add(where);
- panel.add(boxes[2]);
- panel.add(boxes[3]);
- panel.add(boxes[4]);
- panel.add(Nbox);
- panel.add(ok);
- setContentPane(panel);
- pack();
- }
- double P(double x)
- {
- return c*x+d;
- }
- double Q(double x)
- {
- return e*x+f;
- }
- double F(double x)
- {
- return g*x+h;
- }
- void Solution() {
- System.out.println(y[0] + " " + y[N]);
- H = (double) (b-a)/N;
- double[] x = new double[N+1];
- double[] A = new double[N+1];
- double[] B = new double[N+1];
- double[] C = new double[N+1];
- double[] F = new double[N+1];
- double[] alf = new double[N+1];
- double[] bet = new double[N+1];
- for (int i = 0; i <= N; i++)
- {
- x[i]=a+i*H;
- }
- alf[1]=y[0];
- bet[1]=y[N];
- for(int i=1;i<N;i++){
- A[i]=0.5*(1-P(x[i])*(H/2));
- B[i]=0.5*(1+P(x[i])*(H/2));
- C[i]=1+((H*H)/2)*Q(x[i]);
- F[i]=((H*H)/2)*F(x[i]);
- }
- for(int i=1;i<N;i++){
- alf[i+1]=(1/(C[i]-A[i]*alf[i]))*B[i];
- bet[i+1]=(1/(C[i]-A[i]*alf[i]))*(F[i]+A[i]*bet[i]);
- }
- for(int i=N-1;i>=1;i--){
- y[i]=alf[i+1]*y[i+1]+bet[i+1];
- }
- for (int i=0;i<N;i++)
- {
- System.out.printf("%.2f \t",y[i] );
- System.out.println();
- }
- Box mainBox = Box.createHorizontalBox();
- Box[] boxes = new Box[N+2];
- boxes[0] = Box.createVerticalBox();
- boxes[0].add(new JLabel("i:"));
- boxes[0].add(new JLabel("x[i]:"));
- boxes[0].add(new JLabel("y[i]:"));
- mainBox.add(boxes[0]);
- mainBox.add(Box.createHorizontalStrut(15));
- for(int i=1;i<=N+1;i++)
- {
- boxes[i] = Box.createVerticalBox();
- boxes[i].add(new JLabel(new Integer(i-1).toString()));
- x[i-1] = x[i-1] * 10;
- int m = (int) Math.round(x[i-1]);
- x[i-1] = (double)m / 10;
- boxes[i].add(new JLabel(new Double(x[i-1]).toString()));
- y[i-1] = y[i-1] * 1000;
- m = (int) Math.round(y[i-1]);
- y[i-1] = (double)m / 1000;
- //System.out.println(X[i - 1]);
- boxes[i].add(new JLabel(new Double(y[i-1]).toString()));
- mainBox.add(boxes[i]);
- mainBox.add(Box.createHorizontalStrut(15));
- }
- setContentPane(mainBox);
- pack();
- mainBox.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment