1

I have a long 3-columns table of numerical data. For example, a small piece of it looks like this:

-5.3986816409999996e+00 8.3394692357616169e-23 2.2891221151890116e-23
-5.3984375003749996e+00 8.1834931317429596e-23 2.7962314960022732e-23
-5.3981933597499996e+00 7.9967086078053667e-23 3.2928354334362533e-23
-5.3979492191249996e+00 7.7798165392355974e-23 3.7770639137136620e-23
-5.3977050784999996e+00 7.5336312057849817e-23 4.2470933118491389e-23
-5.3974609378749996e+00 7.2590772420102232e-23 4.7011532627697938e-23
-5.3972167972499996e+00 6.9571861716728761e-23 5.1375333330996674e-23
-5.3969726566249996e+00 6.6290925391538841e-23 5.5545894684658167e-23
-5.3967285159999996e+00 6.2760296523720059e-23 5.9507501919987393e-23
-5.3964843753749996e+00 5.8993249531280359e-23 6.3245225306694521e-23
-5.3962402347499996e+00 5.5003950322868833e-23 6.6744976470308050e-23

To be clear, the first column shows the time(t) and the other two indicate some other variables both as functions of time, let say f(t) and g(t). I want to calculate the time-derivative of each function and save it in a new data file. I'm not sure how should I handle this numerically. Any idea?

4

3 回答 3

3

对于一个简单的解决方案。使用numpy包并numpy.diff为您提供衍生产品。

于 2013-11-09T00:11:19.223 回答
2

如果您只是在寻找一阶估计,则可以使用 δx/δt ≈ Δx / Δt。

# import file
t = []; f = []; g = []
for line in open('in_file.dat'):
    line = line.split()
    t.append(float(line[0]))
    f.append(float(line[1]))
    g.append(float(line[2]))

# calculate approximate derivatives
dt = [] # times for derivative values
df = [] # approximate derivative of f
dg = [] # approximate derivative of g
for i in range(len(t) - 1):
    delta_t = t[i + 1] - t[i]  # time between points
    dt.append(t[i] + delta_t * 0.5)  # midway between points
    df.append((f[i - 1] - f[i]) / delta_t)
    dg.append((g[i - 1] - g[i]) / delta_t)

# save to new file
f = '{:23.16e} {:23.16e} {:23.16e}\n'
out_lines = [f.format(dt[i], df[i], dg[i]) for i in range(len(dt))]
open('out_file.dat', 'w').writelines(out_lines)

请注意,导数在点之间具有时间索引。因此,新的时间和导数列表将比输入短 1 个元素。这是正常的,尽管一些更高级的方法会有 1 比 1 的时间。另请注意,输出文件格式取决于您。我刚刚使用了与上面大致相同的格式。

于 2013-11-08T21:15:43.970 回答
0
for i in range(len(t) - 1):
    delta_t = t[i + 1] - t[i]  # time between points
    dt.append(t[i] + delta_t * 0.5)  # midway between points
    df.append((f[i + 1] - f[i]) / delta_t)
    dg.append((g[i + 1] - g[i]) / delta_t)

从 - 到 + ==> f[i + 1] - f[i]) / delta_t 的校正

于 2013-12-05T10:43:13.207 回答