GalWat

Untitled

Jun 14th, 2021
425
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def runge_kutte4_2dim(f1, f, x_bounds, y0, z0, h):
  2.     x0 = x_bounds[0]
  3.     result = [y0]
  4.     derivative = [z0]
  5.  
  6.     while x0 < x_bounds[1]:
  7.         k1 = h * f(x0, y0, z0)
  8.         m1 = h * f1(x0, y0, z0)
  9.         k2 = h * f(x0 + h / 2, y0 + m1 / 2, z0 + k1 / 2)
  10.         m2 = h * f1(x0 + h / 2, y0 + m1 / 2, z0 + k1 / 2)
  11.         k3 = h * f(x0 + h / 2, y0 + m2 / 2, z0 + k2 / 2)
  12.         m3 = h * f1(x0 + h / 2, y0 + m2 / 2, z0 + k2 / 2)
  13.         k4 = h * f(x0 + h, y0 + m3, z0 + k3)
  14.         m4 = h * f1(x0 + h, y0 + m3, z0 + k3)
  15.  
  16.         y0 += (m1 + 2 * m2 + 2 * m3 + m4) / 6
  17.         z0 += (k1 + 2 * k2 + 2 * k3 + k4) / 6
  18.         x0 += h
  19.  
  20.         result.append(y0)
  21.         derivative.append(z0)
  22.  
  23.     return [result, derivative]
  24.  
  25.  
  26. def adams_2dim(f1, f, x_bounds, y0, z0, h):
  27.     x0 = x_bounds[0]
  28.     half = int((x_bounds[1] - x0) / h)
  29.     result = [y0]
  30.     derivative = [z0]
  31.  
  32.     runge = runge_kutte4_2dim(f1, f, x_bounds, y0, z0, h)
  33.  
  34.     for i in range(1, 4):
  35.         x0 += h
  36.         result.append(float(runge[0][i]))
  37.         derivative.append(runge[1][i])
  38.  
  39.     y = runge[0][3]
  40.     z = runge[1][3]
  41.  
  42.     for i in range(4, half + 1):
  43.         z += h * (55 * f(x0 - h, result[i - 1], derivative[i - 1]) - 59 * f(x0 - 2 * h, result[i - 2], derivative[i - 2]) + 37 * f(x0 - 3 * h, result[i - 3], derivative[i - 3]) - 9 * f(x0 - 4 * h, result[i - 4], derivative[i - 4])) / 24
  44.         y += h * (55 * f1(x0 - h, result[i - 1], derivative[i - 1]) - 59 * f1(x0 - 2 * h, result[i - 2], derivative[i - 2]) + 37 * f1(x0 - 3 * h, result[i - 3], derivative[i - 3]) - 9 * f1(x0 - 4 * h, result[i - 4], derivative[i - 4])) / 24
  45.         x0 += h
  46.  
  47.         derivative.append(z)
  48.         result.append(y)
  49.  
  50.     return result
RAW Paste Data