Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Matlab finite difference schemes

  1. Jan 20, 2015 #1
    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. jcsd
  3. Jan 20, 2015 #2
    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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook