
Originariamente inviata da
Andrea1979
Se si sta parlando di interpolazione polinomiale
https://en.wikipedia.org/wiki/Polynomial_interpolation
non ti resta che compilare la matrice di Vandermonde.
Commons di apache ha una libreria (math) che ti linko qui:
http://commons.apache.org/proper/commons-math/
che ha già implementato ogni cosa. Ti riporto un piccolo esempio da cui articolare
codice:
import org.apache.commons.math3.linear.*;
/* ....
....
*/
private RealMatrix VandermondeMatrix;
private RealVector Constants, CoefficientsMatrix;
private int degree;
private RealMatrix calculateVandermondeMatrix() {
double[][] values = new double[degree+1][degree+1];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < (values[i].length-1); j++) {
values[i][j] = Math.pow(points[i].getX(), degree-j);
}
values[i][degree] = 1;
}
VandermondeMatrix = new Array2DRowRealMatrix(values);
return VandermondeMatrix;
}
private RealVector getConstants() {
double[] values = new double[degree+1];
for (int i = 0; i < values.length; i++) {
values[i] = points[i].getY();
}
Constants = new ArrayRealVector(values) ;
return Constants;
}
private RealVector calculateCoefficientsMatrix() {
DecompositionSolver solver = new LUDecomposition(this.VandermondeMatrix).getSolver();
return solver.solve(Constants);
}
public PolynomialInterpolation(Point[] points) {
this.points = points;
this.degree = points.length-1;
}
dove la classe Point (in realtà inutile, ma visto che ho riutilizzato codice di un vecchio progetto me la sono trovata tra i piedi) altro non è che una classe che modellizza un punto nel piano (2D) e che ha solo due campi, x, y e metodi getter e setter.