annotate scripts/polynomial/private/__splinefit__.m @ 19596:0e1f5a750d00

maint: Periodic merge of gui-release to default.
author John W. Eaton <jwe@octave.org>
date Tue, 20 Jan 2015 10:24:46 -0500
parents be8a12acb20a 446c46af4b42
children 83792dd9bcc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14525
624dcb5e978f maint: note reason __splinefit__.m is private instead of subfunction
John W. Eaton <jwe@octave.org>
parents: 14509
diff changeset
1 ## This function is private because it is maintained by Jonas Lundgren
624dcb5e978f maint: note reason __splinefit__.m is private instead of subfunction
John W. Eaton <jwe@octave.org>
parents: 14509
diff changeset
2 ## separtely from Octave. Please do not reformat to match Octave coding
624dcb5e978f maint: note reason __splinefit__.m is private instead of subfunction
John W. Eaton <jwe@octave.org>
parents: 14509
diff changeset
3 ## conventions as that would make it harder to integrate upstream
624dcb5e978f maint: note reason __splinefit__.m is private instead of subfunction
John W. Eaton <jwe@octave.org>
parents: 14509
diff changeset
4 ## changes.
624dcb5e978f maint: note reason __splinefit__.m is private instead of subfunction
John W. Eaton <jwe@octave.org>
parents: 14509
diff changeset
5
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 % Copyright (c) 2010, Jonas Lundgren
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 % All rights reserved.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
8 %
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
9 % Redistribution and use in source and binary forms, with or without
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
10 % modification, are permitted provided that the following conditions are
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 % met:
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
12 %
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
13 % * Redistributions of source code must retain the above copyright
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 % notice, this list of conditions and the following disclaimer.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
15 % * Redistributions in binary form must reproduce the above copyright
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
16 % notice, this list of conditions and the following disclaimer in
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 % the documentation and/or other materials provided with the distribution
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
18 %
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
19 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
20 % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
21 % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
22 % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
23 % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
24 % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
25 % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
26 % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
27 % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17199
diff changeset
28 % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 % POSSIBILITY OF SUCH DAMAGE.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
30 function pp = __splinefit__(varargin)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 %SPLINEFIT Fit a spline to noisy data.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
32 % PP = SPLINEFIT(X,Y,BREAKS) fits a piecewise cubic spline with breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 % (knots) BREAKS to the noisy data (X,Y). X is a vector and Y is a vector
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 % or an ND array. If Y is an ND array, then X(j) and Y(:,...,:,j) are
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 % matched. Use PPVAL to evaluate PP.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 % PP = SPLINEFIT(X,Y,P) where P is a positive integer interpolates the
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 % breaks linearly from the sorted locations of X. P is the number of
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 % spline pieces and P+1 is the number of breaks.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 % OPTIONAL INPUT
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 % Argument places 4 to 8 are reserved for optional input.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 % These optional arguments can be given in any order:
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 % PP = SPLINEFIT(...,'p') applies periodic boundary conditions to
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 % the spline. The period length is MAX(BREAKS)-MIN(BREAKS).
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 % PP = SPLINEFIT(...,'r') uses robust fitting to reduce the influence
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 % from outlying data points. Three iterations of weighted least squares
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 % are performed. Weights are computed from previous residuals.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 % PP = SPLINEFIT(...,BETA), where 0 < BETA < 1, sets the robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 % parameter BETA and activates robust fitting ('r' can be omitted).
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 % Default is BETA = 1/2. BETA close to 0 gives all data equal weighting.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 % Increase BETA to reduce the influence from outlying data. BETA close
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 % to 1 may cause instability or rank deficiency.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 % PP = SPLINEFIT(...,N) sets the spline order to N. Default is a cubic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59 % spline with order N = 4. A spline with P pieces has P+N-1 degrees of
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 % freedom. With periodic boundary conditions the degrees of freedom are
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 % reduced to P.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 % PP = SPLINEFIT(...,CON) applies linear constraints to the spline.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64 % CON is a structure with fields 'xc', 'yc' and 'cc':
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 % 'xc', x-locations (vector)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 % 'yc', y-values (vector or ND array)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 % 'cc', coefficients (matrix).
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 % Constraints are linear combinations of derivatives of order 0 to N-2
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 % according to
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 % cc(1,j)*y(x) + cc(2,j)*y'(x) + ... = yc(:,...,:,j), x = xc(j).
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 % The maximum number of rows for 'cc' is N-1. If omitted or empty 'cc'
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 % defaults to a single row of ones. Default for 'yc' is a zero array.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 % EXAMPLES
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 % % Noisy data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 % x = linspace(0,2*pi,100);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 % y = sin(x) + 0.1*randn(size(x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 % % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 % breaks = [0:5,2*pi];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85 % % Fit a spline of order 5
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86 % pp = splinefit(x,y,breaks,5);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 % % Fit a spline of order 3 with periodic boundary conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 % pp = splinefit(x,y,breaks,3,'p');
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 % % Constraints: y(0) = 0, y'(0) = 1 and y(3) + y"(3) = 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92 % xc = [0 0 3];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 % yc = [0 1 0];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 % cc = [1 0 1; 0 1 0; 0 0 1];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 % con = struct('xc',xc,'yc',yc,'cc',cc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 % % Fit a cubic spline with 8 pieces and constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98 % pp = splinefit(x,y,8,con);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 % % Fit a spline of order 6 with constraints and periodicity
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 % pp = splinefit(x,y,breaks,con,6,'p');
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 %
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103 % See also SPLINE, PPVAL, PPDIFF, PPINT
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
105 % Author: Jonas Lundgren <splinefit@gmail.com> 2010
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107 % 2009-05-06 Original SPLINEFIT.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 % 2010-06-23 New version of SPLINEFIT based on B-splines.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 % 2010-09-01 Robust fitting scheme added.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 % 2010-09-01 Support for data containing NaNs.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111 % 2011-07-01 Robust fitting parameter added.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 % Check number of arguments
19068
be8a12acb20a Deprecate nargchk in favor of narginchk.
Rik <rik@octave.org>
parents: 17199
diff changeset
114 narginchk(3,7);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 % Check arguments
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 [x,y,dim,breaks,n,periodic,beta,constr] = arguments(varargin{:});
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
118
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
119 % Evaluate B-splines
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120 base = splinebase(breaks,n);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
121 pieces = base.pieces;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
122 A = ppval(base,x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
123
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
124 % Bin data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
125 [junk,ibin] = histc(x,[-inf,breaks(2:end-1),inf]); %#ok
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
126
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
127 % Sparse system matrix
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
128 mx = numel(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
129 ii = [ibin; ones(n-1,mx)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
130 ii = cumsum(ii,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
131 jj = repmat(1:mx,n,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
132 if periodic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
133 ii = mod(ii-1,pieces) + 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
134 A = sparse(ii,jj,A,pieces,mx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
135 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
136 A = sparse(ii,jj,A,pieces+n-1,mx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
137 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
138
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
139 % Don't use the sparse solver for small problems
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
140 if pieces < 20*n/log(1.7*n)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
141 A = full(A);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
142 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
143
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
144 % Solve
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
145 if isempty(constr)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
146 % Solve Min norm(u*A-y)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
147 u = lsqsolve(A,y,beta);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
148 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
149 % Evaluate constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
150 B = evalcon(base,constr,periodic);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
151 % Solve constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
152 [Z,u0] = solvecon(B,constr);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
153 % Solve Min norm(u*A-y), subject to u*B = yc
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
154 y = y - u0*A;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
155 A = Z*A;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
156 v = lsqsolve(A,y,beta);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
157 u = u0 + v*Z;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
158 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
159
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
160 % Periodic expansion of solution
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
161 if periodic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
162 jj = mod(0:pieces+n-2,pieces) + 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
163 u = u(:,jj);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
164 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
165
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
166 % Compute polynomial coefficients
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
167 ii = [repmat(1:pieces,1,n); ones(n-1,n*pieces)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
168 ii = cumsum(ii,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
169 jj = repmat(1:n*pieces,n,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
170 C = sparse(ii,jj,base.coefs,pieces+n-1,n*pieces);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
171 coefs = u*C;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
172 coefs = reshape(coefs,[],n);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
173
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
174 % Make piecewise polynomial
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
175 pp = mkpp(breaks,coefs,dim);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
176
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
177
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
178 %--------------------------------------------------------------------------
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
179 function [x,y,dim,breaks,n,periodic,beta,constr] = arguments(varargin)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
180 %ARGUMENTS Lengthy input checking
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
181 % x Noisy data x-locations (1 x mx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
182 % y Noisy data y-values (prod(dim) x mx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
183 % dim Leading dimensions of y
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
184 % breaks Breaks (1 x (pieces+1))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
185 % n Spline order
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
186 % periodic True if periodic boundary conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
187 % beta Robust fitting parameter, no robust fitting if beta = 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
188 % constr Constraint structure
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
189 % constr.xc x-locations (1 x nx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
190 % constr.yc y-values (prod(dim) x nx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
191 % constr.cc Coefficients (?? x nx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
192
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
193 % Reshape x-data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
194 x = varargin{1};
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
195 mx = numel(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
196 x = reshape(x,1,mx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
197
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
198 % Remove trailing singleton dimensions from y
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
199 y = varargin{2};
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
200 dim = size(y);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
201 while numel(dim) > 1 && dim(end) == 1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
202 dim(end) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
203 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
204 my = dim(end);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
205
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
206 % Leading dimensions of y
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
207 if numel(dim) > 1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
208 dim(end) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
209 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
210 dim = 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
211 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
212
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
213 % Reshape y-data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
214 pdim = prod(dim);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
215 y = reshape(y,pdim,my);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
216
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
217 % Check data size
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
218 if mx ~= my
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
219 mess = 'Last dimension of array y must equal length of vector x.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
220 error('arguments:datasize',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
221 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
222
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
223 % Treat NaNs in x-data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
224 inan = find(isnan(x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
225 if ~isempty(inan)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
226 x(inan) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
227 y(:,inan) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
228 mess = 'All data points with NaN as x-location will be ignored.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
229 warning('arguments:nanx',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
230 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
231
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
232 % Treat NaNs in y-data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
233 inan = find(any(isnan(y),1));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
234 if ~isempty(inan)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
235 x(inan) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
236 y(:,inan) = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
237 mess = 'All data points with NaN in their y-value will be ignored.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
238 warning('arguments:nany',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
239 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
240
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
241 % Check number of data points
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
242 mx = numel(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
243 if mx == 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
244 error('arguments:nodata','There must be at least one data point.')
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
245 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
246
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
247 % Sort data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
248 if any(diff(x) < 0)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
249 [x,isort] = sort(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
250 y = y(:,isort);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
251 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
252
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
253 % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
254 if isscalar(varargin{3})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
255 % Number of pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
256 p = varargin{3};
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
257 if ~isreal(p) || ~isfinite(p) || p < 1 || fix(p) < p
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
258 mess = 'Argument #3 must be a vector or a positive integer.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
259 error('arguments:breaks1',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
260 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
261 if x(1) < x(end)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
262 % Interpolate breaks linearly from x-data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
263 dx = diff(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
264 ibreaks = linspace(1,mx,p+1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
265 [junk,ibin] = histc(ibreaks,[0,2:mx-1,mx+1]); %#ok
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
266 breaks = x(ibin) + dx(ibin).*(ibreaks-ibin);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
267 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
268 breaks = x(1) + linspace(0,1,p+1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
269 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
270 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
271 % Vector of breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
272 breaks = reshape(varargin{3},1,[]);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
273 if isempty(breaks) || min(breaks) == max(breaks)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
274 mess = 'At least two unique breaks are required.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
275 error('arguments:breaks2',mess);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
276 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
277 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
278
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
279 % Unique breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
280 if any(diff(breaks) <= 0)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
281 breaks = unique(breaks);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
282 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
283
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
284 % Optional input defaults
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
285 n = 4; % Cubic splines
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
286 periodic = false; % No periodic boundaries
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
287 robust = false; % No robust fitting scheme
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
288 beta = 0.5; % Robust fitting parameter
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
289 constr = []; % No constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
290
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
291 % Loop over optional arguments
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
292 for k = 4:nargin
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
293 a = varargin{k};
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
294 if ischar(a) && isscalar(a) && lower(a) == 'p'
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
295 % Periodic conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
296 periodic = true;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
297 elseif ischar(a) && isscalar(a) && lower(a) == 'r'
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
298 % Robust fitting scheme
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
299 robust = true;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
300 elseif isreal(a) && isscalar(a) && isfinite(a) && a > 0 && a < 1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
301 % Robust fitting parameter
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
302 beta = a;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
303 robust = true;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
304 elseif isreal(a) && isscalar(a) && isfinite(a) && a > 0 && fix(a) == a
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
305 % Spline order
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
306 n = a;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
307 elseif isstruct(a) && isscalar(a)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
308 % Constraint structure
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
309 constr = a;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
310 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
311 error('arguments:nonsense','Failed to interpret argument #%d.',k)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
312 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
313 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
314
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
315 % No robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
316 if ~robust
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
317 beta = 0;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
318 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
319
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
320 % Check exterior data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
321 h = diff(breaks);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
322 xlim1 = breaks(1) - 0.01*h(1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
323 xlim2 = breaks(end) + 0.01*h(end);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
324 if x(1) < xlim1 || x(end) > xlim2
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
325 if periodic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
326 % Move data inside domain
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
327 P = breaks(end) - breaks(1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
328 x = mod(x-breaks(1),P) + breaks(1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
329 % Sort
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
330 [x,isort] = sort(x);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
331 y = y(:,isort);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
332 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
333 mess = 'Some data points are outside the spline domain.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
334 warning('arguments:exteriordata',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
335 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
336 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
337
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
338 % Return
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
339 if isempty(constr)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
340 return
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
341 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
342
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
343 % Unpack constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
344 xc = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
345 yc = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
346 cc = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
347 names = fieldnames(constr);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
348 for k = 1:numel(names)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
349 switch names{k}
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
350 case {'xc'}
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
351 xc = constr.xc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
352 case {'yc'}
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
353 yc = constr.yc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
354 case {'cc'}
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
355 cc = constr.cc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
356 otherwise
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
357 mess = 'Unknown field ''%s'' in constraint structure.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
358 warning('arguments:unknownfield',mess,names{k})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
359 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
360 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
361
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
362 % Check xc
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
363 if isempty(xc)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
364 mess = 'Constraints contains no x-locations.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
365 error('arguments:emptyxc',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
366 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
367 nx = numel(xc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
368 xc = reshape(xc,1,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
369 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
370
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
371 % Check yc
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
372 if isempty(yc)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
373 % Zero array
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
374 yc = zeros(pdim,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
375 elseif numel(yc) == 1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
376 % Constant array
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
377 yc = zeros(pdim,nx) + yc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
378 elseif numel(yc) ~= pdim*nx
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
379 % Malformed array
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
380 error('arguments:ycsize','Cannot reshape yc to size %dx%d.',pdim,nx)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
381 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
382 % Reshape array
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
383 yc = reshape(yc,pdim,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
384 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
385
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
386 % Check cc
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
387 if isempty(cc)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
388 cc = ones(size(xc));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
389 elseif numel(size(cc)) ~= 2
17199
9deb214ae9d5 Use 2-D, not 2D, in error messages.
Rik <rik@octave.org>
parents: 14525
diff changeset
390 error('arguments:ccsize1','Constraint coefficients cc must be 2-D.')
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
391 elseif size(cc,2) ~= nx
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
392 mess = 'Last dimension of cc must equal length of xc.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
393 error('arguments:ccsize2',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
394 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
395
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
396 % Check high order derivatives
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
397 if size(cc,1) >= n
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
398 if any(any(cc(n:end,:)))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
399 mess = 'Constraints involve derivatives of order %d or larger.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
400 error('arguments:difforder',mess,n-1)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
401 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
402 cc = cc(1:n-1,:);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
403 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
404
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
405 % Check exterior constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
406 if min(xc) < xlim1 || max(xc) > xlim2
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
407 if periodic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
408 % Move constraints inside domain
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
409 P = breaks(end) - breaks(1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
410 xc = mod(xc-breaks(1),P) + breaks(1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
411 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
412 mess = 'Some constraints are outside the spline domain.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
413 warning('arguments:exteriorconstr',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
414 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
415 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
416
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
417 % Pack constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
418 constr = struct('xc',xc,'yc',yc,'cc',cc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
419
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
420
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
421 %--------------------------------------------------------------------------
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
422 function pp = splinebase(breaks,n)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
423 %SPLINEBASE Generate B-spline base PP of order N for breaks BREAKS
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
424
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
425 breaks = breaks(:); % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
426 breaks0 = breaks'; % Initial breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
427 h = diff(breaks); % Spacing
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
428 pieces = numel(h); % Number of pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
429 deg = n - 1; % Polynomial degree
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
430
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
431 % Extend breaks periodically
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
432 if deg > 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
433 if deg <= pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
434 hcopy = h;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
435 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
436 hcopy = repmat(h,ceil(deg/pieces),1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
437 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
438 % to the left
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
439 hl = hcopy(end:-1:end-deg+1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
440 bl = breaks(1) - cumsum(hl);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
441 % and to the right
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
442 hr = hcopy(1:deg);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
443 br = breaks(end) + cumsum(hr);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
444 % Add breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
445 breaks = [bl(deg:-1:1); breaks; br];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
446 h = diff(breaks);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
447 pieces = numel(h);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
448 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
449
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
450 % Initiate polynomial coefficients
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
451 coefs = zeros(n*pieces,n);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
452 coefs(1:n:end,1) = 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
453
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
454 % Expand h
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
455 ii = [1:pieces; ones(deg,pieces)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
456 ii = cumsum(ii,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
457 ii = min(ii,pieces);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
458 H = h(ii(:));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
459
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
460 % Recursive generation of B-splines
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
461 for k = 2:n
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
462 % Antiderivatives of splines
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
463 for j = 1:k-1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
464 coefs(:,j) = coefs(:,j).*H/(k-j);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
465 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
466 Q = sum(coefs,2);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
467 Q = reshape(Q,n,pieces);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
468 Q = cumsum(Q,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
469 c0 = [zeros(1,pieces); Q(1:deg,:)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
470 coefs(:,k) = c0(:);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
471 % Normalize antiderivatives by max value
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
472 fmax = repmat(Q(n,:),n,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
473 fmax = fmax(:);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
474 for j = 1:k
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
475 coefs(:,j) = coefs(:,j)./fmax;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
476 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
477 % Diff of adjacent antiderivatives
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
478 coefs(1:end-deg,1:k) = coefs(1:end-deg,1:k) - coefs(n:end,1:k);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
479 coefs(1:n:end,k) = 0;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
480 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
481
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
482 % Scale coefficients
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
483 scale = ones(size(H));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
484 for k = 1:n-1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
485 scale = scale./H;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
486 coefs(:,n-k) = scale.*coefs(:,n-k);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
487 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
488
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
489 % Reduce number of pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
490 pieces = pieces - 2*deg;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
491
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
492 % Sort coefficients by interval number
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
493 ii = [n*(1:pieces); deg*ones(deg,pieces)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
494 ii = cumsum(ii,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
495 coefs = coefs(ii(:),:);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
496
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
497 % Make piecewise polynomial
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
498 pp = mkpp(breaks0,coefs,n);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
499
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
500
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
501 %--------------------------------------------------------------------------
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
502 function B = evalcon(base,constr,periodic)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
503 %EVALCON Evaluate linear constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
504
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
505 % Unpack structures
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
506 breaks = base.breaks;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
507 pieces = base.pieces;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
508 n = base.order;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
509 xc = constr.xc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
510 cc = constr.cc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
511
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
512 % Bin data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
513 [junk,ibin] = histc(xc,[-inf,breaks(2:end-1),inf]); %#ok
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
514
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
515 % Evaluate constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
516 nx = numel(xc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
517 B0 = zeros(n,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
518 for k = 1:size(cc,1)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
519 if any(cc(k,:))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
520 B0 = B0 + repmat(cc(k,:),n,1).*ppval(base,xc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
521 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
522 % Differentiate base
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
523 coefs = base.coefs(:,1:n-k);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
524 for j = 1:n-k-1
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
525 coefs(:,j) = (n-k-j+1)*coefs(:,j);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
526 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
527 base.coefs = coefs;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
528 base.order = n-k;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
529 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
530
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
531 % Sparse output
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
532 ii = [ibin; ones(n-1,nx)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
533 ii = cumsum(ii,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
534 jj = repmat(1:nx,n,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
535 if periodic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
536 ii = mod(ii-1,pieces) + 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
537 B = sparse(ii,jj,B0,pieces,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
538 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
539 B = sparse(ii,jj,B0,pieces+n-1,nx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
540 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
541
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
542
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
543 %--------------------------------------------------------------------------
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
544 function [Z,u0] = solvecon(B,constr)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
545 %SOLVECON Find a particular solution u0 and null space Z (Z*B = 0)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
546 % for constraint equation u*B = yc.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
547
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
548 yc = constr.yc;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
549 tol = 1000*eps;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
550
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
551 % Remove blank rows
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
552 ii = any(B,2);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
553 B2 = full(B(ii,:));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
554
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
555 % Null space of B2
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
556 if isempty(B2)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
557 Z2 = [];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
558 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
559 % QR decomposition with column permutation
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
560 [Q,R,dummy] = qr(B2); %#ok
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
561 R = abs(R);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
562 jj = all(R < R(1)*tol, 2);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
563 Z2 = Q(:,jj)';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
564 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
565
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
566 % Sizes
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
567 [m,ncon] = size(B);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
568 m2 = size(B2,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
569 nz = size(Z2,1);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
570
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
571 % Sparse null space of B
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
572 Z = sparse(nz+1:nz+m-m2,find(~ii),1,nz+m-m2,m);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
573 Z(1:nz,ii) = Z2;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
574
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
575 % Warning rank deficient
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
576 if nz + ncon > m2
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
577 mess = 'Rank deficient constraints, rank = %d.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
578 warning('solvecon:deficient',mess,m2-nz);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
579 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
580
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
581 % Particular solution
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
582 u0 = zeros(size(yc,1),m);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
583 if any(yc(:))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
584 % Non-homogeneous case
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
585 u0(:,ii) = yc/B2;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
586 % Check solution
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
587 if norm(u0*B - yc,'fro') > norm(yc,'fro')*tol
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
588 mess = 'Inconsistent constraints. No solution within tolerance.';
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
589 error('solvecon:inconsistent',mess)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
590 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
591 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
592
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
593
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
594 %--------------------------------------------------------------------------
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
595 function u = lsqsolve(A,y,beta)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
596 %LSQSOLVE Solve Min norm(u*A-y)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
597
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
598 % Avoid sparse-complex limitations
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
599 if issparse(A) && ~isreal(y)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
600 A = full(A);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
601 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
602
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
603 % Solution
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
604 u = y/A;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
605
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
606 % Robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
607 if beta > 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
608 [m,n] = size(y);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
609 alpha = 0.5*beta/(1-beta)/m;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
610 for k = 1:3
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
611 % Residual
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
612 r = u*A - y;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
613 rr = r.*conj(r);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
614 rrmean = sum(rr,2)/n;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
615 rrmean(~rrmean) = 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
616 rrhat = (alpha./rrmean)'*rr;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
617 % Weights
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
618 w = exp(-rrhat);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
619 spw = spdiags(w',0,n,n);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
620 % Solve weighted problem
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
621 u = (y*spw)/(A*spw);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
622 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
623 end
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
624