This is a static copy of a profile reportHome
spap2 (202 calls, 2.423 sec)
Generated 05-Nov-2014 07:53:25 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/curvefit/splines/spap2.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
127 | [knots{i},v,sizev(m+1),k(i)] =... | 404 | 2.283 s | 94.2% |  |
136 | sp = spmak(knots, v, sizev); | 202 | 0.060 s | 2.5% |  |
129 | v = reshape(v,sizev); | 404 | 0.030 s | 1.2% |  |
132 | end | 404 | 0.010 s | 0.4% |  |
131 | v = permute(v,[1,m+1,2:m]); si... | 404 | 0.010 s | 0.4% |  |
All other lines | | | 0.030 s | 1.2% |  |
Totals | | | 2.423 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
spap2>spap21 | subfunction | 404 | 2.253 s | 93.0% |  |
spmak | function | 202 | 0.050 s | 2.1% |  |
spbrk | function | 404 | 0.020 s | 0.8% |  |
Self time (built-ins, overhead, etc.) | | | 0.100 s | 4.1% |  |
Totals | | | 2.423 s | 100% | |
Code Analyzer results
Coverage results
[ Show coverage for parent directory ]
Total lines in function | 141 |
Non-code lines (comments, blank lines) | 106 |
Code lines (lines that can run) | 35 |
Code lines that did run | 26 |
Code lines that did not run | 9 |
Coverage (did run/can run) | 74.29 % |
Function listing
time calls line
1 function sp = spap2(knots,k,x,y,w)
2 %SPAP2 Least squares spline approximation.
3 %
4 % SPAP2(KNOTS,K,X,Y) returns the B-form of the least-squares approximation
5 % f to the data X, Y by splines of order K with knot sequence KNOTS.
6 % The spline approximates, at the data site X(j), the given data value
7 % Y(:,j), j=1:length(X).
8 % The data values may be scalars, vectors, matrices, or even ND-arrays.
9 % Data points with the same site are replaced by their (weighted) average.
10 %
11 % f is the spline of order K with knot sequence KNOTS for which
12 %
13 % (*) Y = f(X)
14 %
15 % in the mean-square sense, i.e., the one that minimizes
16 %
17 % (**) sum_j W(j) |Y(:,j) - f(X(j))|^2 ,
18 %
19 % with W = ones(size(X)). Other weights can be specified by an optional
20 % additional argument, i.e., by using
21 %
22 % SPAP2(KNOTS,K,X,Y,W) which returns the spline f of order K with knot
23 % sequence KNOTS that minimizes (**). A better choice than the default
24 % W = ones(size(X)) would be the composite trapezoid rule weights
25 % W = ([dx;0]+[0;dx]).'/2 , with dx = diff(X(:)) (assuming that X is
26 % strictly increasing).
27 %
28 % If the data sites satisfy the Schoenberg-Whitney conditions
29 %
30 % (***) KNOTS(j) < X(j) < KNOTS(j+K) ,
31 % j=1:length(X)=length(KNOTS)-K ,
32 %
33 % (with equality permitted at knots of multiplicity K), then f is
34 % the unique spline of that order satisfying (***) exactly. No
35 % spline is returned unless (***) is satisfied for some subsequence
36 % of X.
37 %
38 % Since it might be difficult to supply such a knot sequence for the given
39 % data sites, it is also possible to specify KNOTS as a positive integer,
40 % in which case, if possible, a knot sequence will be supplied that satisfies
41 % (***) for some subsequence of X and results in a spline consisting of KNOTS
42 % polynomial pieces.
43 %
44 % If Y is a matrix or, more generally, an ND array, of size [d1,...,ds,n] say,
45 % then Y(:,...,:,j) is the value being approximated at X(j), and the
46 % resulting spline is correspondingly [d1,...,ds]-valued. In that case, the
47 % expression |Y(:,j) - f(X(j))|^2 in the error measure (**) is meant as
48 % the sum of squares of all the d1*...*ds entries of Y(:,j)-f(X(j)) .
49 %
50 % It is also possible to fit to gridded data:
51 %
52 % SPAP2( {KNOTS1,...,KNOTSm}, [K1,...,Km], {X1,...,Xm}, Y ) returns
53 % the m-variate tensor-product spline of coordinate order Ki and with knot
54 % sequence KNOTSi in the i-th variable, i=1,...,m, for which
55 %
56 % Y(:,...,:,i1,...,im) = f(X1(i1),...,Xm(im)), all i := (i1,...,im)
57 %
58 % in the (possibly weighted) mean-square sense.
59 % Note the possibility of fitting to vector-valued and even ND-valued data.
60 % However, in contrast to the univariate case, if the data to be fitted are
61 % scalar-valued, then the input array Y is permitted to be m-dimensional,
62 % in which case
63 % Y(i1,...,im) = f(X1(i1),...,Xm(im)), all i := (i1,...,im)
64 % in the (possibly weighted) mean-square sense.
65 %
66 % Example 1:
67 %
68 % spap2(augknt(x([1 end]),2),2,x,y);
69 %
70 % provides the least-squares straight-line fit to data x,y, assuming that
71 % all the sites x(j) lie in the interval [x(1) .. x(end)], while
72 %
73 % spap2(1,2,x,y);
74 %
75 % accomplishes this without that assumption, and, with that assumption,
76 %
77 % w = ones(size(x)); w([1 end]) = 100;
78 % spap2(1,2,x,y,w);
79 %
80 % forces that fit to come very close to the first and last data point.
81 %
82 % Example 2: The statements
83 %
84 % x = -2:.2:2; y=-1:.25:1; [xx, yy] = ndgrid(x,y);
85 % z = exp(-(xx.^2+yy.^2));
86 % sp = spap2({augknt([-2:2],3),2},[3 4],{x,y},z);
87 % fnplt(sp)
88 %
89 % produce the picture of an approximant to a bivariate function.
90 % Use of MESHGRID instead of NDGRID here would produce an error.
91 %
92 % See also SPAPI, SPAPS.
93
94 % Copyright 1987-2010 The MathWorks, Inc.
95 % $Revision: 1.1.6.4 $
96
202 97 if nargin<5, w = []; end
98
202 99 if iscell(knots) % gridded data are to be fitted by tensor product splines
100
202 101 if ~iscell(x)
102 error(message('SPLINES:SPAP2:Xnotcell'))
103 end
0.01 202 104 m = length(knots);
202 105 if m~=length(x)
106 error(message('SPLINES:SPAP2:wrongsizeX'))
107 end
202 108 sizey = size(y);
202 109 if length(sizey)<m
110 error(message('SPLINES:SPAP2:wrongsizeY'))
111 end
112
202 113 if length(sizey)==m, % grid values of a scalar-valued function
202 114 if issparse(y), y = full(y); end
202 115 sizey = [1 sizey];
202 116 end
117
202 118 sizeval = sizey(1:end-m); sizey = [prod(sizeval), sizey(end-m+(1:m))];
0.01 202 119 y = reshape(y, sizey);
120
202 121 if iscell(k), k = cat(2,k{:}); end
202 122 if length(k)==1, k = repmat(k,1,m); end
202 123 if isempty(w), w = cell(1,m); end
124
202 125 v = y; sizev = sizey;
202 126 for i=m:-1:1 % carry out coordinatewise least-squares fitting
2.28 404 127 [knots{i},v,sizev(m+1),k(i)] = spbrk(spap21(knots{i}, k(i), x{i}, ...
128 reshape(v,prod(sizev(1:m)),sizev(m+1)),w{i}));
0.03 404 129 v = reshape(v,sizev);
0.01 404 130 if m>1
0.01 404 131 v = permute(v,[1,m+1,2:m]); sizev(2:m+1) = sizev([m+1,2:m]);
0.01 404 132 end
404 133 end
134 % At this point, V contains the tensor-product B-spline coefficients.
135 % It remains to put together the spline:
0.06 202 136 sp = spmak(knots, v, sizev);
202 137 if length(sizeval)>1, sp = fnchg(sp,'dz',sizeval); end
138
139 else % univariate spline interpolation
140 sp = spap21(knots,k,x,y,w);
141 end
Other subfunctions in this file are not included in this listing.