Domanda

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?

È stato utile?

Soluzione

For a simple solution. Use numpy package and numpy.diff gives you the derivative.

Altri suggerimenti

If you're just looking for a first-order estimation, you can use δ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)

Note that the derivatives have time indexes midway between points. Consequently, the new time and derivatives lists will be 1 element shorter than the inputs. This is normal, though some more advanced methods will have 1-to-1 times. Note also that the output file format is up to you. I've just used approximately the same format as above.

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)

correction from - to + ==> f[i + 1] - f[i]) / delta_t

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top