# Need help with array calculation

**URL:** https://discourse.julialang.org/t/need-help-with-array-calculation/38423
**Category:** New to Julia
**Created:** [April 29, 2020, 1:40pm UTC](https://discourse.julialang.org/t/need-help-with-array-calculation/38423 "2020-04-29T13:40:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [April 29, 2020, 1:40pm UTC](https://discourse.julialang.org/t/need-help-with-array-calculation/38423/1 "2020-04-29T13:40:50Z")

</div>

I would like to ask for help about array calculation

I want to plot contourline of f in which:

```julia
    f = q + p*ln(p )

```

To do that,

```julia
    n = 100
    p = range(0, stop = 200, length =n )
    q = range(0, stop = 200, length = n)
    p_grid = repeat(p, 1, n)
    q_grid = repeat(q', n, 1)

```

Then, how to generate f\_grid? (` f = q + p*ln(p )`)  
If possible, I can use PyPlot to plot:

```julia
  contour(p_grid, q_grid, f_grid, levels=0);

```

( in case of f2 : `f2 = q²+ p*(p - 1)`  
I could create: ` f2_grid = (q_grid.^2 + p_grid.^2 - p_grid`)

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [April 29, 2020, 2:07pm UTC](https://discourse.julialang.org/t/need-help-with-array-calculation/38423/2 "2020-04-29T14:07:04Z")

</div>

First, let’s define `f` as an actual function:

```julia
julia> f = (x, y) -> x + y * log(y)
#11 (generic function with 1 method)

```

You can broadcast this function over p and q to create a grid:

```julia
julia> f.(p, q')
100×100 Array{Float64,2}:
...

```

But you actually don’t need to! In fact, Plots.jl already knows how to plot the contour of a _function_ if you just pass it the input vectors `p` and `q` and the function itself:

```julia
julia> using Plots

julia> n = 100
100

julia> q = range(0, stop = 200, length = n)
0.0:2.0202020202020203:200.0

julia> p = range(0, stop = 200, length =n )
0.0:2.0202020202020203:200.0

julia> f = (x, y) -> x + y * log(y)
#11 (generic function with 1 method)

julia> contour(p, q, f)

```

Note that we also don’t need to generate `p_grid` or `q_grid` because Plots.jl is smart enough to work directly from the vectors.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 29, 2020, 2:20pm UTC](https://discourse.julialang.org/t/need-help-with-array-calculation/38423/3 "2020-04-29T14:20:53Z")

</div>

Because of the way broadcasting works you very rarely need to define grids like `p_grid` and `q_grid`. So if you find yourself using `repeat` on a vector, there’s a good chance you’re doing something unnecessary.

In fact the same is true for both Matlab and numpy too, so this technique is quite outdated these days.

---

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [April 29, 2020, 2:46pm UTC](https://discourse.julialang.org/t/need-help-with-array-calculation/38423/4 "2020-04-29T14:46:02Z")

</div>

Fantastic! Thank you very much for you quick reply.  
I have only used PyPlot. This may makes me switch to Plots!
