# Homework Help: Setting up diffusion PDE in matlab

Tags:
1. Oct 5, 2014

### henxan

I am going to do a numerical simulation of diffusion in matlab. The diffusion coefficient is concentration dependant, 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: Oct 5, 2014
2. Oct 5, 2014

### henxan

What I maybe should have written is, "How do I expand the following matrix expressions to calculate:
$$\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}$$
for all $i \in [2, .. N-1]$ where $N$ is the size of the array $u \& D$"