Ok, faccio prima a scriverti un esempio completo:
codice:
package interpolation;
import org.apache.commons.math3.linear.*;
/**
*
* @author Andrea
*/
public class PolynomialInterpolation {
private Point[] points;
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;
VandermondeMatrix = this.calculateVandermondeMatrix();
Constants = this.getConstants();
CoefficientsMatrix = this.calculateCoefficientsMatrix();
}
public PolynomialInterpolation() {
this(new Point[]{
new Point(0,1),
new Point (2,5)
});
}
public String toString() {
double[] coeff = CoefficientsMatrix.toArray();
StringBuffer buf = new StringBuffer();
String sign;
String exp;
double pivot;
for (int i = degree; i >= 0; i--) {
pivot = coeff[coeff.length-i-1];
//sign = (pivot > 0 ? "+" : (pivot < 0 ? "-" : ""));
sign = (pivot > 0 ? "+" : "");
exp = (i == 1 ? "x" : "x^"+i);
buf.append(i != 0 ? (" "+sign+pivot+exp+" ") : " "+sign+pivot);
}
return buf.toString();
}
public static void main (String[] args) {
PolynomialInterpolation pi = new PolynomialInterpolation(new Point[] {
new Point (0, 1),
new Point (2, 5),
new Point (3, -6)
});
/*
PolynomialInterpolation pi = new PolynomialInterpolation(new Point[] {
new Point (0, 0),
new Point (1, 0.8415),
new Point (2, 0.9093),
new Point (3, 0.1411),
new Point (4, -0.7568),
new Point (5, -0.9589),
new Point (6, -0.2794)
});
*/
String result = pi.toString();
System.out.println(result);
}
}
e questa è la classe Point
codice:
package interpolation;
/**
*
* @author Andrea
*/
public class Point {
double x;
double y;
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public void setX (double x) {
this.x = x;
}
public void setY (double y) {
this.y = y;
}
public Point (double x, double y) {
this.x = x;
this.y = y;
}
public String toString() {
return new String("["+x+" , "+y+"]");
}
}
Per inciso, la parte commentata relativa all'interpolazione di 6 punti, usa i dati trovati sulla pagina di wikipedia:
https://it.wikipedia.org/wiki/Interp...ne_polinomiale
e come potrai verificare ritorna (in notazione scientifica) i risultati che trovi nella pagina stessa.
Venendo ai tuoi dubbi: tu hai una serie di punti (specifica del tuo progetto nel primo messaggio), ed io parto da una serie di punti (array di Point). Ti vuoi una funziona interpolatrice (assunto: un polinomio) ed lì hai una matrice (array) coefficientMatrix che rappresenta i coefficienti del tuo polinomio interpolatore. Il resto sono metodi di appoggio