Strange behavior using Plots / array initialization

Dear all,

I just want to share a strange behavior which I hope you can reproduce (IJulia environment, Julia 1.0.1). Please find below my code which most of the time leads to a correct plot. But if you run the code repeatedly it ends up plotting something weird or even with an error (ArgumentError: At least one finite value must be provided to formatter).

But this only happens if I define an uninitialized array. If I define a zero filled array, all is fine . Thanks for your help…

using Plots
dt = 1.0/100.0
Fz = Array{Float64}(undef, 50)
t = Array{Float64}(undef, 50)
#Fz = zeros(Float64, 50)
#t = zeros(Float64, 50)

for i = 1:length(Fz)
    t[i] = (i-1)*dt    
    Fz[i] = Fz[i]+sin(pi*fs*t[i])    
end

plot(t,Fz)

The Array{Float64}(undef, 50) is extra awkward to make it clear, that the content get’s filled with undefined numbers.
Did you try at all to actually inspect t Fz before plotting?

Since you access Fz in Fz[i] = Fz[i]+sin(pi*fs*t[i]) before assigning it a value, you can get any number there is. E.g. if it’s all NaNs, you’ll get an array full of NaNs!

2 Likes

Ok, got that. I understand and also see it now. Thanks