Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:编写一个程序来模拟沿字符串的一维波动_Python_String_Simulation_Waveform - Fatal编程技术网

Python:编写一个程序来模拟沿字符串的一维波动

Python:编写一个程序来模拟沿字符串的一维波动,python,string,simulation,waveform,Python,String,Simulation,Waveform,我正在做一个程序,它模拟沿着一维弦的波动,最终模拟不同的波包。我在《计算科学的Python脚本》一书中发现了一个程序,它声称描述了波动,但我不确定如何实现它(这本书在Google Books上,不会向我展示代码前后的文本) 例如,我知道“f”是x和t的函数,“I”是x的函数,但产生波实际上需要什么函数 I= f= c= L= n= dt= tstop= x = linespace(0,L,n+1) #grid points in x dir dx = L/float(n) if dt

我正在做一个程序,它模拟沿着一维弦的波动,最终模拟不同的波包。我在《计算科学的Python脚本》一书中发现了一个程序,它声称描述了波动,但我不确定如何实现它(这本书在Google Books上,不会向我展示代码前后的文本)

例如,我知道“f”是x和t的函数,“I”是x的函数,但产生波实际上需要什么函数

I= 
f= 
c= 
L= 
n=
dt=
tstop=


x = linespace(0,L,n+1) #grid points in x dir
dx = L/float(n)
if dt <= 0: dt = dx/float(c) #max step time
C2 = (c*dt/dx)**2  #help variable in the scheme
dt2 = dt*dt

up = zeros(n+1) #NumPy solution array
u = up.copy()   #solution at t-dt
um = up.copy()    #solution at t-2*dt

t = 0.0
for i in iseq(0,n):
u[i] +0.5*C2*(u[i-1] - 2*u[i] +u[i+1]) + \
    dt2*f(x[i], t)
um[0] = 0;   um[n] = 0

while t<= tstop:
t_old = t; t+=dt
#update all inner points:
for i in iseq(start=1, stop= n-1):
up[i] = -um[i] +2*u[i] + \
    C2*(u[i-1] - 2*u[i] + u[i+1]) + \
    dt2*f(x[i], t_old)

#insert boundary conditions
up[0] = 0; up[n] = 0
#updata data structures for next step
um = u.copy(); u = up.copy()
I=
f=
c=
L=
n=
dt=
顶部=
x=线空间(0,L,n+1)#x方向上的栅格点
dx=升/浮点数(n)

如果dt以下代码应有效:

from math import sin, pi
from numpy import zeros, linspace
from scitools.numpyutils import iseq

def I(x):   
    return sin(2*x*pi/L)  

def f(x,t): 
    return 0

def solver0(I, f, c, L, n, dt, tstop):
    # f is a function of x and t, I is a function of x
    x = linspace(0, L, n+1) # grid points in x dir
    dx = L/float(n)
    if dt <= 0: dt = dx/float(c) # max time step
    C2 = (c*dt/dx)**2 # help variable in the scheme
    dt2 = dt*dt

    up = zeros(n+1) # NumPy solution array
    u = up.copy() # solution at t-dt
    um = up.copy() # solution at t-2*dt

    t = 0.0
    for i in iseq(0,n):
        u[i] = I(x[i])
    for i in iseq(1,n-1):
        um[i] = u[i] + 0.5*C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                dt2*f(x[i], t)

    um[0] = 0; um[n] = 0

    while t <= tstop:
          t_old = t; t += dt
          # update all inner points:
          for i in iseq(start=1, stop=n-1):
              up[i] = - um[i] + 2*u[i] + \
                      C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                      dt2*f(x[i], t_old)

          # insert boundary conditions:
          up[0] = 0; up[n] = 0
          # update data structures for next step
          um = u.copy(); u = up.copy()
    return u

if __name__ == '__main__':

# When choosing the parameters you should also check that the units are correct

   c = 5100
   L = 1
   n = 10
   dt = 0.1
   tstop = 1
   a = solver0(I, f, c, L, n, dt, tstop)  
这是物理学中最重要的微分方程之一。这个偏微分方程或波的解由一个函数给出,该函数是空间和时间的函数
u(x,t)

将波的概念形象化,考虑空间和时间两个维度。如果您固定时间,例如t1,您将得到x的函数:

U(x) = U(x,t=t1) 
然而,在空间的特定点x1,波是时间的函数:

U(t) = U(x=x1, t)
这将帮助您了解波是如何传播的。为了找到一个解决方案,您需要施加一些初始和边界条件,以将所有可能的波限制为您感兴趣的波。对于这种特殊情况:

  • I=I(xi)
    是我们将用于获得 扰动/波浪移动
  • 术语
    f=f(x,t)
    说明了产生的任何外力 波浪
  • c
    为波速;它是一个常数(假设介质为 均质的)
  • L
    是我们要求解PDE的域的长度;也 常数
  • n
    是空间中栅格点的数量
  • dt
    是一个时间步长
  • tstop
    是停止时间

你真的需要问一个更具体的问题。“我从一本书中复制了这段代码,但不知道它的意思”在StackOverflow上不是一个好问题。如果你想知道这本书的代码在说什么,也许可以试着去读这本书。好吧,我买不到这本书(图书馆没有这本书,55.96美元的价格使买它成为不可能),但如果有帮助的话,我简化了我的问题。我真的只想要两个函数(I和f)。谢谢,Amber。你想研究一下波动方程吗?更具体地说,是什么?在实现它之前,你需要理解它背后的数学原理。我在Google的第一页“计算科学的Python脚本”中找到了它,没有引用:frs.web.cern.ch/frs/Source/PythonBook2004.pdfI制作了这段代码的动画。但是当我得到超过100个元素或者当我使用大时间步长时,我会有一种奇怪的行为。有人知道为什么吗?请参阅此处的代码-->更改c和L时也会出现问题。您遇到与CFL条件相关的不稳定性问题,此条件是基于因果关系对时间步长大小和空间步长大小的比率的限制。然后,用dx的大小相应地减小dt的值,也就是说,与元素的数量成反比。我用我在这里找到的代码做了一些事情,它是开着的
U(t) = U(x=x1, t)