已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式附其中一组数据X=1,3,4,5,6,7,8,9,10;Y=10,5,4,2,1,1,2,3,4;阶数为2阶另:最好阶数设为m需要得出公式!急求!尽快解决后再加30
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/05 14:37:01
已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式附其中一组数据X=1,3,4,5,6,7,8,9,10;Y=10,5,4,2,1,1,2,3,4;阶数为2阶另:最好阶数设为m需要得出公式!急求!尽快解决后再加30
已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式
附其中一组数据X=1,3,4,5,6,7,8,9,10;Y=10,5,4,2,1,1,2,3,4;阶数为2阶
另:最好阶数设为m
需要得出公式!急求!
尽快解决后再加30
已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式附其中一组数据X=1,3,4,5,6,7,8,9,10;Y=10,5,4,2,1,1,2,3,4;阶数为2阶另:最好阶数设为m需要得出公式!急求!尽快解决后再加30
/**
* 最小二乘法计算类
*
* @author Administrator
*
*/
public class LeastSquareMethod {
\x05private double[] x;
\x05private double[] y;
\x05private double[] weight;
\x05private int m;
\x05private double[] coefficient;
\x05public LeastSquareMethod(double[] x,double[] y,int m) {
\x05\x05if (x == null || y == null || x.length < 2 || x.length != y.length
\x05\x05\x05\x05|| m < 2)
\x05\x05\x05throw new IllegalArgumentException("无效的参数");
\x05\x05this.x = x;
\x05\x05this.y = y;
\x05\x05this.m = m;
\x05\x05weight = new double[x.length];
\x05\x05for (int i = 0; i < x.length; i++) {
\x05\x05\x05weight[i] = 1;
\x05\x05}
\x05}
\x05public LeastSquareMethod(double[] x,double[] y,double[] weight,int m) {
\x05\x05if (x == null || y == null || weight == null || x.length < 2
\x05\x05\x05\x05|| x.length != y.length || x.length != weight.length || m < 2)
\x05\x05\x05throw new IllegalArgumentException("无效的参数");
\x05\x05this.x = x;
\x05\x05this.y = y;
\x05\x05this.m = m;
\x05\x05this.weight = weight;
\x05}
\x05public double[] getCoefficient() {
\x05\x05if (coefficient == null)
\x05\x05\x05compute();
\x05\x05return coefficient;
\x05}
\x05public double fit(double v) {
\x05\x05if (coefficient == null)
\x05\x05\x05compute();
\x05\x05if (coefficient == null)
\x05\x05\x05return 0;
\x05\x05double sum = 0;
\x05\x05for (int i = 0; i < coefficient.length; i++) {
\x05\x05\x05sum += Math.pow(v,i) * coefficient[i];
\x05\x05}
\x05\x05return sum;
\x05}
\x05private void compute() {
\x05\x05if (x == null || y == null || x.length