Bounds error: attempt to access 21-element array

I am trying to get an output of the function in array form.

#Additionally, I am unable to ask my questions nicely in the post, as in I am unable to write cells.

Try

x=-10:10
y=gaussian.(x)

Note the “.” after gaussian. Array indices start at 1 in Julia. In your for loop i goes from -10 to 10 and you are trying to assign y[-10], which does not exist.

1 Like

Thank you that worked. Now I was trying to plot the same using

using Plots
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
plot(x,y,linewidth=1.5, linecolor=:blue)

And it shows following error

Cannot convert Float64 to series data for plotting

Stacktrace:
[1] error(::String) at ./error.jl:33
[2] _prepare_series_data(::Float64) at /home/jupyterlab/.julia/packages/RecipesPipeline/wolJ9/src/series.jl:8
[3] _series_data_vector(::Float64, ::Dict{Symbol,Any}) at /home/jupyterlab/.julia/packages/RecipesPipeline/wolJ9/src/series.jl:27
[4] macro expansion at /home/jupyterlab/.julia/packages/RecipesPipeline/wolJ9/src/series.jl:139 [inlined]
[5] apply_recipe(::Dict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::Array{Int64,1}, ::Float64, ::Nothing) at /home/jupyterla

Blockquote

I don’t know much about Jupyter but the following should work:

using Plots
function gaussian(x)
    exp(-(x)^2)
end
x = -10:10
y=gaussian.(x)
plot(x,y,linewidth=1.5, linecolor=:blue)

At least it works in Atom.

1 Like

Thanks it worked !