# Mesh grid in Julia

**URL:** <https://discourse.julialang.org/t/mesh-grid-in-julia/66363>\
**Category:** Performance\
**Tags:** plotting\
**Created:** [August 13, 2021, 8:49pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363 "2021-08-13T20:49:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [August 13, 2021, 8:49pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/1 "2021-08-13T20:49:28Z")

</div>

Hello People  
I have been working on heat equation simulation using Julia Software. I was stuck with creating a surface plot (3d plotting)  
I have values of x,t and T (is a matrix) how to make 3d plot  
Please help me

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 13, 2021, 9:01pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/2 "2021-08-13T21:01:40Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 13, 2021, 9:17pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/3 "2021-08-13T21:17:03Z")

</div>

Do you need the following examples?

```julia
using Plots

f(t, x) = exp(-x^2/(2t))/√(2π*t)
u(t, x) = f(t+0.1, x+2) + 2f(t+0.2, x-2)
t = range(0, 3; length=101)
x = range(-5, 5; length=101)
T = u.(t', x)

surface(t, x, T; colorbar=false,
    xlabel="t", ylabel="x", zlabel="T",
    camera=(30, 70), color=reverse(cgrad(:RdYlBu_11)))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f21293bed0cb88a0509d777f4db73c1055f60698.jpeg)

```julia
t = range(0, 3; length=51)
x = range(-5, 5; length=51)
T = u.(t', x)

wireframe(t, x, T; colorbar=false,
    xlabel="t", ylabel="x", zlabel="T",
    camera=(30, 70))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc0ff690565c38fb5b2c445030c592aa5832ea72.jpeg)

---

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [August 13, 2021, 9:34pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/4 "2021-08-13T21:34:42Z")

</div>

Hello Thanks for sharing the examples. But when I am trying to run this code I am finding this errors please help me to clear them

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b3c87977ee7469fbcf25b150d834d5d94ee6864.png)

---

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [August 13, 2021, 9:37pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/5 "2021-08-13T21:37:08Z")

</div>

```julia
using LinearAlgebra, SparseArrays, Plots, DifferentialEquations
using Surrogates
using Plots
gr()
L=20
N=100
k=50
x=range(0, stop=100, length=100);
dx=x[2]-x[1];
dt=0.5*dx^2/(2*k);
t=0:dt:10
# print("the vlues of t",t)
T=zeros(length(t),length(x))
s=size(T)
print("size of matrix",s)
T[1,2]=T[1,1]=100 #complete 1st row
T[end,end]=150 #last row of a matrix
 for i in 2:1960
    for j=1:99
        T[i,j+1]=T[i,j]+k*dt/(dx^2)*(T[i+1,j]-2*T[i,j] + T[i-1,j])
    end
end
surface(x,t,T)

```

This is my code  
I am trying to simulate the function. But i am finding the errors

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 13, 2021, 10:08pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/6 "2021-08-13T22:08:56Z")

</div>

I’m using Windows 10, and the output of the following code is

```julia
VERSION = v"1.6.2"
      Status `D:\.julia\environments\v1.6\Project.toml`
  [91a5bcdd] Plots v1.20.1
      Status `D:\.julia\environments\v1.6\Manifest.toml`
  [28b8d3ca] GR v0.58.1

```

Code:

```julia
@show VERSION
using Pkg
Pkg.status("Plots")
Pkg.status("GR"; mode=PKGMODE_MANIFEST)

```

If you have older versions, I suggest you upgrade and try again.

---

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [August 13, 2021, 10:17pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/7 "2021-08-13T22:17:03Z")

</div>

```julia
VERSION = v"1.6.0"
      Status `C:\Users\manis\.julia\environments\v1.6\Project.toml`
  [91a5bcdd] Plots v1.20.0 `https://github.com/JuliaPlots/Plots.jl.git#master`
      Status `C:\Users\manis\.julia\environments\v1.6\Manifest.toml`
  [28b8d3ca] GR v0.57.4

```

I too have the same version

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 13, 2021, 10:41pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/8 "2021-08-13T22:41:07Z")

</div>

Corrected version of your code:

```julia
#using LinearAlgebra, SparseArrays, Plots, DifferentialEquations
#using Surrogates
using Plots
#gr()
L=20
N=100
k=50
x=range(0, stop=100, length=100);
dx=x[2]-x[1];
dt=0.5*dx^2/(2*k);
t=0:dt:10
# print("the vlues of t",t)
#T=zeros(length(t),length(x))
T=zeros(length(x),length(t)) # t <--> x
s=size(T)
print("size of matrix",s)
#T[1,2]=T[1,1]=100 #complete 1st row
#T[end,end]=150 #last row of a matrix
@. T[:, 1] = exp(-(x-50)^2/(2*5^2)) # initial values
#for i in 2:1960
for j in 1:1960 # i -> j
# for j=1:99
    for i=2:99 # j -> i, 1:99 -> 2:99
        T[i,j+1]=T[i,j]+k*dt/(dx^2)*(T[i+1,j]-2*T[i,j] + T[i-1,j])
    end
end
#surface(x,t,T)
surface(t,x,T) # x <--> t

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a35a41e0613e64232886826c9a2c646178949b5.jpeg)

---

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [August 14, 2021, 7:49am UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/9 "2021-08-14T07:49:48Z")

</div>

> [@genkuroki](#):
>
> `@. T[:, 1] = exp(-(x-50)^2/(2*5^2)) # initial values`

Can you please explain me this line. I havent understood this one as I am new to the Julia

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [August 14, 2021, 10:36am UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/10 "2021-08-14T10:36:13Z")

</div>

To discover what a symbol means you can type a `?` in the REPL. This will get you into the help mode of the RPEL. Once you are in there, you can type the symbol you want to undersantd and click enter.

doing `? @.` enter gives me:

```julia
@. expr

  Convert every function call or operator in expr into a "dot call" (e.g.
  convert f(x) to f.(x)), and convert every assignment in expr to a "dot
  assignment" (e.g. convert += to .+=).

  If you want to avoid adding dots for selected function calls in expr, splice
  those function calls in with $. For example, @. sqrt(abs($sort(x))) is
  equivalent to sqrt.(abs.(sort(x))) (no dot for sort).

  (@. is equivalent to a call to @ __dot__.)

```

So your expression is equivalent to `T[:, 1] .= exp.(-(x .- 50) .^ 2 ./ ( 2 .* 5 .^2)) # initial values`

This basically assigns to each element of `T[:,1]` the value of the function evaluated at each point in the vector `x`.

The dot notation is broadcasting. You can understand it if you go through this section of the maual. [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators)

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 14, 2021, 2:08pm UTC](https://discourse.julialang.org/t/mesh-grid-in-julia/66363/11 "2021-08-14T14:08:38Z")

</div>

Under the condition `T=zeros(length(x),length(t))`, the code `@. T[:, 1] = exp(-(x-50)^2/(2*5^2))` is equivalent to the following for loop:

```julia
for i in eachindex(x)
    T[i, 1] = exp(-(x[i]-50)^2/(2*5^2))
end

```

Code for verification:

```julia
using Plots

k = 50
x = range(0, 100; length=100);
dx = step(x)
dt = 0.5*dx^2/(2k)
t = 0:dt:10
T = zeros(length(x), length(t))
U = zeros(length(x), length(t))

@. T[:, 1] = exp(-(x-50)^2/(2*5^2))

for i in eachindex(x)
    U[i, 1] = exp(-(x[i]-50)^2/(2*5^2))
end

@show T ≈ U

plot(x, T[:, 1]; label="T[:,1]", lw=2)
plot!(x, U[:, 1]; label="U[:,1]", lw=2, ls=:dash)

```

Output:

```julia
T ≈ U = true

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/88d14308c4e3e1985f59cbf4f01b86c1efdf6dbf.jpeg)

You could verify the results of the following code with REPL.

```julia
julia> T[:, 1]

```

```julia
julia> plot(T[:, 1])

```

```julia
julia> ?
help?> @.

```

It will be easier to use Julia if you always ask REPL❗️

**Jupyter notebook:** [https://github.com/genkuroki/public/blob/main/0015/Surrogates/3D%20plot%20example%20-%20heat%20eq.ipynb](https://github.com/genkuroki/public/blob/main/0015/Surrogates/3D%20plot%20example%20-%20heat%20eq.ipynb)

Edit: Add Jupyter notebook.
