当前位置: > 已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式...
题目
已知一组数据,用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

提问时间:2021-01-20

答案
/**
* 最小二乘法计算类
*
* @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) {
x05x05if (x == null || y == null || x.length < 2 || x.length != y.length
x05x05x05x05|| m < 2)
x05x05x05throw new IllegalArgumentException("无效的参数");
x05x05this.x = x;
x05x05this.y = y;
x05x05this.m = m;
x05x05weight = new double[x.length];
x05x05for (int i = 0; i < x.length; i++) {
x05x05x05weight[i] = 1;
x05x05}
x05}
x05public LeastSquareMethod(double[] x,double[] y,double[] weight,int m) {
x05x05if (x == null || y == null || weight == null || x.length < 2
x05x05x05x05|| x.length != y.length || x.length != weight.length || m < 2)
x05x05x05throw new IllegalArgumentException("无效的参数");
x05x05this.x = x;
x05x05this.y = y;
x05x05this.m = m;
x05x05this.weight = weight;
x05}
x05public double[] getCoefficient() {
x05x05if (coefficient == null)
x05x05x05compute();
x05x05return coefficient;
x05}
x05public double fit(double v) {
x05x05if (coefficient == null)
x05x05x05compute();
x05x05if (coefficient == null)
x05x05x05return 0;
x05x05double sum = 0;
x05x05for (int i = 0; i < coefficient.length; i++) {
x05x05x05sum += Math.pow(v,i) * coefficient[i];
x05x05}
x05x05return sum;
x05}
x05private void compute() {
x05x05if (x == null || y == null || x.length
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.