Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main{
- public static void main(String[] args) {
- int a = -1;
- int b = 1;
- int c = -1;
- int d = 1;
- PoissonEquation equation = new PoissonEquation(a, b, c, d);
- equation.calculateSolution();
- equation.printSolution();
- }
- }
- class PoissonEquation{
- private int a;
- private int b;
- private int c;
- private int d;
- private double h1 = 0.05;
- private int N1;
- private double h2 = 0.15;
- private int N2;
- private double h2plus;
- private double h2minus;
- private double[] nodesX1;
- private double[] nodesX2;
- private double accuracy;
- private double[][] solution;
- public PoissonEquation(int a, int b, int c, int d) {
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- N1 = (int)((b - a) / h1);
- N2 = (int)((d - c) / h2) + 1;
- nodesX1 = new double[N1 + 1];
- fillNodesX1();
- nodesX2 = new double[N2 + 1];
- fillNodesX2();
- h2plus = (d - c) - (N2 - 1) * h2;
- h2minus = h2;
- accuracy = h1 * h1 * h1;
- }
- private void fillNodesX1(){
- for(int i = 0; i < N1 + 1; i++){
- nodesX1[i] = a + i * h1;
- }
- }
- private void fillNodesX2(){
- for(int j = 0; j < N2; j++){
- nodesX2[j] = c + j * h2;
- }
- nodesX2[N2] = d;
- }
- private double computationF(double x1, double x2){
- return Math.abs(Math.pow(Math.sin(Math.PI * x1 * x2), 3));
- }
- private double computationPsi1(double x2){
- return 1 - x2 * x2;
- }
- private double computationPsi2(double x2){
- return 1 - x2 * x2;
- }
- private double computationPsi3(double x1){
- return Math.abs(Math.sin(Math.PI * x1));
- }
- private double computationPsi4(double x1){
- return Math.abs(Math.sin(Math.PI * x1));
- }
- public void calculateSolution(){
- double[][] prevSolution;
- double[][] nextSolution = new double[N2 + 1][N1 + 1];
- for(int j = 0; j < N2 + 1; j++){
- nextSolution[j][0] = computationPsi1(nodesX2[j]);
- nextSolution[j][N1] = computationPsi2(nodesX2[j]);
- }
- for(int i = 0; i < N1 + 1; i++){
- nextSolution[0][i] = computationPsi3(nodesX1[i]);
- nextSolution[N2][i] = computationPsi4(nodesX1[i]);
- }
- for(int j = 1; j < N2; j++){
- for(int i = 1; i < N1; i++){
- nextSolution[j][i] = computationF(nodesX1[i], nodesX2[j]);
- }
- }
- do{
- prevSolution = copyMatrix(nextSolution);
- for(int j = 1; j < N2 - 1; j++){
- for(int i = 1; i < N1; i++){
- nextSolution[j][i] = Math.pow(2.0 / (h1 * h1) + 2.0 / (h2 * h2), -1) *
- (
- (prevSolution[j][i + 1] + prevSolution[j][i - 1]) / (h1 * h1) +
- (prevSolution[j + 1][i] + prevSolution[j - 1][i]) / (h2 * h2) +
- computationF(nodesX1[i], nodesX2[j])
- );
- }
- }
- for(int i = 1; i < N1; i++){
- nextSolution[N2 - 1][i] =
- Math.pow(2.0 / (h1 * h1) + 1.0 / ((h2plus + h2minus) / 2) * (1.0 / h2plus + 1.0 / h2minus), -1) *
- (
- (prevSolution[N2 - 1][i + 1] + prevSolution[N2 - 1][i - 1]) / (h1 * h1) +
- (1.0 / ((h2plus + h2minus) / 2)) * (prevSolution[N2][i] / h2plus + prevSolution[N2 - 2][i] / h2minus) +
- computationF(nodesX1[i], nodesX2[N2 - 1])
- );
- }
- }while(normMatrix(differenceMatrix(nextSolution, prevSolution)) >= accuracy);
- solution = nextSolution;
- }
- public void printSolution(){
- for(int j = 0; j < N2 + 1; j++){
- for(int i = 0; i < N1 + 1; i++){
- System.out.printf("%.3f ", solution[j][i]);
- }
- System.out.println();
- }
- }
- private double[][] copyMatrix(double[][] matrix){
- double[][] tempMatrix = new double[N2 + 1][N1 + 1];
- for (int j = 0; j < N2 + 1; j++) {
- for (int i = 0; i < N1 + 1; i++) {
- tempMatrix[j][i] = matrix[j][i];
- }
- }
- return tempMatrix;
- }
- private double[][] differenceMatrix(double[][] matrixA, double[][] matrixB){
- double[][] answer = new double[N2 + 1][N1 + 1];
- for (int j = 0; j < N2 + 1; j++) {
- for (int i = 0; i < N2 + 1; i++) {
- answer[i][j] = matrixA[j][i] - matrixB[j][i];
- }
- }
- return answer;
- }
- private double normMatrix(double[][] matrix){
- double answer = 0;
- double sum;
- for (int j = 0; j < N2 + 1; j++) {
- sum = 0;
- for (int i = 0; i < N1 + 1; i++) {
- sum += Math.abs(matrix[j][i]);
- }
- answer = Math.max(answer, sum);
- }
- return answer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement