为什么我会根据我是手动输入值还是读取具有完全相同值的 CSV 文件而得到不同的输出?

Why do I get different output depending on whether I enter the values manually or read a CSV file with exactly the same values?

我正在尝试绘制物体(准确地说是光叉)的下落随时间变化的曲线,以验证万有引力定律确实是 9.81。不同的数据应该代表每个插槽的通道。不同的狭缝间隔1厘米,总共有11条狭缝。我用 Arduino 设置测量了这些数据,并绘制了图表并用 Python 拟合。但是,根据我是手动输入值还是通过读取包含这些相同值的 CSV 文件,我会得到两个完全不同的输出。 这是我手动输入值时使用的代码:

import numpy as np                    # For the calculation
import pandas as pd                   # To read files
import matplotlib.pyplot as plt       # To draw curves
import scipy.optimize as opt          # For the adjustment

# Raw data
#t = 1e-3 * np.array([7.038,10.574,14.122,17.684,21.258,24.84,28.434,32.032,35.642,39.26,42.886])
z = -0.01 * np.linspace(1, 11, 11)

# Definition of the free fall function
g = 9.81                     # the acceleration of gravity

def f(t,t0,h0):        # Definition of the fitting function
    return -0.5*g*(t-t0)**2 + h0

# Data adjustment
init_param = [0 , 0]          # Initial values t0=0, h0=0
final_param , var = opt.curve_fit(f,t,z)

# Optimal function
tt = np.linspace(final_param[0], 100e-3,100)
hh = f(tt, *final_param) # Reconstruction of the fitted curve

# Plot of analyzed data
plt.clf()                           # Plot of data and fit
plt.xlabel("Time (s)")
plt.ylabel("Height (m)")
legend = "t0 = %f ms, h0 = %f centimeter " % (final_param[0]*1000,final_param[1]*100)
plt.plot(tt,hh,"r--",label=legend)     # The adjustment
plt.plot(t,z,"bo", label="Data")     # The data
plt.legend()

这是我用这段代码得到的输出:

下面是我在不手动输入的情况下直接从 CSV 文件读取值时使用的代码:

import numpy as np                    # For the calculation
import pandas as pd                   # To read files
import matplotlib.pyplot as plt       # To draw curves
import scipy.optimize as opt          # For the adjustment

# Raw data
data = pd.read_csv("Test.csv", sep=";")   # Opening the data file

timeData = data['Sum of the times (s)']
disData = data['Distance (m)']

t = np.array(timeData[3:13])   # extracts data between 3.524 and 3.626 s
z = np.array(disData[3:13])

t = 1e-3 * t
z = -0.01 * z

# Definition of the free fall function
g = 9.81                     # the acceleration of gravity

def f(t,t0,h0):        # Definition of the fitting function
    return -0.5*g*(t-t0)**2 + h0

# Data adjustment
init_param = [0 , 0]          # Initial values t0=0, h0=0
final_param , var = opt.curve_fit(f,t,z,init_param)

# Optimal function
tt = np.linspace(final_param[0], 100e-3,100)
hh = f(tt, *final_param) # Reconstruction of the fitted curve

# Plot of analyzed data
plt.clf()                           # Plot of data and fit
plt.xlabel("Time (s)")
plt.ylabel("Height (m)")
legend = "t0 = %f ms, h0 = %f centimeter " % (final_param[0]*1000,final_param[1]*100)
plt.plot(tt,hh,"r--",label=legend)     # The adjustment
plt.plot(t,z,"bo", label="Data")     # The data
plt.legend()

这是我用第二个代码得到的结果:

最后,这是我在第二个代码中使用的 CSV 文件,名称为 'Test.csv':

T (s) Distance (m) Sum of the times (s)
3.514 0.000 3.514
3.524 0.010 7.038
3.536 0.020 10.574
3.548 0.030 14.122
3.562 0.040 17.684
3.574 0.050 21.258
3.582 0.060 24.84
3.592 0.070 28.434
3.6 0.080 32.032
3.61 0.090 35.642
3.618 0.100 39.26
3.626 0.110 42.886
3.636 0.120 46.522

我指定不取 table 的第一个和最后一个值,因为叉子的边缘测量超过一厘米。

我的两个代码有效,但如您所见,即使值相同,我也得到了两个完全不同的输出。您认为可能是什么原因?

我无法 运行 你的第二个代码段,因为它给出了一个错误,即 x_util 未定义,但问题主要在于 z。在第一个代码中,您将它乘以 -0.01 以获得范围:

[-0.01 -0.02 -0.03 -0.04 -0.05 -0.06 -0.07 -0.08 -0.09 -0.1  -0.11]

但是,CSV 中的比例已经正确,因此通过继续进行乘法运算,您将得到:

[-0.0001 -0.0002 -0.0003 -0.0004 -0.0005 -0.0006 -0.0007 -0.0008 -0.0009
 -0.001  -0.0011 -0.0012]

只需执行 z = -1 * z

此外,切片 [3:13] 会跳过前 3 行并以 T=3.548 而不是 T=3.524 开始。因此,要匹配第一个片段的数据,您需要切片 [1:13].