Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using PyPlot
- function simulation(N::Int64)
- E1 = zeros(Float64,N)
- Eq1 = zeros(Float64,N)
- V1 = zeros(Float64,N)
- D1 = zeros(Float64,N)
- E2 = zeros(Float64,N)
- Eq2 = zeros(Float64,N)
- V2 = zeros(Float64,N)
- D2 = zeros(Float64,N)
- for n = 1:N
- for step = 1:5
- coin = rand(1:2)
- # coin == 1 | tails
- # coin == 2 | heads
- tmp = 0
- if coin == 1
- tmp = 11
- else
- tmp = -110
- end
- E1[n] += tmp
- Eq1[n] += tmp^2
- end
- E1[n] /= 5
- Eq1[n] /= 5
- V1[n] = Eq1[n] - (E1[n])^2
- D1[n] = sqrt(V1[n])
- for step = 1:1100
- coin = rand(1:2)
- # coin == 1 | tails
- # coin == 2 | heads
- tmp1 = 0
- if coin == 1
- tmp1 -= 0.5
- else
- tmp1 += 0.05
- end
- E2[n] += tmp1
- Eq2[n] += tmp1^2
- end
- E2[n] /= 1100
- Eq2[n] /= 1100
- V2[n] = Eq2[n] - (E2[n])^2
- D2[n] = sqrt(V2[n])
- end
- printDiagram(E1, V1, D1, 0)
- printDiagram(E2, V2, D2, 1)
- end
- function Var(E::Array, Eq::Array)
- var = 0
- for i = 1:length(E)
- var = Eq[i] - (E[i])^2
- end
- return var
- end
- function Deviation(Var::Float64)
- return sqrt(Var)
- end
- function printDiagram(E::Array, V::Array, D::Array, case::Int64)
- D1 = zeros(Float64,N)
- D2 = zeros(Float64,N)
- for i = 1:length(D)
- D1[i] = D[i] + 3
- D2[i] = D[i] - 3
- end
- msg = ""
- if case == 0
- msg = " game number 1"
- elseif case == 1
- msg = " game number 2"
- end
- x = 1:N
- fig, ax = subplots()
- suptitle("Zad1")
- ax[:plot](x, E, linewidth=2, alpha=0.6, label="Average value of$msg")
- ax[:plot](x, V, linewidth=2, alpha=0.6, label="Variation of$msg")
- ax[:plot](x, D1, linewidth=2, alpha=0.6, label="(+)Deviation of$msg")
- ax[:plot](x, D2, linewidth=2, alpha=0.6, label="(-)Deviation of$msg")
- ax[:legend]()
- fileName = string("lab4_$msg.png")
- savefig(fileName, dpi=72)
- end
- println("START")
- N = 100000
- simulation(N)
- println("END")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement