Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. import csv
  5. x1 = 0
  6. x2 = 1
  7.  
  8. t1 = 0
  9. t2 = 2
  10.  
  11. N = 100
  12. M = 200
  13.  
  14. y = np.zeros((N, M), dtype=float)
  15. x = np.linspace(x1, x2, N)
  16. t = np.linspace(t1, t2, M)
  17.  
  18.  
  19. h = - (x1-x2)/(N-1)
  20. tau = (t2 - t1)/(M-1)
  21. e = 0.00001
  22. y[:, 0] = x**2 + 1
  23. y[0, :] = np.exp(-t)
  24. print(y)
  25.  
  26.  
  27. def q(x):
  28.     return np.arctan(1+x**2)
  29.  
  30.  
  31. print('here is q(x)', q(x))
  32.  
  33.  
  34. def k(x):  #dq(x)
  35.     return 2*x/(1+(1+x**2)**2)
  36.  
  37.  
  38. print('here is k(x)', k(x))
  39.  
  40.  
  41. def f(x, a, b):
  42.     return (x-a)/tau + (q(x) - q(b))/h
  43.  
  44.  
  45. #print('here is f(x, q, b)', f(x, q, b))
  46.  
  47.  
  48. def df(x):
  49.     return 1/tau+k(x)/h
  50.  
  51.  
  52. print('here is df(x)', df(x))
  53.  
  54.  
  55. def wellhuh(a, b):
  56.     result = b
  57.     d = e+1
  58.     while d > e:
  59.         y = result
  60.         result = y - f(y, a, b)/df(y)
  61.         d = abs(y - result)
  62.         return result
  63.  
  64.  
  65. for i in range(1, N):
  66.     for j in range(1, M):
  67.         y[i, j] = wellhuh(y[i, j-1], y[i-1, j])
  68.  
  69.  
  70. fig = plt.figure()
  71. ax = fig.gca(projection='3d')
  72. t, x = np.meshgrid(t, x)
  73. surf = ax.plot_surface(x, t, y, cmap='magma')
  74.  
  75.  
  76. ax.set_xlabel('x')
  77. ax.set_ylabel('t')
  78. ax.set_zlabel('u')
  79.  
  80.  
  81. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement