Guest User

Untitled

a guest
Aug 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # x
  2. X = 5.0
  3. xlm = [-X,X]
  4. dx = 0.1
  5. x = xlm[1]:dx:xlm[2]
  6. x_n = length(x)
  7.  
  8. # t
  9. T = 50.0
  10. tlm = [0,T]
  11. dt = 0.1
  12. t = tlm[1]:dt:tlm[2]
  13. t_n = length(t)
  14.  
  15. # パラメータ
  16. D = 0.05
  17. α = D * dt / dx^2
  18.  
  19. # 初期条件
  20. f(x) = exp(-x^2)
  21.  
  22. # 初期化
  23. u = zeros(x_n, t_n)
  24.  
  25. # 境界条件
  26. u[1, :] = 0.0
  27. u[end,:] = 0.0
  28.  
  29. # 初期条件
  30. u[:,1] = f.(x)
  31.  
  32. for j = 1:t_n-1
  33. for i = 2:x_n-1
  34. u[i,j+1] = (1.0 - 2.0 * α) * u[i,j] + α * ( u[i+1,j] + u[i-1,j] )
  35. end
  36. end
  37.  
  38. # plot
  39. #----------------------------------------------
  40. using PyPlot
  41. using PyCall
  42. @pyimport matplotlib.animation as anim
  43.  
  44. # figure
  45. fig = figure(figsize=(4, 4))
  46. # line
  47. line = plot([],[])[1]
  48. # setting
  49. xlim(xlm)
  50. ylim([-1,1])
  51. grid()
  52. xlabel("x")
  53. ylabel("u")
  54. tight_layout()
  55. grid("on",linestyle="--")
  56.  
  57. # update
  58. function update(i)
  59. line[:set_data](x, u[:,i+1])
  60. # title
  61. title(@sprintf("%4.2f",t[i+1]))
  62. end
  63. # animation
  64. myanim = anim.FuncAnimation(fig, update, frames=t_n, interval=10)
  65. # save
  66. myanim[:save]("Diffusion_1d_v0.6.gif", writer="imagemagick")
Add Comment
Please, Sign In to add comment