Your code is hard to read with the formatting but it looks like you are using a lot of global variables in your code. Put everything in functions and pass the variables to the functions rather than using global variables.
function energy(Time, N, T, Th, Fo, dt, Tc, h)
for iter = 1:Time
Tn = T
for i = 2:N-1
for j = 2:N-1
T[i, j] = Tn[i, j] + Fo * dt * ((Tn[i+1, j] - 2 * Tn[i, j] + Tn[i-1, j]) / h + (Tn[i, j+1] - 2 * Tn[i, j] + Tn[i, j-1]) / h)
end
end
for j in 1:N # I converted this into a loop to fuse it all together
T[j, 1] = Th
T[j, N] = Tc
T[1, j] = T[2, j]
T[N, j] = T[N-1, j]
end
end
end
using BenchmarkTools # for benchmarking
@benchmark energy($Time, $N, $T, $Th, $Fo, $dt, $Tc, $h)
BenchmarkTools.Trial: 84 samples with 1 evaluation.
Range (min … max): 59.009 ms … 62.235 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 59.959 ms ┊ GC (median): 0.00%
Time (mean ± σ): 59.995 ms ± 469.045 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
▁ ▁ ▁ ▆ ▁▁▃▃ ▆ ▁▃▁█ ▁
▄▄▁▁▁▁▁▄▁▁▄▇█▁█▇▄█▇█▄▇████▄█▇████▄▁█▇▄▄▄▇▁▁▄▁▁▇▁▁▁▁▁▁▁▄▄▁▁▁▄ ▁
59 ms Histogram: frequency by time 61.2 ms <
Memory estimate: 0 bytes, allocs estimate: 0
There are some other performance optimisations you could make but this is the most obvious one.