MATLAB Matlab finite difference schemes

AI Thread Summary
The discussion centers on implementing finite difference schemes in MATLAB, specifically for calculating higher-order derivatives such as u_xxx, u_yyy, and others. The user successfully implemented first and second-order derivatives but struggles with third and higher orders. A suggested approach involves vectorizing the data points and using a coefficients matrix, typically a sparse band matrix, to perform the calculations efficiently. The block diagonal matrix is highlighted, where each block corresponds to identical band matrices that facilitate the computation of derivatives. Special attention is advised for endpoint handling, as the user does not evaluate certain derivatives at the boundaries, which simplifies the matrix structure. The discussion emphasizes the scalability of this method for higher dimensions, noting that while the matrix size increases, it remains sparse.
micheal9000
Messages
2
Reaction score
0
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:
Physics news on Phys.org
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:

Similar threads

Replies
5
Views
2K
Replies
3
Views
4K
Replies
4
Views
3K
Replies
11
Views
4K
Replies
2
Views
1K
Replies
1
Views
4K
Back
Top