Function Repository Resource:

# FiniteDifferenceStencil

Compute the finite difference stencil for a specified set of variables and their derivatives

Contributed by: Paritosh Mokhasi
 ResourceFunction["FiniteDifferenceStencil"][deriv,vars,depvar,indepvar] returns the finite difference stencil associated with the derivative expression deriv in terms of the specified variables vars and the order of the finite difference approximation.

## Details

A finite difference stencil refers to a formula that can be used to approximate derivatives at a given position using function values (and its derivatives) sampled at finite intervals around the point of interest.
The finite difference stencil is obtained using the method of undetermined coefficients.
The first argument must be a derivative specified in terms of the dependent variable depvar and independent variable indepvar.
The second argument must be a list of variables and their derivatives specified using depvar and indepvar.
The third and fourth argument must be a symbol for the dependent and independent variables respectively.
The function returns an Association with the following properties:
 "Stencil" finite difference stencil "Coefficients" coefficients of the stencil "Order" order of the approximation

## Examples

### Basic Examples (4)

Find the finite difference stencil for the first derivative spanning three points:

 In[1]:=
 Out[1]=

Find the finite difference stencil for a second derivative spanning three points:

 In[2]:=
 Out[2]=

Find the finite difference stencil for a second derivative spanning three one-sided points:

 In[3]:=
 Out[3]=

Find the finite difference stencil for a third derivative spanning five points:

 In[4]:=
 Out[4]=

### Scope (4)

Use functions and their derivatives to obtain high order finite difference approximation with same stencil size:

 In[5]:=
 Out[5]=

Use the order of the finite difference stencil to obtain optimal stencil positions:

 In[6]:=
 Out[6]=

Solve for the arbitrary parameter to obtain the optimal stencil position for the first derivative:

 In[7]:=
 Out[7]=

The order of the finite difference approximation can be increased by choosing the appropriate parameters:

 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=

Derivative values can be computed from function values using finite difference approximation. Generate the data:

 In[10]:=

Use a three point finite difference coefficients to get a second order approximation of the first derivative:

 In[11]:=
 Out[12]=

Compare the results with the reference values:

 In[13]:=
 Out[13]=

The error can be reduced by using more data points:

 In[14]:=
 Out[15]=

Plot the error associated with the better approximation:

 In[16]:=
 Out[16]=

Generate finite difference stencils for complex grid points:

 In[17]:=
 Out[18]=

The complex stencils are able to generate high accuracy approximations. Approximate the third derivative of a function at a specific point:

 In[19]:=
 Out[20]=

Approximate the third derivative at x=0.1 for various spacing h:

 In[21]:=
 Out[21]=

### Applications (8)

#### Finite Difference Matrices: (3)

Convert the finite difference stencils into differentiation matrices:

 In[22]:=
 Out[22]=
 In[23]:=
 Out[24]=

Use one sided differences to get second order approximation at the boundaries:

 In[25]:=
 Out[26]=
 In[27]:=
 Out[28]=

Approximate the second derivative using function values:

 In[29]:=
 Out[30]=

#### Boundary Value Problems: (3)

Solve the boundary value problem , using finite difference approximations:

 In[31]:=
 Out[33]=

Generate the finite difference matrix for the second derivative:

 In[34]:=
 Out[36]=

Modify the first and last row of the matrix to account for the boundary conditions:

 In[37]:=
 Out[37]=

Solve the discretized system:

 In[38]:=
 Out[40]=

Plot the result:

 In[41]:=
 Out[41]=

Plot the error:

 In[42]:=
 Out[42]=

Solve the boundary value problem , using finite difference approximations on a non-uniform grid:

 In[43]:=
 Out[45]=

Generate the finite difference matrix for the second derivative:

 In[46]:=
 Out[48]=

Modify the first and last row of the matrix to account for the boundary conditions:

 In[49]:=
 Out[49]=

Solve the discretized system:

 In[50]:=
 Out[52]=

Plot the result:

 In[53]:=
 Out[53]=

Plot the error:

 In[54]:=
 Out[54]=

Solve the boundary value problem , using higher order compact finite difference approximations on a non-uniform grid:

 In[55]:=
 Out[57]=

The finite difference stencil will consist of the functions and the second derivatives spanning three points:

 In[58]:=
 Out[59]=

The discretized higher order system will take the form Generate the matrix A:

 In[60]:=
 Out[61]=

Generate the matrix B:

 In[62]:=
 Out[63]=

Modify the first and last row of the matrices A, B to account for the boundary conditions:

 In[64]:=

Solve the discretized system which is equal to :

 In[65]:=
 Out[67]=

Plot the result:

 In[68]:=
 Out[68]=

The error is significantly better for the compact scheme:

 In[69]:=
 Out[69]=

#### Ordinary Differential Equations: (2)

Use finite difference approximations to generate a variable step time integrator within NDSolve for solving ordinary differential equations. Use 4th and 5th order approximations of the function. The approximations are implicit because they use the first and second derivatives of the function at time t:

 In[70]:=
 Out[70]=
 In[71]:=
 Out[71]=

Set up the initialization of the method:

 In[72]:=

Generate the step function. The next time step is computed based on the error between the fourth and fifth order approximation and a PI controller. Picard iterations are used to solve the implicit system:

 In[73]:=

Solve the Lorenz system using the new variable step solver:

 In[74]:=
 Out[74]=

Plot the result:

 In[75]:=
 Out[75]=

Plot the time-steps taken:

 In[76]:=
 Out[76]=

Use the variable step integrator to solve an ODE whose exact solution is known:

 In[77]:=
 Out[78]=

Solve the ODE:

 In[79]:=
 Out[80]=

The error between the exact and approximated solution should be within the default tolerance of 10-8:

 In[81]:=
 Out[81]=

Plot the time steps used:

 In[82]:=
 Out[82]=

### Possible Issues (2)

A finite difference stencil may not be obtained if sufficient stencils are not provided:

 In[83]:=
 Out[84]=

For certain stencil positions, a finite difference formula cannot be obtained:

 In[85]:=
 Out[85]=

Paritosh Mokhasi

## Version History

• 1.0.1 – 19 January 2022