# Matlab finite difference schemes

Tags:
1. Jan 20, 2015

### micheal9000

I have big problem with finite difference schemes (DS) on Matlab. I need write DS on Matlab, example:
u_x=(u_(i+1,j)-u_(i-1,j))/2, we choose step is 1.
On Matlab:
u_x=(u( :,[2:n,n])-u( :,[1,1:n-1]))/2
And I can write u_y, u_xx, u_yy, u_xy. But now, I need to write for higher order, example, u_xxx, u_yyy, u_xxy, u_xyy, u_xxxx, u_yyyy, u_xxyy, u_xyyy, u_xxxy. From 3rd order, I cannot do it. Please help me. Many thanks.
P/s: I just need on Matlab code, I can write the schemes in mathematical form.

Last edited: Jan 20, 2015
2. Jan 20, 2015

### da_nang

If you have the difference scheme, then a simple way to do it (for problems of low dimensions) is to vectorize the data points and use a coefficients matrix to multiply said vector with. These are typically (sparse) band matrices (blockband matrices in the general n-dimensional case), so the diag function is handy. In your example, this blockband matrix is a block diagonal matrix, where each block in the main diagonal are identical band matrices that consists of diagonals of 1/2 and -1/2 above and below the main diagonal by one diagonal respectively. You just need to be careful with the end points. In your example, you choose not to evaluate u_x(i,j) at the end points, so you don't technically need the top and bottom row of the band matrix. So if you have a n x n data grid, the matrix will be of the size (n2-n) x n2.

This works for higher dimensions as well, but the matrix gets very large (but sparse) quickly.

EDIT: Fixed mistake.

Last edited: Jan 20, 2015