-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppint.m
70 lines (61 loc) · 1.65 KB
/
ppint.m
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
function output = ppint(pp,a,b)
%PPINT Integrate piecewise polynomial.
% QQ = PPINT(PP,A) returns the indefinite integral from A to X of a
% piecewise polynomial PP. PP must be on the form evaluated by PPVAL.
% QQ is a piecewise polynomial on the same form. Default value for A is
% the leftmost break of PP.
%
% I = PPINT(PP,A,B) returns the definite integral from A to B.
%
% Example:
% x = linspace(-pi,pi,7);
% y = sin(x);
% pp = spline(x,y);
% I = ppint(pp,0,pi)
%
% qq = ppint(pp,pi/2);
% xx = linspace(-pi,pi,201);
% plot(xx,-cos(xx),xx,ppval(qq,xx),'r')
%
% See also PPVAL, SPLINE, SPLINEFIT, PPDIFF
% Author: Jonas Lundgren <splinefit@gmail.com> 2009
if nargin < 1, help ppint, return, end
if nargin < 2, a = pp.breaks(1); end
% Get coefficients and breaks
coefs = pp.coefs;
[m n] = size(coefs);
xb = pp.breaks;
pdim = prod(pp.dim);
% Interval lengths
hb = diff(xb);
hb = repmat(hb,pdim,1);
hb = hb(:);
% Integration
coefs(:,1) = coefs(:,1)/n;
y = coefs(:,1).*hb;
for k = 2:n
coefs(:,k) = coefs(:,k)/(n-k+1);
y = (y + coefs(:,k)).*hb;
end
y = reshape(y,pdim,[]);
I = cumsum(y,2);
I = I(:);
coefs(:,n+1) = [zeros(pdim,1); I(1:m-pdim)];
% Set preliminary indefinite integral
qq = pp;
qq.coefs = coefs;
qq.order = n+1;
% Set output
if nargin < 3
% Indefinite integral from a to x
if a ~= xb(1)
I0 = ppval(qq,a);
I0 = I0(:);
I0 = repmat(I0,m/pdim,1);
qq.coefs(:,n+1) = qq.coefs(:,n+1) - I0;
end
output = qq;
else
% Definite integral from a to b
output = ppval(qq,b) - ppval(qq,a);
end