# Display function returning BoundsError

**URL:** <https://discourse.julialang.org/t/display-function-returning-boundserror/93548>\
**Category:** New to Julia\
**Tags:** question, plotting, plots\
**Created:** [January 26, 2023, 2:08am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548 "2023-01-26T02:08:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fda-tome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fda-tome/32/46248_2.png) [@fda-tome](https://discourse.julialang.org/u/fda-tome)\
**Post date:** [January 26, 2023, 2:08am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/1 "2023-01-26T02:08:37Z")

</div>

I am trying to display a plot using the code below, however i`m getting the following BoundsError

```julia
 BoundsError: attempt to access 256-element Vector{Float64} at index [257]

```

```julia
using SpecialFunctions
using AssociatedLegendrePolynomials
using Plots
setprecision(167)

function besbeam(psiamp, axiconang, order, rho, phi, z) 
    k = big(2 * pi) / big(1.54/1000) #fixed wavelength 1.54mm
    kz = k * big(cosd(axiconang))
    krho = k * big(sind(axiconang))
    return big(psiamp * besselj(order, krho * rho) * exp(order * im * phi) * exp(kz * im * z))
end

function makeplotbes(nx, ny, ry, rx, psiamp)
    z = 0
    x = range(-rx, rx, nx)
    y = range(-ry, ry, ny)
    axang = [deg2rad(1), deg2rad(10), deg2rad(40)]
    ord = [0, 1, 5]
    htmaps = [(heatmap([(abs(besbeam(psiamp, big(axiconang), order, big(sqrt(i^2 + j^2)), big(acos(i / big(sqrt(i^2 + j^2)))), z))) for i in x, j in y])) for order in ord, axiconang in axang]
    gr()
    display(plot(vec(htmaps)..., layout = (3, 3)))
end

makeplotbes(200, 200, 20, 20, 1)

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 10:45am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/2 "2023-01-26T10:45:28Z")

</div>

> [@fda-tome](#):
>
> `big(2 * pi) / big(1.54/1000)`

Unrelated to your question, but this lines (and all the lines below) is not really doing what you think it does: `big` promotes its argument to arbitrary precision, but if you perform some operations in double precision _before_ converting to `BigFloat` then you get a highly inaccurate result in arbitrary precision. Instead you want to convert your operands to `BigFloat` and then perform the operations. You can do something like

```julia
big(2) * big(pi) / (big(154) / big(100000))

```

or maybe simplify to

```julia
big(100000) * big(pi) / big(77)

```

The same in the lines below. Note also that you can use `cis(x)` instead of `exp(im *x)`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 26, 2023, 11:05am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/3 "2023-01-26T11:05:41Z")

</div>

I think there’s a bug with `heatmap` and `BigFloat`:

```julia
julia> heatmap(rand(BigFloat, 200, 200))
Error showing value of type Plots.Plot{Plots.GRBackend}:
ERROR: BoundsError: attempt to access 256-element Vector{Float64} at index [257]

```

If I change your code to convert the output of `besbeam` to `Float64` instead, I get a pretty picture:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/41786cc0f9ee7c032d7c79e76e047c246628f5ce.jpeg)

Don’t know if this is what you’re expecting, but if it is maybe that’s a sign you don’t need BigFloat at all (given as Mose says it’s not really working the way you think it is in your current code anyway).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 26, 2023, 11:06am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/4 "2023-01-26T11:06:46Z")

</div>

Turns out this is a known issue:

[https://github.com/JuliaPlots/Plots.jl/issues/4270](https://github.com/JuliaPlots/Plots.jl/issues/4270)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [January 26, 2023, 11:30am UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/5 "2023-01-26T11:30:02Z")

</div>

Since it’s a know issue with Plots.jl (but only for BigFloat, I would suggest not using it, maybe rather Float128 from a package or) I would suggest a different plotting package, e.g. Makie.jl. That seems to be the future, there are also many others, e.g. GR.jl directly (what Plots.jl uses by default, it’s an abstraction layer for many), or pyplot (very fast), also VegaLite if I recall. Or PythonPlot.jl.

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [January 26, 2023, 12:42pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/6 "2023-01-26T12:42:19Z")

</div>

Fixed by [fix `BigFloat` oob by t-bltg · Pull Request #145 · JuliaPlots/PlotUtils.jl · GitHub](https://github.com/JuliaPlots/PlotUtils.jl/pull/145).

![pl](https://global.discourse-cdn.com/julialang/original/3X/3/9/3979dee405cdb5608d6ba277abc5237e3ca02a45.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 26, 2023, 1:09pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/7 "2023-01-26T13:09:06Z")

</div>

> [@giordano](#):
>
> or maybe simplify to
> 
> ```julia
> big(100000) * big(pi) / big(77)
> 
> ```

Or just `100000 * big(pi) / 77`. Promotion will do the rest for you.

---

<div class="post-metadata">

**Author:** ![fda-tome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fda-tome/32/46248_2.png) [@fda-tome](https://discourse.julialang.org/u/fda-tome)\
**Post date:** [January 26, 2023, 11:04pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/8 "2023-01-26T23:04:15Z")

</div>

Hey, you’re right. Thank you very much, i’ll change it

---

<div class="post-metadata">

**Author:** ![fda-tome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fda-tome/32/46248_2.png) [@fda-tome](https://discourse.julialang.org/u/fda-tome)\
**Post date:** [January 26, 2023, 11:06pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/9 "2023-01-26T23:06:23Z")

</div>

Wow, i spent hours trying to understand what i was doing wrong thank you so much for explaining that it is a bug, turns out i’m not crazy after all 🙂

---

<div class="post-metadata">

**Author:** ![fda-tome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fda-tome/32/46248_2.png) [@fda-tome](https://discourse.julialang.org/u/fda-tome)\
**Post date:** [January 26, 2023, 11:07pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/10 "2023-01-26T23:07:20Z")

</div>

heeey nice, it worked! Thank you

---

<div class="post-metadata">

**Author:** ![fda-tome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fda-tome/32/46248_2.png) [@fda-tome](https://discourse.julialang.org/u/fda-tome)\
**Post date:** [January 26, 2023, 11:08pm UTC](https://discourse.julialang.org/t/display-function-returning-boundserror/93548/11 "2023-01-26T23:08:20Z")

</div>

yea i need to study and learn more about type hierarchy and promotion for julia. Thank you for the valuable advice.
