How to Construct a Matrix for Diffusion PDE in MATLAB?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
henxan
Messages
46
Reaction score
2
I am going to do a numerical simulation of diffusion in matlab. The diffusion coefficient is concentration dependent, and i use an array operation to calculate D(x), so it is known.

Based on Fick's second equation:

$$
\frac{\partial C}{\partial t} = \frac{\partial}{\partial x} D \frac{\partial C}{\partial x}
$$

I am going to use the Forward time centered scheme for a variable D:

$$
u_i^{n+1} = u_i^{n} + \frac{\Delta t}{2\Delta x^2}\left( \left( D_{i+1}^n + D_{i}^n \right) \left(u_{i+1}^n -u_i^n \right) - \left( D_{i}^n + D_{i-1}^n \right) \left(u_{i}^n -u_{i-1}^n \right)\right)
$$

How can i construct a matrix which I can put into matlab? I can do the following with the rightmost expression:

$$
\left( \left( D_{i+1}^n + D_{i}^n \right) \left(u_{i+1}^n -u_i^n \right) - \left( D_{i}^n + D_{i-1}^n \right) \left(u_{i}^n -u_{i-1}^n \right)\right) = \begin{pmatrix} D_{i-1}^{n} & D_{i}^{n} & D_{i+1}^{n} \end{pmatrix} \begin{pmatrix} 1 & -1 & 0\\ 1 & -2 & 1\\ 0 & -1 & 1 \end{pmatrix} \begin{pmatrix} u_{i-1}^{n} \\ u_{i}^{n} \\ u_{i+1}^{n} \end{pmatrix}
$$

But how can I apply this to the entire row n+1 and include boundary conditions?
 
Last edited:
Physics news on Phys.org
What I maybe should have written is, "How do I expand the following matrix expressions to calculate:
[tex] \Delta u_i^{n+1} = \begin{pmatrix} D_{i-1}^{n} & D_{i}^{n} & D_{i+1}^{n} \end{pmatrix} \begin{pmatrix} 1 & -1 & 0\\ 1 & -2 & 1\\ 0 & -1 & 1 \end{pmatrix} \begin{pmatrix} u_{i-1}^{n} \\ u_{i}^{n} \\ u_{i+1}^{n} \end{pmatrix}[/tex]
for all [itex]i \in [2, .. N-1][/itex] where ##N## is the size of the array ##u \& D##"