Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class scheme {
- protected int N, M, n;
- protected double h, tau, a;
- protected double[][] temp;
- protected double[] U1;
- protected scheme(double h, double tau) {
- this.h = h;
- this.tau = tau;
- this.N = (int)(1/h);
- this.M = (int)(1/tau);
- temp = new double[N+1][M+1];
- }
- protected abstract void run();
- protected void paint() {
- if(n == 0) { n = 11;}
- double[] U = new double[n];
- double[] T = new double[n];
- for(int i = 0; i < n; i++) {
- U[i] = temp[(int)(N/2)][i*M/(n-1)];
- T[i] = (M/(n-1))*tau*i;
- System.out.print(U[i]);
- System.out.print(' ');
- System.out.println(U1[i]);
- }
- int[] X = new int[n];
- for(int i = 0; i < n; i++) {
- X[i] = (int) (440*T[i]) + 20;
- }
- double Umax = U[0];
- double Umin = U[0];
- for(int i = 0; i < n; i++) {
- if(Umax < U[i]) {Umax = U[i];}
- if(Umax < U1[i]) {Umax = U1[i];}
- if(Umin > U[i]) {Umin = U[i];}
- if(Umin > U1[i]) {Umin = U1[i];}
- }
- myFrame.n = n;
- myFrame.y = doubleToInt(U, n, Umin, Umax);
- myFrame.y1 = doubleToInt(U1, n, Umin, Umax);
- myFrame.x = X;
- new myFrame().setVisible(true);
- }
- private int[] doubleToInt(double[] U, int n, double min, double max) {
- int[] Y = new int[n];
- for(int i = 0; i < n; i++) {
- Y[i] = 340 - (int) (320*(U[i] - min)/(max - min));
- }
- return Y;
- }
- protected void error() {
- double error = 0;
- for(int i = 0; i < N+1; i++) {
- for(int j = 0; j < M+1; j++) {
- double er = Math.abs(temp[i][j] - solution(j*tau, i*h));
- if(er > error) {
- error = er;
- }
- }
- }
- System.out.println(error);
- }
- protected abstract double f(double t, double x);
- protected abstract double solution(double t, double x);
- }
- /////////////////////////////////////////////////////////////
- public class exTerm extends scheme{
- public exTerm(double h, double tau) {
- super(h, tau);
- a = 0.037;
- n = 11;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- for(int i = 1; i < N; i++) {
- temp[i][j] = temp[i][j-1] + a*(temp[i+1][j-1] - 2*temp[i][j-1] + temp[i-1][j-1])*tau/(h*h) + tau*f((j-1)*tau, i*h);
- }
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- @Override
- protected double f(double t, double x) {
- double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
- return result;
- }
- }
- //////////////////////////////////////////////////////////////////
- public class imTerm extends scheme{
- private double A, B, C;
- protected imTerm(double h, double tau) {
- super(h, tau);
- a = 0.034;
- n = 11;
- A = a*tau/(h*h);
- B = (-1)*(1 + 2*a*tau/(h*h));
- C = A;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- double[] F = new double[N-1];
- for(int i = 0; i < N-1; i++) {
- F[i] = (-1)*(temp[i+1][j-1] + tau*f(j*tau, i*h));
- }
- F[0] -= solution(tau*j, 0)*A;
- F[N-2] -= solution(tau*j, 1)*A;
- calc(F, j);
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- private void calc(double[] F, int j) {
- int n = N-1;
- double[] p = new double[n-1], q = new double[n-1], X = new double[n];
- p[0] = C/B;
- q[0] = F[0]/B;
- for(int i = 1; i < n-1; i++) {
- p[i] = C/(B - p[i-1]*A);
- q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
- }
- X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
- for(int i = n-2; i >= 0; i--) {
- X[i] = q[i] - p[i]*X[i+1];
- }
- for(int i = 1; i < N; i++) {
- temp[i][j] = X[i-1];
- }
- }
- @Override
- protected double f(double t, double x) {
- double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
- return result;
- }
- }
- /////////////////////////////////////////////////////////////////////
- public class KNTerm extends scheme{
- double A, B, C;
- protected KNTerm(double h, double tau) {
- super(h, tau);
- a = 0.02;
- n = 11;
- A = a*tau/(2*h*h);
- B = (-1)*(1 + a*tau/(h*h));
- C = A;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- double[] F = new double[N+1];
- for(int i = 1; i < N-1; i++) {
- F[i-1] = (-1)*(temp[i-1][j-1]*(a*tau/(2*h*h)) + temp[i][j-1]*(1 - a*tau/(h*h)) + temp[i+1][j-1]*(a*tau/(2*h*h)) + tau*f(j*tau, i*h));
- }
- F[0] -= solution(tau*j, 0)*a*tau/(2*h*h); ////1.4268
- F[N-2] -= solution(tau*j, 1)*a*tau/(2*h*h);
- calc(F, j);
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- private void calc(double[] F, int j) {
- int n = N-1;
- double[] p = new double[n-1], q = new double[n-1], X = new double[n];
- p[0] = C/B;
- q[0] = F[0]/B;
- for(int i = 1; i < n-1; i++) {
- p[i] = C/(B - p[i-1]*A);
- q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
- }
- X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
- for(int i = n-2; i >= 0; i--) {
- X[i] = q[i] - p[i]*X[i+1];
- }
- for(int i = 1; i < N; i++) {
- temp[i][j] = X[i-1];
- }
- }
- @Override
- protected double f(double t, double x) {
- double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
- return result;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- public class exDiff extends scheme{
- public exDiff(double h, double tau) {
- super(h, tau);
- a = 0.034;
- n = 11;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- for(int i = 1; i < N; i++) {
- temp[i][j] = temp[i][j-1] - a*(temp[i][j-1] - temp[i-1][j-1])*tau/h + tau*f((j-1)*tau, i*h);
- }
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- @Override
- protected double f(double t, double x) {
- double result = a*(2*Math.PI - Math.PI*Math.sin(Math.PI*x)) - Math.PI*Math.cos(2*Math.PI*t) - 3.5;
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = Math.cos(Math.PI*x) - 0.5*Math.sin(2*Math.PI*t) + 2*Math.PI*x - 3.5*t;
- return result;
- }
- }
- ///////////////////////////////////////////////////////////////
- public class imDiff extends scheme{
- private double A, B, C;
- protected imDiff(double h, double tau) {
- super(h, tau);
- a = 0.001;
- n = 11;
- A = -a*tau/h;
- B = (1 + a*tau/h);
- C = 0;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- double[] F = new double[N+1];
- for(int i = 0; i < N-1; i++) {
- F[i] = (temp[i][j-1] + tau*f(j*tau, i*h));
- }
- calc(F, j);
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- private void calc(double[] F, int j) {
- int n = N-1;
- double[] p = new double[n-1], q = new double[n-1], X = new double[n];
- p[0] = C/B;
- q[0] = F[0]/B;
- for(int i = 1; i < n-1; i++) {
- p[i] = C/(B - p[i-1]*A);
- q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
- }
- X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
- for(int i = n-2; i >= 0; i--) {
- X[i] = q[i] - p[i]*X[i+1];
- }
- for(int i = 1; i < N; i++) {
- temp[i][j] = X[N-(i+1)];
- }
- }
- @Override
- protected double f(double t, double x) {
- double result = a*(2*Math.PI - Math.PI*Math.sin(Math.PI*x)) - Math.PI*Math.cos(2*Math.PI*t) - 3.5;
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = Math.cos(Math.PI*x) - 0.5*Math.sin(2*Math.PI*t) + 2*Math.PI*x - 3.5*t;
- return result;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- public class lacksDiff extends scheme{
- public lacksDiff(double h, double tau) {
- super(h, tau);
- a = 0.4;
- n = 11;
- run();
- paint();
- error();
- }
- @Override
- protected void run() {
- for(int i = 0; i < N+1; i++) {
- temp[i][0] = solution(0, i*h);
- }
- for(int j = 1; j < M+1; j++) {
- temp[0][j] = solution(j*tau, 0);
- temp[N][j] = solution(j*tau, 1);
- for(int i = 1; i < N; i++) {
- temp[i][j] = 0.5*(temp[i+1][j-1] + temp[i-1][j-1]) + (temp[i-1][j-1] - temp[i+1][j-1])*(a*tau/(2*h));
- }
- }
- U1 = new double[n];
- for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
- }
- @Override
- protected double f(double t, double x) {
- double result = 0;
- return result;
- }
- @Override
- protected double solution(double t, double x) {
- double result = Math.cos(2*Math.PI*(x - a*t)) + Math.pow((x - a*t), 3);
- return result;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- import java.awt.BorderLayout;
- import java.awt.Color;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class myFrame extends JFrame{
- public static int y[];
- public static int x[];
- public static int y1[];
- public static int n = 5;
- public myFrame () {
- super("График");
- JPanel jcp = new JPanel(new BorderLayout());
- setContentPane(jcp);
- jcp.add(new drawingComponent (), BorderLayout.CENTER);
- jcp.setBackground(Color.gray);
- setSize(500, 400);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.JPanel;
- public class drawingComponent extends JPanel{
- int xg[] = myFrame.x;
- int yg[] = myFrame.y;
- int yg1[] = myFrame.y1;
- int ng = myFrame.n;
- @Override
- protected void paintComponent(Graphics gh) {
- Graphics2D drp = (Graphics2D)gh;
- drp.drawLine(20, 340, 20, 20);
- drp.drawLine(20, 340, 460, 340);
- drp.setColor(Color.RED);
- drp.drawPolyline(xg, yg, ng);
- drp.setColor(Color.BLACK);
- drp.drawPolyline(xg, yg1, ng);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement