1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.LUDecomposition; import org.apache.commons.math3.linear.RealMatrix;
import java.text.DecimalFormat;
public class Sanci { final int max = 10; double[] h = new double[max]; double[] b = new double[max-1]; double[] m = new double[max-1]; double[] n = new double[max-1]; double[] D = new double[max+1];
double s;
public void step(double[] x){ for (int i = 0; i < max - 1; i++) b[i] = 2; for (int i = 0; i < max; i++) { h[i] = x[i + 1] - x[i]; } for (int k = 1; k < max - 1; k++) { m[k] = h[k - 1] / (h[k - 1] + h[k]); n[k] = 1 - m[k]; }
}
public RealMatrix conect(double[] y){ D[0] = 0; D[max] = 0; double[] D1 = new double[max-1]; RealMatrix MD = null; for (int i = 1; i < max; i++) { D[i] = (6 / (h[i - 1] + h[i]) * ((y[i + 1] - y[i]) / h[i] - (y[i] - y[i - 1]) / h[i - 1])); D1[i-1] =D[i]; } MD = new Array2DRowRealMatrix(D1); return MD; }
public RealMatrix det(){ double[][] A =new double[max-1][max-1];
for (int i = 0; i < max -1; i++){ for (int j = 0; j < max-1; j++){ if (i == j){ A[i][i] = b[i]; } else if (j - i == 1) { A[i][j] = n[i + 1]; } else if (i - j == 1) { A[i][j] = m[j + 1]; } else { A[i][j] = 0; } } } RealMatrix matrix = new Array2DRowRealMatrix(A); return matrix; }
public static RealMatrix inverseMatrix(RealMatrix A) { RealMatrix result = new LUDecomposition(A).getSolver().getInverse(); return result; }
public String get(double[] h, double[] M, double[] x, double[] y, double l){ double s = 0; DecimalFormat df = new DecimalFormat("0.0000"); String s1 = null; for (int i = 0; i < max; i++) { if (l >=x[i] && l <= x[i + 1]) { s = (M[i] * (Math.pow((x[i + 1] - l), 3))) / (6 * h[i]) + (M[i + 1] * (Math.pow((l - x[i]), 3))) / (6 * h[i]) + (y[i] - (M[i] * h[i] * h[i] / 6)) * (x[i + 1] - l) / h[i] + (y[i + 1] - (M[i + 1] * h[i] * h[i] / 6)) * (l - x[i]) / h[i]; }
} s1 = df.format(s); return s1; } public boolean print1(double[] array){ int count = 0; for (int n=0;n<array.length;n++){ System.out.print(array[n]+" "); count++; if (count%9==0) System.out.println("\n"); } return true; } public double[] change(double[][] M){ double[] m; int len = 0; for (double[] element : M) { len += element.length; } m = new double[len]; int index = 0; for (double[] element : M) { for (double element2 : element) { m[index++] = element2; } } return m; } public double[] xxx(double[] m){ double[] m1 = new double[m.length+2]; m1[0] =0; m1[m.length+1]=0; for (int i=0;i<m.length;i++){ m1[i+1] = m[i]; } return m1; }
public static void main(String[] args) { Sanci sc = new Sanci(); double[] h = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; double[] x = { -5.0000,-4.0000,-3.0000,-2.0000,-1.0000, 0 ,1.0000,2.0000,3.0000,4.0000,5.0000}; double[] y = { -0.0000,-0.0002,-0.0004,-0.0366,-0.3679, 0 ,0.3679,0.0366,0.0004,0.0002,0.0000}; double[] x0 ={ -5.0000,-4.5000,-4.0000,-3.5000,-3.0000,-2.5000,-2.0000, -1.5000,-1.0000,-0.5000,0,0.5000,1.0000,1.5000, 2.0000,2.5000,3.0000,3.5000,4.0000,4.5000,5.0000};
sc.step(x); RealMatrix MD = sc.conect(y); RealMatrix A = sc.det(); RealMatrix inversetestMatrix = inverseMatrix(A); RealMatrix MM = inversetestMatrix.multiply(MD); double[][] M = MM.getData(); double[] m = sc.change(M); double[] m1 = sc.xxx(m); String s; for(int p=0;p<x0.length;p++){ s = sc.get(h,m1,x,y,x0[p]); System.out.println("S:"+s); }
} }
|