.php xmlns="http://www.w3.org/1999/.php"> - k-Wave MATLAB Toolbox

# getFDMatrix

Create a matrix of finite-difference coefficients.

## Syntax

```fdm = getFDMatrix(Nx)
fdm = getFDMatrix(Nx, dx)
fdm = getFDMatrix(Nx, dx, deriv_order, accuracy_order)
```

## Description

`getFDMatrix` returns a matrix of finite-difference coefficients to compute the derivative of the given order with the given numerical accuracy. The coefficients in the center of the grid use centered differences, while the coefficients at the edges of the grid use forward and backward differences. The matrix is generated and returned in sparse format. To convert to a full matrix, use the syntax fdm = full(fdm).

Example: Compute the gradient of a 1D column vector

```dx = pi/20;
x = (0:dx:4*pi).';
y = sin(x);
fdm = getFDMatrix(length(x), dx);
dydx = fdm * y;
plot(x, y, 'k-', x, dydx, 'r-');
```

## Inputs

 `Nx` size of data to differentiate

## Optional Inputs

 `dx` spacing between grid points (default = 1) `deriv_order` order of the derivative to compute, e.g., use 1 to compute df/dx, 2 to compute df^2/dx^2, etc. (default = 1) `accuracy_order` order of accuracy for the finite difference coefficients. Because centered differences are used, this must be set to an integer multiple of 2 (default = 2)

## Outputs

 `fdm` matrix of finite difference coefficients

`gradientSpect`