# 2d finite differences implementation for a Numerical Analysis lecture

**URL:** https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222
**Category:** Performance
**Tags:** numerics, finitediff
**Created:** [October 22, 2021, 3:32pm UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222 "2021-10-22T15:32:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vicdolean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicdolean/32/30134_2.png) [@vicdolean](https://discourse.julialang.org/u/vicdolean)
#### Post date: [October 22, 2021, 3:32pm UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222/1 "2021-10-22T15:32:21Z")

</div>

Hi! I’m new to Julia world and would need some help in the transition from Matlab.  
I am teaching a numerical analysis class where I use finite differences on a two dimensional (cartesian but not necessarily square) domain. The Matlab function **delsq** was very useful as I could then generate the grid by meshgrid, build the FD matrix from it, change boundary conditions to Neumann or Robin to a part of the boundary and also localise the source term, diffusion coeff etc…  
I all want are simple teaching codes where I can illustrate all the basic concepts on finite differences in a genuinely 2d implementation (no kronecker of 1d!) - and not black box like in [[https://github.com/SciML/DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl)]  
Has someone already tried that?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 23, 2021, 7:58am UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222/2 "2021-10-23T07:58:15Z")

</div>

Maybe the codes in [https://github.com/luraess/parallel-gpu-workshop-JuliaCon21/tree/main/scripts](https://github.com/luraess/parallel-gpu-workshop-JuliaCon21/tree/main/scripts) can be of use, e.g. here diffusion with a forward euler step [https://github.com/luraess/parallel-gpu-workshop-JuliaCon21/blob/main/scripts/diffusion\_2D\_expl.jl](https://github.com/luraess/parallel-gpu-workshop-JuliaCon21/blob/main/scripts/diffusion_2D_expl.jl).

---

<div class="post-metadata">

### Author: ![vicdolean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vicdolean/32/30134_2.png) [@vicdolean](https://discourse.julialang.org/u/vicdolean)
#### Post date: [November 20, 2021, 8:43am UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222/3 "2021-11-20T08:43:03Z")

</div>

Thanks a lot! This is very useful.

---

<div class="post-metadata">

### Author: ![pxshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pxshen/32/38776_2.png) [@pxshen](https://discourse.julialang.org/u/pxshen)
#### Post date: [June 22, 2022, 11:17pm UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222/4 "2022-06-22T23:17:38Z")

</div>

Check out this `EquivariantOperators.jl` [tutorial](https://colab.research.google.com/drive/17JZEdK6aALxvn0JPBJEHGeK2nO1hPnhQ#scrollTo=2xW0gUV_DFXT&line=1&uniqifier=1) and [package website](https://aced-differentiate.github.io/EquivariantOperators.jl/)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 23, 2022, 12:22am UTC](https://discourse.julialang.org/t/2d-finite-differences-implementation-for-a-numerical-analysis-lecture/70222/5 "2022-06-23T00:22:31Z")

</div>

> [@vicdolean](#):
>
> I all want are simple teaching codes where I can illustrate all the basic concepts on finite differences in a genuinely 2d implementation (no kronecker of 1d!)

See, for example, [this post](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556/8), which provides a simple N-dimensional implementation of a finite-difference method for a wave equation using a leapfrog explicit timestepping scheme. It would be pretty easy to change it to the diffusion equation, too.

Among other things, it illustrates computing the finite-difference Laplacian in N dimensions cleanly and efficiently using Julia’s Cartesian-indexing code, and the useful technique of “ghost cells” for boundary conditions (which allow you to separate the boundary-condition code from the finite-difference code).

(Whoops, sorry, I see that this is an old thread that was recently revived.)
