Numerical Solutions of Heat Equations: Finite Difference Method
📂Numerical Analysis Numerical Solutions of Heat Equations: Finite Difference Method Numerical Solution of Heat Equation Let’s assume we are given a one-dimensional heat equation as follows.
∂ u ∂ t = ∂ 2 u ∂ x 2 , 0 ≤ x ≤ 1 , t ≥ 0 (1)
\dfrac{\partial u}{\partial t} = \dfrac{\partial^{2} u}{\partial x^{2}},\qquad 0\le x \le 1,\quad t \ge 0 \tag{1}
∂ t ∂ u = ∂ x 2 ∂ 2 u , 0 ≤ x ≤ 1 , t ≥ 0 ( 1 )
Our goal is to approximate the solution using finite points. Let’s divide the space-time domain as follows.
{ ( ℓ Δ x , n Δ t ) : ℓ = 0 , 1 , … , d + 1 , n ≥ 0 } where Δ x = 1 d + 1
\left\{ (\ell \Delta x, n\Delta t) : \ell=0,1,\dots,d+1,\ n\ge 0 \right\}\quad \text{ where } \Delta x = \frac{1}{d+1}
{ ( ℓ Δ x , n Δ t ) : ℓ = 0 , 1 , … , d + 1 , n ≥ 0 } where Δ x = d + 1 1
Let’s denote the approximation to u ( ℓ Δ x , n Δ t ) u(\ell \Delta x, n\Delta t) u ( ℓ Δ x , n Δ t ) by u ℓ n u_{\ell}^{n} u ℓ n . Pay attention that the superscript in n {}^{n} n is not a power but an index of time. The RHS and LHS of ( 1 ) (1) ( 1 ) are approximated as follows respectively.
∂ 2 u ( x , t ) ∂ x 2 = 1 ( Δ x ) 2 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 2 )
\dfrac{\partial^{2} u(x,t)}{\partial x^{2}} = \dfrac{1}{(\Delta x)^{2}} \big[ u(x-\Delta x, t) - 2u(x,t) + u(x+\Delta x, t) \big] + \mathcal{O}\left( (\Delta x)^{2} \right)
∂ x 2 ∂ 2 u ( x , t ) = ( Δ x ) 2 1 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 2 )
∂ u ( x , t ) ∂ t = 1 Δ t [ u ( x , t + Δ t ) − u ( x , t ) ] + O ( Δ t )
\dfrac{\partial u(x,t)}{\partial t} = \dfrac{1}{\Delta t} \big[ u(x,t+\Delta t) - u(x,t) \big] + \mathcal{O}(\Delta t)
∂ t ∂ u ( x , t ) = Δ t 1 [ u ( x , t + Δ t ) − u ( x , t ) ] + O ( Δ t )
Now, let’s set μ = Δ t ( Δ x ) 2 \mu = \dfrac{\Delta t}{(\Delta x)^{2}} μ = ( Δ x ) 2 Δ t . Substituting the two equations into ( 1 ) (1) ( 1 ) and then multiplying by Δ t = μ ( Δ x ) 2 \Delta t = \mu (\Delta x)^{2} Δ t = μ ( Δ x ) 2 , we obtain the following.
u ( x , t + Δ t ) − u ( x , t ) Δ t = 1 ( Δ x ) 2 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 2 )
\dfrac{u(x,t+\Delta t) - u(x,t)}{\Delta t} = \dfrac{1}{(\Delta x)^{2}} \big[ u(x-\Delta x, t) - 2u(x,t) + u(x+\Delta x, t) \big] + \mathcal{O}\left( (\Delta x)^{2} \right)
Δ t u ( x , t + Δ t ) − u ( x , t ) = ( Δ x ) 2 1 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 2 )
⟹ u ( x , t + Δ t ) = u ( x , t ) + μ ( Δ x ) 2 ( Δ x ) 2 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 4 )
\implies u(x,t+\Delta t) = u(x,t) + \dfrac{\mu (\Delta x)^{2}}{(\Delta x)^{2}} \big[ u(x-\Delta x, t) - 2u(x,t) + u(x+\Delta x, t) \big] + \mathcal{O}\left( (\Delta x)^{4} \right)
⟹ u ( x , t + Δ t ) = u ( x , t ) + ( Δ x ) 2 μ ( Δ x ) 2 [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 4 )
⟹ u ( x , t + Δ t ) = u ( x , t ) + μ [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 4 )
\implies u(x,t+\Delta t) = u(x,t) + \mu \big[ u(x-\Delta x, t) - 2u(x,t) + u(x+\Delta x, t) \big] + \mathcal{O}\left( (\Delta x)^{4} \right)
⟹ u ( x , t + Δ t ) = u ( x , t ) + μ [ u ( x − Δ x , t ) − 2 u ( x , t ) + u ( x + Δ x , t ) ] + O ( ( Δ x ) 4 )
⟹ u ℓ n + 1 = u ℓ n + μ ( u ℓ − 1 n − 2 u ℓ n + u ℓ + 1 n ) , ℓ = 1 , 2 , … , d , n = 0 , 1 , … (2)
\implies u_{\ell}^{n+1} = u_{\ell}^{n} + \mu \left( u_{\ell-1}^{n} - 2u_{\ell}^{n} + u_{\ell+1}^{n} \right),\quad \ell=1,2,\dots,d,\ n=0,1,\dots \tag{2}
⟹ u ℓ n + 1 = u ℓ n + μ ( u ℓ − 1 n − 2 u ℓ n + u ℓ + 1 n ) , ℓ = 1 , 2 , … , d , n = 0 , 1 , … ( 2 )
Here, the ratio of spatial and temporal intervals μ = Δ t ( Δ x ) 2 \mu = \dfrac{\Delta t}{(\Delta x)^{2}} μ = ( Δ x ) 2 Δ t is an important constant called the Courant number , which determines the convergence of ( 2 ) (2) ( 2 ) .
Explanation From the theorem below, ( 2 ) (2) ( 2 ) converges when μ ≤ 1 2 \mu \le \frac{1}{2} μ ≤ 2 1 . This means that there is an upper bound on the time step which can be advanced at one time depending on the spatial interval. That is, the denser the space domain is divided, the smaller the time step that can be advanced at once becomes. For simple examples, this may not be a problem, but if the length of the time domain is long, it can be a cause of long computation time. For example, when the space domain is divided into a vector of size 100, the time domain is divided into as many as 1733 pieces.
Convergence If μ ≤ 1 2 \mu \le \dfrac{1}{2} μ ≤ 2 1 , the numerical solution ( 2 ) (2) ( 2 ) converges.
Proof Let’s define t ∗ > 0 t^{\ast} \gt 0 t ∗ > 0 as an arbitrary constant. Let’s define the error between the approximated value and the actual solution at each point as follows.
e ℓ n : = u ℓ n − u ( ℓ Δ x , n Δ t ) , ℓ = 0 , 1 , … , d + 1 , n = 0 , 1 , … , n Δ t
e_{\ell}^{n} := u_{\ell}^{n} - u(\ell\Delta x, n\Delta t),\qquad \ell = 0,1,\dots,d+1,\quad n=0,1,\dots,n_{\Delta t}
e ℓ n := u ℓ n − u ( ℓ Δ x , n Δ t ) , ℓ = 0 , 1 , … , d + 1 , n = 0 , 1 , … , n Δ t
Here, n Δ t = ⌊ t ∗ / Δ t ⌋ = ⌊ t ∗ / ( μ ( Δ x ) 2 ) ⌋ n_{\Delta t} = \lfloor t^{\ast}/{\Delta t} \rfloor = \lfloor t^{\ast} / (\mu (\Delta x)^{2}) \rfloor n Δ t = ⌊ t ∗ / Δ t ⌋ = ⌊ t ∗ / ( μ ( Δ x ) 2 )⌋ is the endpoint of n n n . That the method converges can be expressed as the following equation being true for error e ℓ n e_{\ell}^{n} e ℓ n .
lim Δ x → 0 [ max n = 0 , 1 , … , n Δ t ( max ℓ = 0 , 1 , … , d + 1 ∣ e ℓ n ∣ ) ] = 0
\lim\limits_{\Delta x \to 0} \left[ \max\limits_{n=0,1,\dots,n_{\Delta t}} \left( \max\limits_{\ell=0,1,\dots,d+1} \left| e_{\ell}^{n} \right| \right) \right] = 0
Δ x → 0 lim [ n = 0 , 1 , … , n Δ t max ( ℓ = 0 , 1 , … , d + 1 max ∣ e ℓ n ∣ ) ] = 0
Let’s set the value inside the parentheses as follows.
η n : = max ℓ = 0 , 1 , … , d + 1 ∣ e ℓ n ∣ , n = 0 , 1 , … , n Δ t (3)
\eta^{n} := \max\limits_{\ell=0,1,\dots,d+1} \left| e_{\ell}^{n} \right|,\quad n=0,1,\dots,n_{\Delta t} \tag{3}
η n := ℓ = 0 , 1 , … , d + 1 max ∣ e ℓ n ∣ , n = 0 , 1 , … , n Δ t ( 3 )
Then the expression for convergence is,
lim Δ x → 0 ( max n = 0 , 1 , … , n Δ t η n ) = 0 (4)
\lim\limits_{\Delta x \to 0} \left( \max\limits_{n=0,1,\dots,n_{\Delta t}} \eta^{n} \right) = 0 \tag{4}
Δ x → 0 lim ( n = 0 , 1 , … , n Δ t max η n ) = 0 ( 4 )
Now let’s assume u ~ ℓ n = u ( ℓ Δ x , n Δ t ) \tilde{u}_{\ell}^{n} = u(\ell\Delta x, n\Delta t) u ~ ℓ n = u ( ℓ Δ x , n Δ t ) is the true value of u ℓ n u_{\ell}^{n} u ℓ n .
u ℓ n + 1 = u ℓ n + μ ( u ℓ − 1 n − 2 u ℓ n + u ℓ + 1 n ) u ~ ℓ n + 1 = u ~ ℓ n + μ ( u ~ ℓ − 1 n − 2 u ~ ℓ n + u ~ ℓ + 1 n ) + O ( ( Δ x ) 4 ) ℓ = 0 , 1 , … , d + 1 n = 0 , 1 , … , n Δ t
\begin{align*}
u_{\ell}^{n+1} &= u_{\ell}^{n} + \mu \left( u_{\ell-1}^{n} - 2u_{\ell}^{n} + u_{\ell+1}^{n} \right) \\
\tilde{u}_{\ell}^{n+1} &= \tilde{u}_{\ell}^{n} + \mu \left( \tilde{u}_{\ell-1}^{n} - 2\tilde{u}_{\ell}^{n} + \tilde{u}_{\ell+1}^{n} \right) + \mathcal{O}\left( (\Delta x)^{4} \right)
\end{align*}
\qquad \begin{array}{l}
\ell = 0,1,\dots,d+1 \\
n=0,1,\dots,n_{\Delta t}
\end{array}
u ℓ n + 1 u ~ ℓ n + 1 = u ℓ n + μ ( u ℓ − 1 n − 2 u ℓ n + u ℓ + 1 n ) = u ~ ℓ n + μ ( u ~ ℓ − 1 n − 2 u ~ ℓ n + u ~ ℓ + 1 n ) + O ( ( Δ x ) 4 ) ℓ = 0 , 1 , … , d + 1 n = 0 , 1 , … , n Δ t
Subtracting these, we get the following.
e ℓ n + 1 = e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + O ( ( Δ x ) 4 ) ≤ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + c ( Δ x ) 4 , c > 0
\begin{align*}
e_{\ell}^{n+1}
&= e_{\ell}^{n} + \mu \left( e_{\ell-1}^{n} - 2e_{\ell}^{n} + e_{\ell+1}^{n} \right) + \mathcal{O}\left( (\Delta x)^{4} \right) \\[1em]
&\le e_{\ell}^{n} + \mu \left( e_{\ell-1}^{n} - 2e_{\ell}^{n} + e_{\ell+1}^{n} \right) + c (\Delta x)^{4},\qquad c \gt 0
\end{align*}
e ℓ n + 1 = e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + O ( ( Δ x ) 4 ) ≤ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + c ( Δ x ) 4 , c > 0
The inequality holds by the definition of asymptotic notation . By the triangle inequality , we obtain the following.
∣ e ℓ n + 1 ∣ ≤ ∣ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + c ( Δ x ) 4 ∣ ≤ ∣ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) ∣ + c ( Δ x ) 4 ≤ μ ∣ e ℓ − 1 n ∣ + ∣ 1 − 2 μ ∣ ∣ e ℓ n ∣ + μ ∣ e ℓ + 1 n ∣ + c ( Δ x ) 4 ≤ ( 2 μ + ∣ 1 − 2 μ ∣ ) η n + c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t − 1
\begin{align*}
\left| e_{\ell}^{n+1} \right|
&\le \left| e_{\ell}^{n} + \mu \left( e_{\ell-1}^{n} - 2e_{\ell}^{n} + e_{\ell+1}^{n} \right) + c (\Delta x)^{4} \right| \\
&\le \left| e_{\ell}^{n} + \mu \left( e_{\ell-1}^{n} - 2e_{\ell}^{n} + e_{\ell+1}^{n} \right) \right| + c (\Delta x)^{4} \\
&\le \mu \left| e_{\ell-1}^{n} \right| + \left| 1 - 2\mu \right| \left| e_{\ell}^{n} \right| + \mu \left| e_{\ell+1}^{n} \right| + c (\Delta x)^{4} \\
&\le (2\mu + \left| 1 - 2\mu \right|)\eta^{n} + c (\Delta x)^{4},\qquad n=0,1,\dots,n_{\Delta t}-1
\end{align*}
e ℓ n + 1 ≤ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + c ( Δ x ) 4 ≤ e ℓ n + μ ( e ℓ − 1 n − 2 e ℓ n + e ℓ + 1 n ) + c ( Δ x ) 4 ≤ μ e ℓ − 1 n + ∣ 1 − 2 μ ∣ ∣ e ℓ n ∣ + μ e ℓ + 1 n + c ( Δ x ) 4 ≤ ( 2 μ + ∣ 1 − 2 μ ∣ ) η n + c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t − 1
The last inequality holds due to ( 3 ) (3) ( 3 ) . Since μ ≤ 1 / 2 \mu \le 1/2 μ ≤ 1/2 , ( 2 μ + ∣ 1 − 2 μ ∣ ) = 1 (2\mu + \left| 1 - 2\mu \right|)=1 ( 2 μ + ∣ 1 − 2 μ ∣ ) = 1 , and again from ( 3 ) (3) ( 3 ) we obtain the following.
η n + 1 = max ℓ = 0 , 1 , … , d + 1 ∣ e ℓ n + 1 ∣ ≤ η n + c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t − 1
\eta^{n+1} = \max\limits_{\ell=0,1,\dots,d+1} \left| e_{\ell}^{n+1} \right| \le \eta^{n} + c (\Delta x)^{4},\qquad n=0,1,\dots,n_{\Delta t}-1
η n + 1 = ℓ = 0 , 1 , … , d + 1 max e ℓ n + 1 ≤ η n + c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t − 1
Repeatedly applying this inductively,
η n + 1 ≤ η n + c ( Δ x ) 4 ≤ η n − 1 + 2 c ( Δ x ) 4 ≤ η n − 2 + 3 c ( Δ x ) 4 ≤ ⋯
\eta^{n+1} \le \eta^{n} + c (\Delta x)^{4} \le \eta^{n-1} + 2c (\Delta x)^{4} \le \eta^{n-2} + 3 c (\Delta x)^{4} \le \cdots
η n + 1 ≤ η n + c ( Δ x ) 4 ≤ η n − 1 + 2 c ( Δ x ) 4 ≤ η n − 2 + 3 c ( Δ x ) 4 ≤ ⋯
⟹ η n ≤ η 0 + n c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t
\implies \eta^{n} \le \eta^{0} + n c (\Delta x)^{4},\qquad n=0,1,\dots,n_{\Delta t}
⟹ η n ≤ η 0 + n c ( Δ x ) 4 , n = 0 , 1 , … , n Δ t
Since the initial value is given as η 0 = 0 \eta^{0} = 0 η 0 = 0 , and since n ( Δ x ) 2 = n Δ t / μ = t ∗ / μ n(\Delta x)^{2} = n\Delta t / \mu = t^{\ast} / \mu n ( Δ x ) 2 = n Δ t / μ = t ∗ / μ ,
η n ≤ c t ∗ μ ( Δ x ) 2 , n = 0 , 1 , … , n Δ t
\eta^{n} \le \dfrac{ct^{\ast}}{\mu}(\Delta x)^{2},\qquad n=0,1,\dots,n_{\Delta t}
η n ≤ μ c t ∗ ( Δ x ) 2 , n = 0 , 1 , … , n Δ t
Therefore, for all n n n ,
η n → 0 as Δ x → 0
\eta^{n} \to 0\quad \text{ as } \Delta x \to 0
η n → 0 as Δ x → 0
Combining this with ( 4 ) (4) ( 4 ) completes the proof.
■
Example This can be implemented in Julia as follows.
1 Dimension Now let’s consider the following one-dimensional heat equation.
Domain: Ω = [ − 1 , 1 ] × [ 0 , 0.35 ] Heat equation: u t − u x x = 0 u ( x , 0 ) = sin ( π x ) u ( − 1 , t ) = 0 , u ( 1 , t ) = 0 Exact solution: u ( x , t ) = sin ( π x ) e − π 2 t
\begin{array}{lc}
\text{Domain:} & \\
& \Omega = [-1,1]\times [0,0.35] \\
\text{Heat equation:} & \\
& u_{t} - u_{xx} = 0 \\[0.5em]
& u(x,0) = \sin (\pi x) \\[0.5em]
& u(-1, t) = 0,\quad u(1, t) = 0 \\[0.5em]
\text{Exact solution:}& \\
& u(x,t) = \sin(\pi x) e^{-\pi^2 t}
\end{array}
Domain: Heat equation: Exact solution: Ω = [ − 1 , 1 ] × [ 0 , 0.35 ] u t − u xx = 0 u ( x , 0 ) = sin ( π x ) u ( − 1 , t ) = 0 , u ( 1 , t ) = 0 u ( x , t ) = sin ( π x ) e − π 2 t
Here, if we set Δ x = 1 − ( − 1 ) 99 \Delta x = \dfrac{1-(-1)}{99} Δ x = 99 1 − ( − 1 ) and Δ t = μ ( Δ ) 2 = 0.5 ( Δ x ) 2 \Delta t = \mu (\Delta)^{2} = 0.5(\Delta x)^{2} Δ t = μ ( Δ ) 2 = 0.5 ( Δ x ) 2 and solve it with ( 2 ) (2) ( 2 ) , we get the following result.
On the other hand, if we set it to Δ t = μ ( Δ ) 2 = 0.51 ( Δ x ) 2 \Delta t = \mu (\Delta)^{2} = 0.51(\Delta x)^{2} Δ t = μ ( Δ ) 2 = 0.51 ( Δ x ) 2 and solve it, we can see that the solution is not obtained properly.
using Plots
using LinearAlgebra
function Solver_Heat_1d(Δt, T, x, f, g)
t = Vector (0 :Δt:T)
n = length(t)
ℓ = length(x)
Δx = x[2 ]-x[1 ]
μ = Δt/((Δx)^2 )
μ = Δt/((Δx)^2 )
U = zeros(ℓ,n)
U[1 ,:] .= g[1 ]
U[end ,:] .= g[end ]
U[:,1 ] = f
for i ∈ 2 :n
U[2 :end -1 , i] = (1 -2 μ)*U[2 :end -1 , i-1 ] + μ*(U[1 :end -2 , i-1 ] + U[3 :end , i-1 ])
end
return U
end
function ref_sol(x, t, exact_sol)
xt =[kron(ones(length(t)),x);; kron(t,ones(length(x)))]
ref_U = reshape(exact_sol(xt),length(x),length(t))
return ref_U
end
function results(t, x, U, ref_U, μ)
h1 = heatmap(t, x, U, colormap=:viridis, clim=(-1 ,1 ))
title!("FDM solution" )
h2 = heatmap(t, x, ref_U, colormap=:viridis, clim=(-1 ,1 ))
title!("exact solution" )
h3 = heatmap(t, x, U - ref_U, colormap=:viridis)
title!("error" )
h4 = heatmap(framestyle=:none)
annotate!([0.5 ],[0.5 ], text("μ = $(round(μ, digits=3 ) )" , 30 ))
plot(h1, h2, h3, h4, size=(1500 ,750 ))
end
for μ ∈ [0.5 , 0.51 ]
x = Vector (LinRange (-1. , 1 , 100 ))
Δx = x[2 ]-x[1 ]
initial_f = sin.(π *x)
Δt = μ*((Δx)^2 )
T = 0.35
bdry_g = [0.0 , 0.0 ]
t = Vector (0 :Δt:T)
exact_sol(xt) = sin.(π *xt[:,1 ]) .* exp.(- (π )^2 * xt[:,2 ])
ref_u = ref_sol(x, t, exact_sol)
U = Solver_Heat_1d(Δt, T, x, initial_f, bdry_g)
results(t, x, U, ref_u, μ)
savefig("mu=" * string(μ) * ".png" )
end
2 Dimension Domain: Ω = [ − 2 , 2 ] × [ − 2 , 2 ] × [ 0 , 1 ] Heat equation: u t − u x x = 0 u ( x , y , 0 ) = 1 4 e − x 2 − y 2 u ( x , y , t ) = 0 on ∂ ( [ − 2 , 2 ] × [ − 2 , 2 ] )
\begin{array}{lc}
\text{Domain:} & \\
& \Omega = [-2,2] \times [-2,2] \times [0,1] \\
\text{Heat equation:} & \\
& u_{t} - u_{xx} = 0 \\[0.5em]
& u(x,y,0) = \frac{1}{4} e^{-x^{2} -y^{2}} \\[0.5em]
& u(x, y, t) = 0\qquad \text{ on } \partial \left( [-2,2] \times [-2,2] \right)
\end{array}
Domain: Heat equation: Ω = [ − 2 , 2 ] × [ − 2 , 2 ] × [ 0 , 1 ] u t − u xx = 0 u ( x , y , 0 ) = 4 1 e − x 2 − y 2 u ( x , y , t ) = 0 on ∂ ( [ − 2 , 2 ] × [ − 2 , 2 ] )
Using the same method to solve the above differential equations,
using Plots
cd = @__DIR__
function Solver_Heat_2d(Δt, T, x, f)
t = Vector (0 :Δt:T)
n = length(t)
ℓ = length(x)
Δx = x[2 ]-x[1 ]
μ = Δt/((Δx)^2 )
U = zeros(ℓ,ℓ,n)
U[:,:,1 ] = f
for i ∈ 2 :n
U[2 :end -1 , 2 :end -1 , i] = (1 -4 μ)*U[2 :end -1 , 2 :end -1 , i-1 ] + μ*(U[1 :end -2 , 2 :end -1 , i-1 ] + U[3 :end , 2 :end -1 , i-1 ] + U[2 :end -1 , 1 :end -2 , i-1 ] + U[2 :end -1 , 3 :end , i-1 ])
end
return U
end
x = range(-2. , stop=2 , length=100 )'
y = range(-2. , stop=2 , length=100 )
initial_f = @. (1 /4 )exp(-x^2 ) * exp(-y^2 )
Δx = x[2 ]-x[1 ]
Δt = 0.49 *((Δx)^2 )/2
T = 1.0
U = Solver_Heat_2d(Δt, T, x, initial_f)
anim = @animate for i ∈ range(1 , stop=size(U)[3 ], step=100 )
surface(U[:,:,i], zlims=(0 ,0.5 ), camera=(30 ,30 ), colorbar=:none, showaxis = false , grid=false , size=(500 ,500 ))
end
gif(anim, cd*"/2d.gif" , fps=10 )
Environment OS: Windows11 Version: Julia v1.8.3, Plots v1.38.6