You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB

/**
* 第二题
*/
public class NewtonInterpolation {
public static void main(String[] args) {
// 定义时间点数组
double[] t = {10, 15, 20, 22.5};
// 定义速度值数组
double[] v = {227.04, 362.78, 517.35, 602.97};
// 目标时间点
double targetT = 16;
// 计算目标时间点的速度值
double targetV = f2(t, v, targetT);
// 输出结果
System.out.println(targetV);
}
// 牛顿插值法函数
public static double f2(double[] t, double[] v, double targetT) {
int n = t.length;
double[][] dividedDifference = new double[n][n];
// 初始化差商表第一列为给定速度值
for (int i = 0; i < n; i++) {
dividedDifference[i][0] = v[i];
}
// 计算差商表
for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
dividedDifference[i][j] = (dividedDifference[i + 1][j - 1] - dividedDifference[i][j - 1]) / (t[i + j] - t[i]);
}
}
// 使用差商表计算插值多项式的值
double result = dividedDifference[0][0];
double term = 1.0;
// 构造插值多项式
for (int i = 1; i < n; i++) {
term *= (targetT - t[i - 1]);
result += dividedDifference[0][i] * term;
}
return result;
}
}