📄 ppval1.m
字号:
function v=ppval1(ppfun,xx)
%PPVAL1 Fast 1-D Piecewise Polynomial Evaluation.
% PPVAL1(PP,XI) evaluates the piecewise polynomial PP at the points
% specified in XI.
% This function is equivalent to PPVAL in MATLAB, but faster since it is
% restricted to 1-D polynomials and it uses HISTC to find the local
% coordinates of each point in XI. For scalar XI, it is even faster.
%
% This function is used by PPCREATE, PPINTEGRAL, PPGRADIENT
% D.C. Hanselman, University of Maine, Orono, ME 04469
% MasteringMatlab@yahoo.com
% Mastering MATLAB 7
% 2005-10-17
if isa(ppfun,'function_handle') % accept function input to be nice
pp=ppfun('pp');
else
pp=ppfun;
end
if ~isstruct(pp) || ~isfield(pp,'form') ...
|| ~strcmp(pp.form,'pp') || (pp.dim~=1);
error('Input Does Not Contain a 1-D Piecewise Polynomial.')
end
if numel(xx)==1 % scalar input
idx=find(xx>=pp.breaks);
if isempty(idx) % extrapolate if necessary
idx=1;
elseif idx(end)>pp.pieces
idx=pp.pieces;
end
xs=xx-pp.breaks(idx(end)); % local coordinates
c=pp.coefs(idx(end),:); % local polynomial
if pp.order==4 % quick eval for cubic spline
v=((c(1)*xs + c(2))*xs + c(3))*xs +c(4);
else
v=c(1);
for i=2:pp.order % apply Horner's method
v=xs.*v+c(i);
end
end
else % array input
x=pp.breaks;
c=pp.coefs.';
[rx,cx] = size(xx);
xs=xx(:).';
tosort=false;
if any(diff(xs)<0)
tosort=true;
[xs,ix]=sort(xs);
end
% for each data point, compute its breakpoint interval
[idx,idx]=histc(xs,x);
idx(xs<x(1)|~isfinite(xs))=1; % extrapolate using first
idx(xs>=x(end))=pp.pieces; % and last polynomial
xs=xs-x(idx); % local coordinates
if pp.order==4 % quick eval for cubic spline
v=((c(1,idx).*xs + c(2,idx)).*xs + c(3,idx)).*xs +c(4,idx);
else
v=c(1,idx);
for i=2:pp.order % apply Horner's method
v=xs.*v+c(i,idx);
end
end
if tosort
v(ix)=v;
end
v=reshape(v,rx,cx);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -