我正在尝试创建一个程序,用后向微分法求解质量-弹簧-阻尼器系统,唯一的问题是我遇到了一个索引错误,我不知道如何解决:
import numpy as np
import matplotlib.pyplot as plt
def MSD_Solver(m,b,K):
#input: m = mass, b = damping ratio, K = spring constant
#output: (t,x) time vs position
tinitial = 0
tfinal = 15
step = .005
t = np.linspace(tinitial,tfinal,step)
x = np.zeros_like(t)
x[0]=0
x[1]=0
for k in range (len(t)-1): # extra element so subtract by 1
x[k] = (t**2)/((m+b)*t+(t**2)*k) + (x[k-2](-m))/((m+b)*t+(t**2)*k) + (x[k-1]((2*m)+(b*t)))/((m+b)*t+(t**2)*k)
return plt.plot(t,x)
print(MSD_Solver(1,.5,5)),MSD_Solver(1,1,5),MSD_Solver(1,2,5)
plt.show()
linspace doc显示第三个参数是项目数,而不是步骤数。您的
step
值被截断为0,因此t
返回的数组为空。结果,x
没有元素,x[0]
超出范围试试这个:
这将使您了解复杂计算中的语义错误
你可能想,用一阶和二阶差商来离散化
至
所以
在代码中
相关问题 更多 >
编程相关推荐