From 22e4fd76a13d86d8108eb96cc67388416738f846 Mon Sep 17 00:00:00 2001 From: SupZ <3321076758@qq.com> Date: Tue, 28 May 2024 19:47:34 +0800 Subject: [PATCH] add --- .gitignore | 29 +++ .idea/.gitignore | 8 + .idea/dbnavigator.xml | 411 +++++++++++++++++++++++++++++++++ .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + HomeWork_.iml | 11 + src/LagrangeInterpolation.java | 30 +++ src/NewtonInterpolation.java | 46 ++++ src/T3.md | 9 + 10 files changed, 564 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/dbnavigator.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 HomeWork_.iml create mode 100644 src/LagrangeInterpolation.java create mode 100644 src/NewtonInterpolation.java create mode 100644 src/T3.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..52282c8 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,411 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c70a0c6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/HomeWork_.iml b/HomeWork_.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/HomeWork_.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/LagrangeInterpolation.java b/src/LagrangeInterpolation.java new file mode 100644 index 0000000..55f729f --- /dev/null +++ b/src/LagrangeInterpolation.java @@ -0,0 +1,30 @@ +/** + * 第一题 + */ + +public class LagrangeInterpolation { + 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 = f1(t, v, targetT); + System.out.println(targetV); + } + + public static double f1(double[] t, double[] v, double targetT) { + double result = 0.0; + int n = t.length; + + for (int i = 0; i < n; i++) { + double term = v[i]; + for (int j = 0; j < n; j++) { + if (i != j) { + term *= (targetT - t[j]) / (t[i] - t[j]); + } + } + result += term; + } + return result; + } +} diff --git a/src/NewtonInterpolation.java b/src/NewtonInterpolation.java new file mode 100644 index 0000000..a0a5447 --- /dev/null +++ b/src/NewtonInterpolation.java @@ -0,0 +1,46 @@ +/** + * 第二题 + */ +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; + } +} diff --git a/src/T3.md b/src/T3.md new file mode 100644 index 0000000..e04e82b --- /dev/null +++ b/src/T3.md @@ -0,0 +1,9 @@ +# 第三题 + +两种方法得到的结果相同。 + + + +观点: + +在数据点很少的情况下,两种方法的精度没有区别 \ No newline at end of file