# sandbox/easystab/diffmat_dif1D.m

This code is just like diffmat.m but instead of building explicitely the differentiation matrices, we use the function dif1D.m. This is the way we do in most of the codes, since producing the differentiation matrices is one of the fondamental elements of Easystab.

clear all; clf

% parameters
L=2*pi; % domain length
N=15; % number of points


# dif1D.m

The output arguments are:

• D: the first derivative differentiation matrix
• DD: the second derivative differentiation matrix
• wx: the integration weights (they are used to integrate a function on the grid, please see integration_2D.m to learn about how to build and understand these weights).
• x: the location of grid cells (This location is increasing with cell number, that is, x(i+1)>x(i)).

The intput arguments are:

• ‘fd’: the method of interpolant. Here ‘fd’ means finite differences. The other possible choices are: ‘fp’ for periodic finite differences (see periodicals%20boudaries.m for explanations), ‘cheb’ for Chebychev polynomial, ‘fou’ for Fourier periodic.
• 0: the start of the grid.
• L: the end of the grid.
• N: the number of grid cells.
• 3: the number of element in the finite difference stencil. Here, 3 means centered stencils that use the value on the right and the value on the left of the point at which we want to compute the derivative. If you chose 5, you will use two points at the left and two points at the right (this means an approximation of higher order). For other choices than finite difference, this number is not used, since spectral differentiation matrices are dense (there are no zeros…)

To get a better idea of how this is done, please see dif1D.m.

% building the differentiation matrices
[D,DD,wx,x]=dif1D('fd',0,L,N,3);


# Validation of the computation

% test of the derivatives
f=cos(x);
fp=-sin(x)';

plot(x,cos(x),'b.-',x,-sin(x),'r.-',x,D*cos(x),'r.--',x,-cos(x),'m.-',x,DD*cos(x),'m.--');
legend('cos','-sin','D*cos(x)','-cos','DD*cos')
print('-dsvg','diffmat.svg'); % save the figure


And here is the figure that is produced by the code: