 |
พอดีได้เจออันดับ1พอดี ถ้าใช้ degreeK==n ก็จะเข้าสู่ อันดับ2
import Jama.Matrix;
public class PolyLeastSquare {
/** * @param args */ public static void main(String[] args) {
double[] x={1,2,3,4,5,6,7,8,9}; int degreeK = 9; // double[] y={1,2,3,4,5,4,3,2,1};
// Matrix matB = new Matrix(new double[][]{y}); Matrix matY = new Matrix(new double[][]{{1},{2},{3},{4},{5},{4},{3},{2},{1}}); double[][] rowsX = new double[x.length][degreeK]; for (int r=0;r<x.length;r++) { double[] row = new double[degreeK]; for (int c=0;c<degreeK;c++) { row[c]=Math.pow(x[r], c); } rowsX[r]=row; } Matrix matX = new Matrix(rowsX); //public Matrix solve(Matrix B) //Solve X*A = B Matrix matA = matX.solve(matY);
println("what's A[k] make LeastSquare(Y-AX)"); println("given Y[n] X[n] degreeK<=n"); println("A"); printMat(matA);
println("X"); printMat(matX);
println("Y"); printMat(matY);
println("//set A plus 0 for the LeastSquare"); // matA=matA.plus(new Matrix(new double[][]{{0.1},{0.1},{0.01},{0.0}})); println("A'shift"); printMat(matA);
println("AX"); printMat(matX.times(matA));
Matrix matY_AX = matY.minus(matX.times(matA)); println("Y-AX"); printMat(matY_AX);
println("LeastSquare(Y-AX)"+LeastSquare(matY_AX)); // println("positive diff LeastSquare(Y-AoffX)"+(LeastSquare(matY_AX)-1.1255411255411265));
}
private static double LeastSquare(Matrix mat1col) { double LeastSquare = 0;
for (double[] row : mat1col.getArray()) { LeastSquare+=row[0]*row[0]; } return LeastSquare; }
private static void println(String string) {
System.out.println(string); }
private static void printMat(Matrix matA) { for (double[] row : matA.getArray()) {
for (double cell : row) {
System.out.print(" "+cell);
} System.out.println(""); } }
}
จากคุณ |
:
ฟฟ7
|
เขียนเมื่อ |
:
18 ต.ค. 55 16:38:05
A:58.137.93.5 X: TicketID:375732
|
|
|
|
 |