comparison +grid/evalOnScalar.m @ 273:f688e2bdb74a feature/beams

grid: Added evalOn for scalar functions.
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 06 Sep 2016 15:51:41 +0200
parents
children 3ea2ae2a3d15
comparison
equal deleted inserted replaced
272:9fe423849525 273:f688e2bdb74a
1 % Takes a funciton and evaluates it on a grid to return a grid function in the
2 % form of a (n*k)x1 vector, where n is the number of grid points and k is the
3 % number of components of the function.
4 % g -- Grid to evaluate on.
5 % func -- Function to evaluate. May be a function handle or a constant. If
6 % it is a vector value it has to be provided as a column vector,
7 function gf = evalOn(g, func)
8 if ~isa(func, 'function_handle')
9 % We should have a constant.
10 if size(func,2) ~= 1
11 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
12 end
13
14 gf = repmat(func,[g.N, 1]);
15 return
16 end
17 % func should now be a function_handle
18
19 % Get coordinates and convert to cell array for easier use as a parameter
20 x = g.points();
21 X = {};
22 for i = 1:size(x, 2)
23 X{i} = x(:,i);
24 end
25
26 % Find the number of components
27 x0 = num2cell(x(1,:));
28 f0 = func(x0{:});
29 k = length(f0);
30
31 if size(f0,2) ~= 1
32 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
33 end
34
35 gf = func(X{:});
36 if k > 1 % Reorder so that componets sits together.
37 gf = reshape(reshape(gf, [g.N, k])', [g.N*k, 1]);
38 end
39 end