Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. def fd_8(x, v, w, t, a, b):
  2. """Implements the shooting method to solve linear second order BVPs
  3.  
  4. Compute finite difference solution to the BVP but with the 8-order formula!
  5.  
  6.  
  7. t should be passed in as an n element array. x, v, and w should be
  8. either n element arrays corresponding to x(t), v(t) and w(t) or
  9. scalars, in which case an n element array with the given value is
  10. generated for each of them.
  11.  
  12. USAGE:
  13. u'' = x(t) + v(t) u + w(t) u'
  14. u = fd(x, v, w, t, a, b)
  15.  
  16. INPUT:
  17. x,v,w - arrays containing x(t), v(t), and w(t) values. May be
  18. specified as Python lists, NumPy arrays, or scalars. In
  19. each case they are converted to NumPy arrays.
  20. t - array of n time values to determine u at
  21. a - solution value at the left boundary: a = u(t[0])
  22. b - solution value at the right boundary: b = u(t[n-1])
  23.  
  24. OUTPUT:
  25. u - array of solution function values corresponding to the
  26. values in the supplied array t.
  27. """
  28.  
  29. # Get the dimension of t and make sure that t is an n-element vector
  30.  
  31. if type(t) != np.ndarray:
  32. if type(t) == list:
  33. t = np.array(t)
  34. else:
  35. t = np.array([ float( t ) ])
  36.  
  37. n = len(t)
  38.  
  39. # Make sure that x, v, and w are either scalars or n-element vectors.
  40. # If they are scalars then we create vectors with the scalar value in
  41. # each position.
  42.  
  43. if type(x) == int or type(x) == float:
  44. x = np.array([float(x)] * n)
  45.  
  46. if type(v) == int or type(v) == float:
  47. v = np.array([float(v)] * n)
  48.  
  49. if type(w) == int or type(w) == float:
  50. w = np.array([float(w)] * n)
  51.  
  52. # Compute the stepsize. It is assumed that all elements in t are
  53. # equally spaced.
  54.  
  55. h = t[1] - t[0];
  56.  
  57. if (n > 7):
  58. # Construct tridiagonal system; boundary conditions appear as first and
  59. # last equations in system.
  60.  
  61. #A = -( 1.0 + w[1:n] * h / 2.0 )
  62.  
  63. A = - (490/(180*(h**2))) - v
  64.  
  65. A[1:3] = -(2/h**2) - v[1:3]
  66. A[-3:-1] = -(2/h**2) - v[-3:-1]
  67. A[0] = A[-1] = 1.0
  68.  
  69. B = np.zeros(n-1)
  70. for i in range(n-1):
  71. B[i] = 270/(180*(h**2))
  72. B[0:2] = B[-3:-1] = B[-1] = 1/h**2
  73. B[-1] = 0.0
  74.  
  75. C = B[::-1].copy()
  76.  
  77. D = np.zeros(n-2)
  78. for i in range(n-2):
  79. D[i] = - (27/(180*(h**2)))
  80. D[0] = 0.0
  81. D[-3:-1] = D[-1] = 0.0
  82.  
  83. E = D[::-1].copy()
  84.  
  85. F = np.zeros(n-3)
  86. for i in range(n-3):
  87. F[i] = 2/(180*(h**2))
  88. F[-3:-1] = F[-1] = 0.0
  89.  
  90. G = F[::-1].copy()
  91.  
  92. H = x
  93. H[0] = a
  94. H[-1] = b
  95. '''
  96. # Solve tridiagonal system?? How?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement