How can I solve this error to plot those two plot side by side?

Viewed 38

I have tried this code to plot these two side by side.

Edited

using StatsBase, Plots
    using Distributions
    n= 21600
    P = M = Vector{Int}(undef,n)
    P[1] =100 
    for second in 2:n
      toss = sample(["H","T"], Weights([0.5,0.5]))
      M[second] = rand(Poisson(5),1)
      if toss=="H" 
          P[second]=P[second-1]+M[second]
      else
          P[second]=P[second-1]-M[second]
      end
    end
    
    plot1((P))
    hline!([0])
    
    plot2((P))
    hline!([0])
    
    plot(plot1, plot2, layout = (1, 2), legend = false)

This is my code. Getting error as

ArgumentError: invalid index: sec of type typeof(sec)

How to fix this?

1 Answers

You appear to have mixed up sec and second in your loop. sec is also a function name https://docs.julialang.org/en/v1/base/math/#Base.Math.sec-Tuple{Number}. Replace each instance of sec with second


using StatsBase, Plots, Distributions
n= 21600
P = Vector{Int}(undef,n)
M = similar(P) #unless you want P and M to point to the same location in memory
# could also do P,M  = Vector{Int}(undef,n),Vector{Int}(undef,n)
P[1] =100 
M[1] =100 
for second in 2:n
   toss = sample(["H","T"], Weights([0.5,0.5]))
   M[second] = rand(Poisson(5)) #puting rand(Poisson(5),1) gives a vector of length 1
   if toss=="H" 
       P[second]=P[second-1]+M[second]
   else
       P[second]=P[second-1]-M[second]
   end
end
    
plot1 = plot(P)
hline!([0])    
plot2 = plot(M)
hline!([0])
    
plot(plot1, plot2, layout = (1, 2), legend = false)

Related