Matlab finite difference schemes

Click For Summary
SUMMARY

This discussion focuses on implementing finite difference schemes (DS) in Matlab, specifically for calculating derivatives such as u_x, u_y, u_xx, and higher-order derivatives like u_xxx and u_yyy. The user successfully implemented first and second-order derivatives using the formula u_x=(u( :,[2:n,n])-u( :,[1,1:n-1]))/2. For higher-order derivatives, the user seeks assistance in vectorizing data points and utilizing a coefficients matrix, particularly a sparse band matrix, to facilitate calculations. The discussion emphasizes the importance of careful handling of boundary conditions when constructing the matrix.

PREREQUISITES
  • Proficiency in Matlab programming
  • Understanding of finite difference methods
  • Familiarity with matrix operations, particularly sparse matrices
  • Knowledge of numerical differentiation techniques
NEXT STEPS
  • Research Matlab's sparse matrix functions and their applications
  • Explore advanced finite difference methods for higher-order derivatives
  • Learn about vectorization techniques in Matlab for performance optimization
  • Investigate boundary condition handling in numerical methods
USEFUL FOR

Matlab users, numerical analysts, and engineers working on computational simulations that require efficient implementation of finite difference schemes for derivative calculations.

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 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K