Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vadim8kiselev
Created December 18, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadim8kiselev/6de4c62965a8a73594ec13f2b7cf831e to your computer and use it in GitHub Desktop.
Save vadim8kiselev/6de4c62965a8a73594ec13f2b7cf831e to your computer and use it in GitHub Desktop.
package com.company;
public class Main {
private static final int V = 100;
private static final double begin = 0;
private static final double end = V;
private static final int countPartitions = 15;
private static final double part = (end - begin) / (countPartitions - 1);
public static void main(String[] args) {
double[] arrayOfX = new double[countPartitions];
arrayOfX[0] = begin;
for (int i = 1; i < countPartitions; i++) {
arrayOfX[i] = arrayOfX[i - 1] + part;
}
double[][] matrix = getMatrix(arrayOfX, part);
double[] vector = getVector(arrayOfX);
double[] yArray = sweepMethod(matrix, vector);
double[] preciseArrayOfY = new double[arrayOfX.length];
for (int i = 0; i < countPartitions; i++) {
preciseArrayOfY[i] = getYExact(arrayOfX[i]);
}
double[] diffs = new double[yArray.length];
for (int i = 0; i < yArray.length; i++) {
diffs[i] = Math.abs(yArray[i] - preciseArrayOfY[i]);
}
printArray(" x: ", arrayOfX );
printArray("prec y: ", preciseArrayOfY );
printArray("calc y: ", yArray );
printArray("diff y: ", diffs );
}
public static double[][] getMatrix(double[] array, double h) {
double[][] matrix = new double[array.length][array.length];
for (int i = 0; i < array.length; i++) {
double x = array[i];
double q = (1 / (h * h)) - ((x * x) / (2 * h));
double w = (2 / (h * h)) - (x * x * x);
double e = (1 / (h * h)) + ((x * x) / (2 * h));
try { matrix[i][i - 1] = q; } catch (ArrayIndexOutOfBoundsException ex) {}
try { matrix[i][i] = -w; } catch (ArrayIndexOutOfBoundsException ex) {}
try { matrix[i][i + 1] = e; } catch (ArrayIndexOutOfBoundsException ex) {}
}
return matrix;
}
public static double[] getVector(double[] array) {
double[] vector = new double[array.length];
for (int i = 0; i < vector.length; i++) {
vector[i] = getF(array[i]);
}
return vector;
}
public static double getYExact(double x) {
return x * x * x // x ^ 3
- V * x * x; // - V * x ^ 2
}
public static double getF(double x) {
return x * x * x * x * x * x // x ^ 6
- V * x * x * x * x * x // - V * x ^ 5
+ 3 * x * x * x * x // + 3 * x ^ 4
- 2 * V * x * x * x // - 2 * V * x ^ 3
+ 6 * x // + 6 * x
- 2 * V; // - 2 * V
}
public static double[] sweepMethod(double[][] matrix, double[] vector) {
double[] arrayP = new double[matrix.length + 1];
double[] arrayQ = new double[matrix.length + 1];
arrayP[1] = matrix[0][1] / -matrix[0][0];
arrayQ[1] = -vector[0] / -matrix[0][0];
for (int i = 1; i < vector.length; i++) {
if (i == vector.length - 1) {
arrayP[i + 1] = 0;
} else {
arrayP[i + 1] =
matrix[i][i + 1] / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
}
arrayQ[i + 1] = (matrix[i][i - 1] * arrayQ[i] - vector[i]) / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
}
double[] result = new double[vector.length];
result[result.length - 1] = arrayQ[result.length];
for (int i = result.length - 2; i >= 0; i--) {
result[i] = arrayP[i + 1] * result[i + 1] + arrayQ[i + 1];
}
result[0] = 0;
result[result.length - 1] = 0;
return result;
}
public static void printArray(String message, double[] array) {
System.out.println(message);
System.out.print("| ");
for (double value : array) {
System.out.print(String.format("%10.2f | ", value));
}
System.out.println("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment