Cody

Solution 420633

Submitted on 20 Mar 2014 by Mehmet OZC
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% x = 'interp1'; y_correct = true; assert(isequal(SupportedFunction(x),y_correct))

function Vout = interp1(varargin) %INTERP1 1-D interpolation (table lookup) % % Some features of INTERP1 will be removed in a future release. % See the R2012a release notes for details. % % Vq = INTERP1(X,V,Xq) interpolates to find Vq, the values of the % underlying function V=F(X) at the query points Xq. X must % be a vector of length N. % If V is a vector, then it must also have length N, and Vq is the % same size as Xq. If V is an array of size [N,D1,D2,...,Dk], then % the interpolation is performed for each D1-by-D2-by-...-Dk value % in V(i,:,:,...,:). % If Xq is a vector of length M, then Vq has size [M,D1,D2,...,Dk]. % If Xq is an array of size [M1,M2,...,Mj], then Vq is of size % [M1,M2,...,Mj,D1,D2,...,Dk]. % % Vq = INTERP1(V,Xq) assumes X = 1:N, where N is LENGTH(V) % for vector V or SIZE(V,1) for array V. % % Interpolation is the same operation as "table lookup". Described in % "table lookup" terms, the "table" is [X,V] and INTERP1 "looks-up" % the elements of Xq in X, and, based upon their location, returns % values Vq interpolated within the elements of V. % % Vq = INTERP1(X,V,Xq,METHOD) specifies alternate methods. % The default is linear interpolation. Use an empty matrix [] to specify % the default. Available methods are: % % 'nearest' - nearest neighbor interpolation % 'linear' - linear interpolation % 'spline' - piecewise cubic spline interpolation (SPLINE) % 'pchip' - shape-preserving piecewise cubic interpolation % 'cubic' - same as 'pchip' % 'v5cubic' - the cubic interpolation from MATLAB 5, which does not % extrapolate and uses 'spline' if X is not equally % spaced. % % Vq = INTERP1(X,V,Xq,METHOD,'extrap') uses the interpolation algorithm % specified by METHOD to perform extrapolation for elements of Xq outside % the interval spanned by X. % % Vq = INTERP1(X,V,Xq,METHOD,EXTRAPVAL) replaces the values outside of the % interval spanned by X with EXTRAPVAL. NaN and 0 are often used for % EXTRAPVAL. The default extrapolation behavior with four input arguments % is 'extrap' for 'spline' and 'pchip' and EXTRAPVAL = NaN (NaN +NaNi for % complex values) for the other methods. % % PP = INTERP1(X,V,METHOD,'pp') will use the interpolation algorithm specified % by METHOD to generate the ppform (piecewise polynomial form) of V. The % method may be any of the above METHOD except for 'v5cubic'. PP may then % be evaluated via PPVAL. PPVAL(PP,Xq) is the same as % INTERP1(X,V,Xq,METHOD,'extrap'). % % For example, generate a coarse sine curve and interpolate over a % finer abscissa: % X = 0:10; V = sin(X); Xq = 0:.25:10; % Vq = interp1(X,V,Xq); plot(X,V,'o',Xq,Vq) % % For a multi-dimensional example, we construct a table of functional % values: % X = [1:10]'; V = [ X.^2, X.^3, X.^4 ]; % Xq = [ 1.5, 1.75; 7.5, 7.75]; Vq = interp1(X,V,Xq); % % creates 2-by-2 matrices of interpolated function values, one matrix for % each of the 3 functions. Vq will be of size 2-by-2-by-3. % % Class support for inputs X, V, Xq, EXTRAPVAL: % float: double, single % % See also INTERPFT, SPLINE, PCHIP, INTERP2, INTERP3, INTERPN, PPVAL. % Copyright 1984-2013 The MathWorks, Inc. % % Determine input arguments. % Work backwards parsing from the end argument. % Set up the defaults narginchk(2,5); [method,extrapval,ndataarg,pp] = parseinputs(varargin{:}); if ~isempty(pp) Vout = pp; return end % Set up X, V, and Xq and sanity check the data % At this point we have two possible scenarios % (X,V,Xq) or (V,Xq) and V may not be a vector % if ndataarg ~= 2 or ndataarg ~=3, error if ndataarg == 2 V = varargin{1}; if isvector(V) orig_size_v = size(V); V = V(:); % Reorient not considered a resize else orig_size_v = size(V); n = orig_size_v(1); ds = orig_size_v(2:end); prodDs = prod(ds); V = reshape(V,[n prodDs]); end Xq = varargin{2}; X =(1:size(V,1))'; elseif ndataarg == 3 X = varargin{1}; if ~isnumeric(X) error(message('MATLAB:interp1:Xnumeric')); end V = varargin{2}; if isvector(V) orig_size_v = size(V); V = V(:); % Reorient not considered a resize else orig_size_v = size(V); n = orig_size_v(1); ds = orig_size_v(2:end); prodDs = prod(ds); V = reshape(V,[n prodDs]); end X = X(:); if any(diff(X)<0) [X, idx] = sort(X); V = V(idx,:); end Xq = varargin{3}; else error(message('MATLAB:interp1:nargin')); end if ~isfloat(V) error(message('MATLAB:interp1:NonFloatValues')); end if isscalar(X) && isempty(Xq) Vout = zeros(size(Xq)); return end if isvector(V)% V is a vector so size(Vq) == size(Xq) siz_vq = size(Xq); else if isvector(Xq)% V is not a vector but Xq is. Batch evaluation. siz_vq = [length(Xq) orig_size_v(2:end)]; else% Both V and Xq are non-vectors siz_vq = [size(Xq) orig_size_v(2:end)]; end end if ~isempty(extrapval) if ~isempty(Xq) && isfloat(Xq) && isreal(Xq) % Impose the extrap val; this is independent of method extptids = Xq < X(1) | Xq > X(end); if any(extptids(:)) Xq = Xq(~extptids); else extrapval = []; end else extrapval = []; end end Xqcol = Xq(:); num_vals = size(V,2); if any(~isfinite(V(:))) || (num_vals > 1 && strcmpi(method,'pchip')) ... || (num_vals == 2 && strcmpi(method,'cubic')) F = griddedInterpolant(X,V(:,1),method); if any(strcmpi(F.Method,{'spline','pchip'})) && any(isnan(V(:))) VqLite = Interp1DStripNaN(X,V,Xq,F.Method); else VqLite = zeros(numel(Xqcol),num_vals); VqLite(:,1) = F(Xqcol); for iv = 2:num_vals F.Values = V(:,iv); VqLite(:,iv) = F(Xqcol); end end else % can use ND if (num_vals > 1) Xext = {cast(X,'double'),(1:num_vals)'}; F = griddedInterpolant(Xext,V,method); VqLite = F({cast(Xqcol,class(Xext{1})),Xext{2:end}}); else F = griddedInterpolant(X,V,method); VqLite = F(Xqcol); end end if ~isempty(extrapval) if ischar(extrapval) if ~isreal(V) extrapval = NaN + 1i*NaN; else extrapval = NaN; end end % Vq is too small since elems of Xq were removed. sizeVqLite = size(VqLite); Vq = zeros([siz_vq(1) sizeVqLite(2:end)],superiorfloat(X,V,Xq)); Vq(~extptids,:) = VqLite; Vq(extptids,:) = extrapval; % Reshape result, possibly to an ND array Vout = reshape(Vq,siz_vq); else VqLite = reshape(VqLite,siz_vq); Vout = cast(VqLite,superiorfloat(X,V,Xq)); end end % INTERP1 %-------------------------------------------------------------------------% function Vq = Interp1DStripNaN(X,V,Xq,method) Xqcol = Xq(:); num_value_sets = 1; numXq = numel(Xqcol); if ~isvector(V) num_value_sets = size(V,2); end % Allocate Vq Vq = zeros(numXq,num_value_sets); nans_stripped = false; for i = 1:num_value_sets numvbefore = numel(V(:,i)); [xi, vi] = stripnansforspline(X,V(:,i)); numvafter = numel(vi); if numvbefore > numvafter nans_stripped = true; end F = griddedInterpolant(xi,vi,method); if isempty(Xq) Vq(:,i) = Xqcol; else Vq(:,i) = F(Xqcol); end end if nans_stripped warning(message('MATLAB:interp1:NaNstrip')); end end %-------------------------------------------------------------------------% function sanitycheck(X,V) if ~isvector(X) error(message('MATLAB:interp1:Xvector')); end if ~isnumeric(X) error(message('MATLAB:interp1:Xnumeric')); end if length(X) ~= size(V,1); if isvector(V) error(message('MATLAB:interp1:YVectorInvalidNumRows')) else error(message('MATLAB:interp1:YInvalidNumRows')); end end end %-------------------------------------------------------------------------% % 'nearest' - nearest neighbor interpolation % 'linear' - linear interpolation % 'spline' - piecewise cubic spline interpolation (SPLINE) % 'pchip' - shape-preserving piecewise cubic interpolation % 'cubic' - same as 'pchip' % 'v5cubic' - the cubic interpolation from MATLAB 5, which does not function methodname = sanitycheckmethod(method) if isempty(method) methodname = 'linear'; warning(message('MATLAB:interp1:EmptyMethod')); else if method(1) == '*' method(1) = []; end switch lower(method(1)) case 'n' methodname = 'nearest'; case 'l' methodname = 'linear'; case 's' methodname = 'spline'; case 'c' methodname = 'pchip'; warning(message('MATLAB:interp1:UsePCHIP')); case 'p' methodname = 'pchip'; case 'v' % 'v5cubic' methodname = 'cubic'; otherwise error(message('MATLAB:interp1:InvalidMethod')); end end end %-------------------------------------------------------------------------% function pp = ppinterp(X,V, orig_size_v, method) %PPINTERP ppform interpretation. n = size(V,1); ds = 1; prodDs = 1; if ~isvector(V) ds = orig_size_v(2:end); prodDs = size(V,2); end switch method(1) case 'n' % nearest breaks = [X(1); ... (X(1:end-1)+X(2:end))/2; ... X(end)].'; coefs = V.'; pp = mkpp(breaks,coefs,ds); case 'l' % linear breaks = X.'; page1 = (diff(V)./repmat(diff(X),[1, prodDs])).'; page2 = (reshape(V(1:end-1,:),[n-1, prodDs])).'; coefs = cat(3,page1,page2); pp = mkpp(breaks,coefs,ds); case 'p' % pchip and cubic pp = pchip(X.',reshape(V.',[ds, n])); case 's' % spline pp = spline(X.',reshape(V.',[ds, n])); case 'c' % v5cubic b = diff(X); if norm(diff(b),Inf) <= eps(norm(X...

2   Pass
%% x = 'sum'; y_correct = true; assert(isequal(SupportedFunction(x),y_correct))

'sum' is a built-in function.

3   Pass
%% x = 'pretty'; y_correct = false; assert(isequal(SupportedFunction(x),y_correct))

4   Pass
%% x = 'limit'; y_correct = false; assert(isequal(SupportedFunction(x),y_correct))

5   Pass
%% x = 'fakefunction'; y_correct = false; assert(isequal(SupportedFunction(x),y_correct))