-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchebvar.m
More file actions
72 lines (63 loc) · 1.79 KB
/
chebvar.m
File metadata and controls
72 lines (63 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function chebvar(varargin)
%CHEBVAR Short-cut for constructing CHEBFUN variables.
% CHEBVAR arg1 arg2 ...
% is short-hand notation for creating symbolic variables
% arg1 = chebfun('arg1');
% arg2 = chebfun('arg2'); ...
% The outputs are created in the current workspace.
%
% CHEBVAR arg1 arg2 ... DOM constructs the CHEBFUN objects on the domain DOM,
% i.e., arg1 = chebfun('arg1', DOM);
% arg2 = chebfun('arg2', DOM); ...
%
% In both cases, the CHEBFUN is created according to the currently stored
% default preferences.
%
% Example:
% chebvar x
% f = sin(x)
%
% See also CHEBFUN, CHEBFUNPREF.
% Copyright 2014 by The University of Oxford and The Chebfun Developers.
% See http://www.chebfun.org/ for Chebfun information.
% Trivial case:
if ( nargin == 0 )
return
end
dom = [];
% Locate valid variable names:
isVar = cellfun(@isvarname, varargin);
% The last entry may be a domain:
if ( ~all(isVar) )
dom = str2num(varargin{end}); %#ok<ST2NM>
if ( ~isempty(dom) )
varargin(end) = [];
isVar(end) = [];
end
end
% Check validity of variable name:
if ( ~all(isVar) )
error('CHEBFUN:chebvar:badName', 'Not a valid variable name.');
end
% Acquire some preferences:
pref = chebfunpref();
if ( isempty(dom) )
dom = pref.domain;
end
% Loop over each of the inputs:
for k = numel(varargin):-1:1
op = str2op(varargin{k});
f = chebfun(op, dom, pref);
assignin('caller', varargin{k}, f);
end
end
function op = str2op(op)
% This is here as it's a clean function with no other variables hanging around
% in the scope.
depVar = symvar(op);
if ( numel(depVar) ~= 1 )
error('CHEBFUN:chebvar:indepVars', ...
'Incorrect number of independent variables in string input.');
end
op = eval(['@(' depVar{:} ')', op]);
end