简体   繁体   English

谐波振荡器

[英]Harmonic oscillator

I'm trying to solve the simple pendulum in python.我正在尝试解决python中的简单钟摆。 My goal is to save my results into a file in order to make a plot afterwards.我的目标是将我的结果保存到一个文件中,以便之后进行绘图。 Should I put my the code that saves the data in the loop or define a new function ?我应该将保存数据的代码放在循环中还是定义一个新函数?

NB: I'm a beginner.注意:我是初学者。

Thank you.谢谢你。

import numpy as np

g = 9.8
L = 3

THETA_0 = np.pi / 4
THETA_DOT_0 = 0

def get_theta_double_dot(theta):
    return - (g / L) * np.sin(theta)

def theta(t):
    theta = THETA_0
    theta_dot = THETA_DOT_0

    dela_t = 0.01
    for tps in np.arange(0, t, delta_t):
        theta_double_dot = get_theta_double_dot(theta)
        theta = theta + (theta_dot * delta_t)
        theta_dot = theta_dot + (theta_double_dot * delta_t)
    return theta

Definitely store your results in a variable, and then save the whole thing into a file afterwards.绝对将您的结果存储在一个变量中,然后将整个内容保存到一个文件中。 Saving during the loop is "messy" and less efficient because of I/O calls constantly.由于 I/O 调用不断,循环期间的保存是“混乱的”且效率较低。

Note that this is not true if you are dealing with millions of entries, in which case you'd have to spare memory a bit and write to file in "batches".请注意,如果您正在处理数百万个条目,则情况并非如此,在这种情况下,您必须节省一点内存并“批量”写入文件。 But it doesn't feel like this should be a problem in your case.但在你的情况下,这不应该是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM