【发布时间】:2020-04-13 18:18:56
【问题描述】:
我有一个接受 4 个参数的函数,如下所示:
def Euler(nsteps, k , m , b):
nsteps = int(time_interval/step_size)
# Create empty arrays ready for the values of x and v
x = np.zeros(nsteps)
v = np.zeros(nsteps)
# Choose some initial conditions
x[0] = 0.0
v[0] = -1.0
for i in range(nsteps-1):
# Calculate the acceleration at step i
a = -(k/m)*x[i] - (b/m)*v[i]
# For each configuration at step i, calculate x and v for the later step i+1
x[i+1] = x[i] + v[i]*step_size
v[i+1] = v[i] + a*step_size
return x, v
我想将 x 和 v 写入文件,但我不太确定如何操作。这就是我所拥有的,但它不起作用。请问有人知道怎么解决吗?
Euler_method = open('Euler_method.txt' , 'w')
Euler_method.write(Euler(nsteps, k, m, b))
Euler_method.close()
【问题讨论】:
-
请在问题标题中添加编程语言名称并添加相应的问题标签。这将使可以回答的社区更加关注您的问题。
-
@AlexSC 实际上不需要在标题中添加语言名称。标签是繁重的工作。
-
看起来不错。 “不起作用”是什么意思?请edit 澄清。如果有错误信息,请包括它。参考minimal reproducible example。
标签: python function writetofile