最小二乘法求多次拟合

GerD82 -1 2016-04-13 07:35 5

× 1

1

[Java]代码

```import java.util.*;

public class Nihe {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n, m, i, j, k;
System.out.println("输入x的个数");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
double a[] = new double[n];
double b[] = new double[n];
System.out.println("输入x");
Scanner str1 = new Scanner(System.in);
for (i = 0; i < n; i++)
a[i] = str1.nextDouble();// 数组a存储x的值
System.out.println("输入y");
Scanner str2 = new Scanner(System.in);
for (i = 0; i < n; i++)
b[i] = str2.nextDouble();// 数组b存储y的值
double sumx = 0;
double sumy = 0;
for (i = 0; i < n; i++) {
sumx += a[i];// x的类和；
sumy += b[i];// y的类和；
}
// System.out.println("x的类和"+sumx);
// System.out.println("y的类和"+sumy);
System.out.println("输入拟合次数");
Scanner str3 = new Scanner(System.in);
m = str3.nextInt();
int s = 2 * m;
double Sumx[] = new double[s];
for (i = 0; i < s; i++) {
double sum = 0;
for (j = 0; j < n; j++) {
double r = 1;
for (k = 0; k <= i; k++)
r = r * a[j];
sum += r;
}

Sumx[i] = sum;
}
/*
* for(i=0;i<s;i++){ System.out.print(Sumx[i]+"  "); }
*/
// System.out.println();
double Sumxy[] = new double[m];
for (i = 0; i < m; i++) {
double sumxy = 0;
for (j = 0; j < n; j++) {
double p = 1;
double w = 0;
for (k = 0; k <= i; k++)
p = p * a[j];
w = p * b[j];
sumxy += w;
}
Sumxy[i] = sumxy;
}
/*
* for(i=0;i<m;i++){ System.out.print(Sumxy[i]+"  "); }
*/
// System.out.println();
int t = m + 1;
int q = m + 2;
double A[][] = new double[t][q];
A[0][0] = n;
A[0][q - 1] = sumy;
for (j = 1; j < q - 1; j++)
A[0][j] = Sumx[j - 1];
for (i = 1; i < t; i++)
for (j = 0; j < q - 1; j++)
A[i][j] = Sumx[i + j - 1];
for (i = 1; i < t; i++)
A[i][q - 1] = Sumxy[i - 1];
/*
* for (i = 0; i < t; i++) { int count1 = 0; for (j= 0; j < q;j++) {
* System.out.print(A[i][j] + "  "); count1++; if (count1 == q)
* System.out.println(); } }
*/
for (k = 0; k < t; k++) {
for (i = k + 1; i < t; i++) {
double L = A[i][k] / A[k][k];
for (j = 0; j < q; j++)
A[i][j] = A[i][j] - L * A[k][j];
}
}
for (k = t - 1; k >= 0; k--) {
for (i = k - 1; i >= 0; i--) {
double L = A[i][k] / A[k][k];
for (j = q - 1; j >= k; j--)
A[i][j] = A[i][j] - L * A[k][j];
}
} // 求多项式的系数
/*
* for (i = 0; i < t; i++) { int count1 = 0; for (j= 0; j < q;j++) {
* System.out.print(A[i][j] + "  "); count1++; if (count1 == q)
* System.out.println(); } }
*/
double r[] = new double[t];
for (i = 0; i < t; i++) {
r[i] = A[i][q - 1] / A[i][i];
// System.out.println("x"+i+"="+r[i]);
}
double x;
System.out.println("输入计算的值");
Scanner sc1 = new Scanner(System.in);
x = sc1.nextDouble();

double SUM = 0;
double Z[] = new double[t];
for (i = 0; i < t; i++) {
double z = 1;
for (j = 1; j <= i; j++)
z = z * x;
Z[i] = z * r[i];

}
for (i = 0; i < t; i++)
SUM += Z[i];
System.out.println("f(" + x + ")" + "=" + SUM);
}
}```

1 年 前

16评论:
• ossaa 2018-05-09 16:37 代码数：0

Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.

currency converter

• Johnli 2017-01-10 14:11 代码数：0

谢谢

• ossaa 2018-05-10 19:00 代码数：0

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.

• tumubai 2016-05-18 22:26 代码数：0

谢谢楼主分享

谢谢分享啦，不知道是不是最优？

2 年 前
• aq2130062 2018-02-05 18:37 代码数：0

好不错啊

• ossaa 2018-05-16 19:42 代码数：0

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well..

SEO Company

• ossaa 2018-05-16 21:22 代码数：0

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.  seo

• 秒秒科技 2018-03-28 16:07 代码数：0

谢谢分享最小二乘求多次拟合算法

• hard_deng 2017-05-03 20:16 代码数：0

辛苦了啊。

• gererik 2018-04-21 14:17 代码数：0

Thanks  for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing?  ninja logo