Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.78 KB | None | 0 0
  1.  
  2. using PyPlot
  3.  
  4.  
  5. function simulation(N::Int64)
  6.  
  7.   E1 = zeros(Float64,N)
  8.   Eq1 = zeros(Float64,N)
  9.   V1 = zeros(Float64,N)
  10.   D1 = zeros(Float64,N)
  11.  
  12.   E2 = zeros(Float64,N)
  13.   Eq2 = zeros(Float64,N)
  14.   V2 = zeros(Float64,N)
  15.   D2 = zeros(Float64,N)
  16.  
  17.   for n = 1:N
  18.  
  19.     for step = 1:5
  20.       coin = rand(1:2)
  21.       tmpE = 0.0
  22.       # coin == 1 | tails
  23.       # coin == 2 | heads
  24.       if coin == 1
  25.         E1[n] += 11.0
  26.         tmpE = 11.0
  27.       else
  28.         E1[n] -= 110.0
  29.         tmpE = -110.0
  30.       end
  31.       Eq1[n] = tmpE^2
  32.     end
  33.  
  34.     V1[n] = Eq1[n] - (E1[n])^2
  35.     D1[n] = sqrt(V1[n])
  36.  
  37.     for step = 1:1100
  38.       coin = rand(1:2)
  39.       tmpE2 = 0.0
  40.       # coin == 1 | tails
  41.       # coin == 2 | heads
  42.       if coin == 1
  43.         E2[n] -= 0.5
  44.         tmpE = -0.5
  45.       else
  46.         E2[n] += 0.05
  47.         tmpE = 0.05
  48.       end
  49.       Eq2[n] = tmpE^2
  50.     end
  51.  
  52.     V2[n] = Eq2[n] - E2[n]^2
  53.     D2[n] = sqrt(V2[n])
  54.   end
  55.   printDiagram(E1, D1, 0)
  56.   printDiagram(E2, D2, 1)
  57. end
  58.  
  59. function printDiagram(E::Array, D::Array, case::Int64)
  60.  
  61.   D1 = zeros(Float64,N)
  62.   D2 = zeros(Float64,N)
  63.   for i = 1:length(D)
  64.     D1[i] = D[i] + 3
  65.     D2[i] = D[i] - 3
  66.   end
  67.   msg = ""
  68.   if case == 0
  69.     msg = " game number 1"
  70.   elseif case == 1
  71.     msg = " game number 2"
  72.   end
  73.   x = 1:N
  74.   fig, ax = subplots()
  75.   suptitle("Zad1")
  76.   #ax[:plot](x, E, linewidth=2, alpha=0.6, label="Average value of$msg")
  77.   #ax[:plot](x, V, linewidth=2, alpha=0.6, label="Variation of$msg")
  78.   ax[:plot](x, D1, linewidth=2, alpha=0.6, label="(+)Deviation of$msg")
  79.   ax[:plot](x, D2, linewidth=2, alpha=0.6, label="(-)Deviation of$msg")
  80.   ax[:legend]()
  81.   fileName = string("lab4_1_$msg.png")
  82.   savefig(fileName, dpi=72)
  83.  
  84. end
  85.  
  86. println("START")
  87.  
  88. N = 1000
  89. simulation(N)
  90.  
  91. println("END")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement