comparison +grid/primalDual1D.m @ 641:070d578997f6 feature/d1_staggered

Add helper function for primal and dual 1D staggered grid, and a test for that function.
author Martin Almquist <malmquist@stanford.edu>
date Sat, 11 Nov 2017 20:26:37 -0800
parents
children
comparison
equal deleted inserted replaced
640:4046b046c1f7 641:070d578997f6
1 % Creates a 1D staggered grid of dimension length(m).
2 % over the interval xlims
3 % Primal grid: equidistant with m points.
4 % Dual grid: m + 1 points, h/2 spacing first point.
5 % Examples
6 % g = grid.primal_dual_1D(m, xlim)
7 % g = grid.primal_dual_1D(11, {0,1})
8 function [g_primal, g_dual] = primalDual1D(m, xlims)
9
10 if ~iscell(xlims) || numel(xlims) ~= 2
11 error('grid:primalDual1D:InvalidLimits','The limits should be cell arrays with 2 elements.');
12 end
13
14 if xlims{1} > xlims{2}
15 error('grid:primalDual1D:InvalidLimits','The elements of the limit must be increasing.');
16 end
17
18 xl = xlims{1};
19 xr = xlims{2};
20 h = (xr-xl)/(m-1);
21
22 % Primal grid
23 g_primal = grid.equidistant(m, xlims);
24 g_primal.h = h;
25
26 % Dual grid
27 x = [xl; linspace(xl+h/2, xr-h/2, m-1)'; xr];
28 g_dual = grid.Cartesian(x);
29 g_dual.h = h;
30 end