Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.02 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.       # coin == 1 | tails
  22.       # coin == 2 | heads
  23.       tmp = 0
  24.       if coin == 1
  25.         tmp = 11
  26.       else
  27.         tmp = -110
  28.       end
  29.       E1[n] += tmp
  30.       Eq1[n] += tmp^2
  31.     end
  32.     #E1[n] /= 5.0
  33.     #Eq1[n] /= 5.0
  34.  
  35.     V1[n] = Eq1[n] - (E1[n])^2
  36.     D1[n] = sqrt(V1[n])
  37.  
  38.     for step = 1:1100
  39.       coin = rand(1:2)
  40.       # coin == 1 | tails
  41.       # coin == 2 | heads
  42.       tmp1 = 0
  43.       if coin == 1
  44.         tmp1 -= 0.5
  45.       else
  46.         tmp1 += 0.05
  47.       end
  48.       E2[n] += tmp1
  49.       Eq2[n] += tmp1^2
  50.     end
  51.  
  52.     #E2[n] /= 1100
  53.     #Eq2[n] /= 1100
  54.  
  55.     V2[n] = Eq2[n] - (E2[n])^2
  56.     D2[n] = sqrt(V2[n])
  57.   end
  58.   println(E2)
  59.   printDiagram(E1, V1, D1, 0)
  60.   printDiagram(E2, V2, D2, 1)
  61. end
  62.  
  63. function Var(E::Array, Eq::Array)
  64.   var = 0
  65.   for i = 1:length(E)
  66.     var = Eq[i] - (E[i])^2
  67.   end
  68.   return var
  69. end
  70.  
  71.  
  72. function Deviation(Var::Float64)
  73.     return sqrt(Var)
  74. end
  75.  
  76. function printDiagram(E::Array, V::Array, D::Array, case::Int64)
  77.  
  78.   D1 = zeros(Float64,N)
  79.   D2 = zeros(Float64,N)
  80.   for i = 1:length(D)
  81.     D1[i] = D[i] + 3
  82.     D2[i] = D[i] - 3
  83.   end
  84.   msg = ""
  85.   if case == 0
  86.     msg = " game number 1"
  87.   elseif case == 1
  88.     msg = " game number 2"
  89.   end
  90.   x = 1:N
  91.   fig, ax = subplots()
  92.   suptitle("Zad1")
  93.  
  94.   ax[:plot](x, E, linewidth=2, alpha=0.6, label="Average value of$msg")
  95.   ax[:plot](x, V, linewidth=2, alpha=0.6, label="Variation of$msg")
  96.   ax[:plot](x, D1, linewidth=2, alpha=0.6, label="(+)Deviation of$msg")
  97.   ax[:plot](x, D2, linewidth=2, alpha=0.6, label="(-)Deviation of$msg")
  98.   ax[:legend]()
  99.   fileName = string("lab4_$msg.png")
  100.   savefig(fileName, dpi=72)
  101.  
  102. end
  103.  
  104. println("START")
  105.  
  106. N = 100000
  107. simulation(N)
  108.  
  109. println("END")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement