Mesh grid in Julia

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

1 Like

Do you need the following examples?

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)))

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))

1 Like

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

using LinearAlgebra, SparseArrays, Plots, DifferentialEquations
using Surrogates
using Plots
x=range(0, stop=100, length=100);
# print("the vlues of t",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])

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

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

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


using Pkg
Pkg.status("GR"; mode=PKGMODE_MANIFEST)

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

VERSION = v"1.6.0"
      Status `C:\Users\manis\.julia\environments\v1.6\Project.toml`
  [91a5bcdd] Plots v1.20.0 ``
      Status `C:\Users\manis\.julia\environments\v1.6\Manifest.toml`
  [28b8d3ca] GR v0.57.4

I too have the same version

Corrected version of your code:

#using LinearAlgebra, SparseArrays, Plots, DifferentialEquations
#using Surrogates
using Plots
x=range(0, stop=100, length=100);
# print("the vlues of t",t)
T=zeros(length(x),length(t)) # t <--> x
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])
surface(t,x,T) # x <--> t


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

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:

@. 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

1 Like

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:

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

Code for verification:

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))

@show T ≈ U

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


T ≈ U = true

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

julia> T[:, 1]
julia> plot(T[:, 1])
julia> ?
help?> @.

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

Jupyter notebook:

Edit: Add Jupyter notebook.

1 Like