日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

c语言解一维波动方程,python绘制一维波动方程(初学者)

發布時間:2025/4/16 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言解一维波动方程,python绘制一维波动方程(初学者) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我剛開始學習如何用Python編碼.我有興趣

>如何復制u [x,t]矩陣.我嘗試返回u,這使我出錯.

>如果此代碼中for循環的位置正確并且可以正常運行.

>最重要的是,如何為該一維波方程制作動畫,在其中我可以看到波是如何從高斯演化成并分裂成兩個相同高度的波的.

這是我的代碼:

import numpy as np

import matplotlib.pyplot as plt

dx=0.1 #space increment

dt=0.05 #time increment

tmin=0.0 #initial time

tmax=2.0 #simulate until

xmin=-5.0 #left bound

xmax=5.0 #right bound...assume packet never reaches boundary

c=1.0 #speed of sound

rsq=(c*dt/dx)**2 #appears in finite diff sol

nx = int((xmax-xmin)/dx) + 1 #number of points on x grid

nt = int((tmax-tmin)/dt) + 2 #number of points on t grid

u = np.zeros((nt,nx)) #solution to WE

#set initial pulse shape

def init_fn(x):

val = np.exp(-(x**2)/0.25)

if val<.001:>

return 0.0

else:

return val

for a in range(0,nx):

u[0,a]=init_fn(xmin+a*dx)

u[1,a]=u[0,a]

#simulate dynamics

for t in range(1,nt-1):

for a in range(1,nx-1):

u[t+1,a] = 2*(1-rsq)*u[t,a]-u[t-1,a]+rsq*(u[t,a-1]+u[t,a+1])

# Where is the code that is needed to run the simulation?

我看到一些動畫代碼對我來說太復雜了.有人可以幫我解決上面提到的問題嗎?謝謝!

解決方法:

在文件末尾執行以下操作:

fig = plt.figure()

plts = [] # get ready to populate this list the Line artists to be plotted

plt.hold("off")

for i in range(nt):

p, = plt.plot(u[i,:], 'k') # this is how you'd plot a single line...

plts.append( [p] ) # ... but save the line artist for the animation

ani = animation.ArtistAnimation(fig, plts, interval=50, repeat_delay=3000) # run the animation

ani.save('wave.mp4') # optionally save it to a file

plt.show()

這是mp4的gif:

標簽:matplotlib,animation,waveform,python,numpy

來源: https://codeday.me/bug/20191121/2049615.html

總結

以上是生活随笔為你收集整理的c语言解一维波动方程,python绘制一维波动方程(初学者)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。