Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Oct 17 14:22:59 2019
  4.  
  5. @author: gullholm
  6. """
  7. # Q1
  8. import random
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. # First makes vector with the resolution and size of the grid
  12. N = 10
  13. size = [0]*N
  14. res = [0]*N
  15. ress = [0]*N
  16.  
  17. for n in range(N):
  18. size[n] = 2**(n+1)
  19. res[n] = 2**-(n+1)
  20.  
  21. # makes brownian motion for finest grid 2^-10
  22. Z = np.zeros(size[9])
  23. sigma = res[9]
  24. Z = sigma*np.random.randn(1024)
  25. Z[0] = 0
  26. B_10 = np.cumsum(Z)
  27. for i in range(N):
  28. ress[i] = np.arange(0,1, res[i])
  29.  
  30.  
  31.  
  32. def BM(B): # function that's makes the bigger grids
  33. lgd = int(len(B)/2)
  34. B_new = [0]*lgd
  35. for i in range(1,lgd):
  36. B_new[i] = (1/2)*(B[i]+B[i+1]-B[i-1]) # each coarser grid is the mean of two finer
  37. return(B_new)
  38.  
  39.  
  40. B_9 = BM(B_10)
  41. B_8 = BM(B_9)
  42. B_7 = BM(B_8)
  43. B_6 = BM(B_7)
  44. B_5 = BM(B_6)
  45. B_4 = BM(B_5)
  46. B_3 = BM(B_4)
  47. B_2 = BM(B_3)
  48. B_1 = BM(B_2)
  49.  
  50.  
  51. plt.plot(ress[9], B_10)
  52. plt.plot(ress[8], B_9)
  53. plt.plot(ress[7], B_8)
  54. plt.plot(ress[6], B_7)
  55. plt.plot(ress[5], B_6)
  56. plt.plot(ress[4], B_5)
  57. plt.plot(ress[3], B_4)
  58. plt.plot(ress[2], B_3)
  59. plt.plot(ress[1], B_2)
  60. plt.plot(ress[0], B_1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement