From 4434b55419c456d44623c721131fa8f13868eeb1 Mon Sep 17 00:00:00 2001 From: pj5b93fvk <1478154439@qq.com> Date: Wed, 29 May 2024 09:30:01 +0800 Subject: [PATCH] ADD file via upload --- Lagrange.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Lagrange.py diff --git a/Lagrange.py b/Lagrange.py new file mode 100644 index 0000000..f2ee22b --- /dev/null +++ b/Lagrange.py @@ -0,0 +1,27 @@ +import numpy as np + +# 给定数据 +t_values = np.array([0, 10, 15, 20, 22.5, 30]) +v_values = np.array([0, 227.04, 362.78, 517.35, 602.97, 901.67]) + +# 确定插值区间,选择包含 t=16 的四个数据点 +t0 = t_values[1] # 10 +t1 = t_values[2] # 15 +t2 = t_values[3] # 20 +t3 = t_values[4] # 22.5 + +v0 = v_values[1] # 227.04 +v1 = v_values[2] # 362.78 +v2 = v_values[3] # 517.35 +v3 = v_values[4] # 602.97 + +# 构造Lagrange插值多项式 +p = v0 * ((16 - t1) * (16 - t2) * (16 - t3)) / ((t0 - t1) * (t0 - t2) * (t0 - t3)) + \ + v1 * ((16 - t0) * (16 - t2) * (16 - t3)) / ((t1 - t0) * (t1 - t2) * (t1 - t3)) + \ + v2 * ((16 - t0) * (16 - t1) * (16 - t3)) / ((t2 - t0) * (t2 - t1) * (t2 - t3)) + \ + v3 * ((16 - t0) * (16 - t1) * (16 - t2)) / ((t3 - t0) * (t3 - t1) * (t3 - t2)) + +# 计算 t=16 时的速度 +velocity_at_16 = p + +print(f"使用Lagrange插值法,t=16 秒时的速度为: {velocity_at_16:.2f} m/s") \ No newline at end of file