Mercurial > hg > octave-nkf
annotate scripts/polynomial/splinefit.m @ 14868:5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
* lin2mu.m, loadaudio.m, wavread.m, accumarray.m, bicubic.m, celldisp.m,
colon.m, cplxpair.m, dblquad.m, divergence.m, genvarname.m, gradient.m,
int2str.m, interp1.m, interp1q.m, interp2.m, interpn.m, loadobj.m, nthargout.m,
__isequal__.m, __splinen__.m, quadgk.m, quadl.m, quadv.m, rat.m, rot90.m,
rotdim.m, saveobj.m, subsindex.m, triplequad.m, delaunay3.m, griddata.m,
inpolygon.m, tsearchn.m, voronoi.m, get_first_help_sentence.m, which.m,
gray2ind.m, pink.m, dlmwrite.m, strread.m, textread.m, textscan.m, housh.m,
ishermitian.m, issymmetric.m, krylov.m, logm.m, null.m, rref.m,
compare_versions.m, copyfile.m, dump_prefs.m, edit.m, fileparts.m,
getappdata.m, isappdata.m, movefile.m, orderfields.m, parseparams.m,
__xzip__.m, rmappdata.m, setappdata.m, swapbytes.m, unpack.m, ver.m, fminbnd.m,
fminunc.m, fsolve.m, glpk.m, lsqnonneg.m, qp.m, sqp.m, configure_make.m,
copy_files.m, describe.m, get_description.m, get_forge_pkg.m, install.m,
installed_packages.m, is_architecture_dependent.m, load_package_dirs.m,
print_package_description.m, rebuild.m, repackage.m, save_order.m, shell.m,
allchild.m, ancestor.m, area.m, axes.m, axis.m, clabel.m, close.m, colorbar.m,
comet.m, comet3.m, contour.m, cylinder.m, ezmesh.m, ezsurf.m, findobj.m,
fplot.m, hist.m, isocolors.m, isonormals.m, isosurface.m, isprop.m, legend.m,
mesh.m, meshz.m, pareto.m, pcolor.m, peaks.m, plot3.m, plotmatrix.m, plotyy.m,
polar.m, print.m, __add_datasource__.m, __add_default_menu__.m,
__axes_limits__.m, __bar__.m, __clabel__.m, __contour__.m, __errcomm__.m,
__errplot__.m, __ezplot__.m, __file_filter__.m, __fltk_print__.m,
__ghostscript__.m, __gnuplot_print__.m, __go_draw_axes__.m,
__go_draw_figure__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m,
__pie__.m, __plt__.m, __print_parse_opts__.m, __quiver__.m, __scatter__.m,
__stem__.m, __tight_eps_bbox__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiputfile_fltk__.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m,
ribbon.m, scatter.m, semilogy.m, shading.m, slice.m, subplot.m, surface.m,
surfl.m, surfnorm.m, text.m, uigetfile.m, uiputfile.m, whitebg.m, deconv.m,
mkpp.m, pchip.m, polyaffine.m, polyder.m, polygcd.m, polyout.m, polyval.m,
ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, splinefit.m,
addpref.m, getpref.m, setpref.m, ismember.m, setxor.m, arch_fit.m, arch_rnd.m,
arch_test.m, autoreg_matrix.m, diffpara.m, fftconv.m, filter2.m, hanning.m,
hurst.m, periodogram.m, triangle_sw.m, sinc.m, spectral_xdf.m, spencer.m,
stft.m, synthesis.m, unwrap.m, yulewalker.m, bicgstab.m, gmres.m, pcg.m, pcr.m,
__sprand_impl__.m, speye.m, spfun.m, sprandn.m, spstats.m, svds.m,
treelayout.m, treeplot.m, bessel.m, factor.m, legendre.m, perms.m, primes.m,
magic.m, toeplitz.m, corr.m, cov.m, mean.m, median.m, mode.m, qqplot.m,
quantile.m, ranks.m, zscore.m, logistic_regression_likelihood.m,
bartlett_test.m, chisquare_test_homogeneity.m, chisquare_test_independence.m,
kolmogorov_smirnov_test.m, run_test.m, u_test.m, wilcoxon_test.m, z_test.m,
z_test_2.m, bin2dec.m, dec2base.m, mat2str.m, strcat.m, strchr.m, strjust.m,
strtok.m, substr.m, untabify.m, assert.m, demo.m, example.m, fail.m, speed.m,
test.m, now.m: Use Octave coding conventions for cuddling parentheses in
scripts directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 17 Jul 2012 07:08:39 -0700 |
parents | 1804d5422f61 |
children | f0b04a20d7cf |
rev | line source |
---|---|
14509 | 1 ## Copyright (C) 2012 Ben Abbott, Jonas Lundgren |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
20 ## @deftypefn {Function File} {@var{pp} =} splinefit (@var{x}, @var{y}, @var{breaks}) |
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
21 ## Fit a piecewise cubic spline with breaks (knots) @var{breaks} to the |
14509 | 22 ## noisy data, @var{x} and @var{y}. @var{x} is a vector, and @var{y} |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
23 ## a vector or N-D array. If @var{y} is an N-D array, then @var{x}(j) |
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
24 ## is matched to @var{y}(:,@dots{},:,j). |
14509 | 25 ## |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
26 ## The fitted spline is returned as a piecewise polynomial, @var{pp}, and |
14509 | 27 ## may be evaluated using @code{ppval}. |
28 ## | |
29 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@var{x}, @var{y}, @var{p}) | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
30 ## @var{p} is a positive integer defining the number of intervals along @var{x}, |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
31 ## and @var{p}+1 is the number of breaks. The number of points in each interval |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
32 ## differ by no more than 1. |
14509 | 33 ## |
34 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "periodic", @var{periodic}) | |
35 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "robust", @var{robust}) | |
36 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "beta", @var{beta}) | |
37 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "order", @var{order}) | |
38 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "constraints", @var{constraints}) | |
39 ## | |
40 ## The optional property @var{periodic} is a logical value which specifies | |
41 ## whether a periodic boundary condition is applied to the spline. The | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
42 ## length of the period is @code{max (@var{breaks}) - min (@var{breaks})}. |
14509 | 43 ## The default value is @code{false}. |
44 ## | |
45 ## The optional property @var{robust} is a logical value which specifies | |
46 ## if robust fitting is to be applied to reduce the influence of outlying | |
47 ## data points. Three iterations of weighted least squares are performed. | |
48 ## Weights are computed from previous residuals. The sensitivity of outlier | |
49 ## identification is controlled by the property @var{beta}. The value of | |
50 ## @var{beta} is stricted to the range, 0 < @var{beta} < 1. The default | |
51 ## value is @var{beta} = 1/2. Values close to 0 give all data equal | |
52 ## weighting. Increasing values of @var{beta} reduce the influence of | |
53 ## outlying data. Values close to unity may cause instability or rank | |
54 ## deficiency. | |
55 ## | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
56 ## The splines are constructed of polynomials with degree @var{order}. |
14509 | 57 ## The default is a cubic, @var{order}=3. A spline with P pieces has |
58 ## P+@var{order} degrees of freedom. With periodic boundary conditions | |
59 ## the degrees of freedom are reduced to P. | |
60 ## | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
61 ## The optional property, @var{constaints}, is a structure specifying |
14509 | 62 ## linear constraints on the fit. The structure has three fields, "xc", |
63 ## "yc", and "cc". | |
64 ## | |
65 ## @table @asis | |
66 ## @item "xc" | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
67 ## Vector of the x-locations of the constraints. |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
68 ## |
14509 | 69 ## @item "yc" |
14621
1804d5422f61
doc: Periodic spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14620
diff
changeset
|
70 ## Constraining values at the locations, @var{xc}. |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
71 ## The default is an array of zeros. |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
72 ## |
14509 | 73 ## @item "cc" |
74 ## Coefficients (matrix). The default is an array of ones. The number of | |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
75 ## rows is limited to the order of the piecewise polynomials, @var{order}. |
14509 | 76 ## @end table |
77 ## | |
78 ## Constraints are linear combinations of derivatives of order 0 to | |
79 ## @var{order}-1 according to | |
80 ## | |
81 ## @example | |
82 ## @group | |
83 ## @tex | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
84 ## $cc(1,j) \cdot y(xc(j)) + cc(2,j) \cdot y\prime(xc(j)) + ... = yc(:,\dots,:,j)$. |
14509 | 85 ## @end tex |
86 ## @ifnottex | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
87 ## cc(1,j) * y(xc(j)) + cc(2,j) * y'(xc(j)) + ... = yc(:,...,:,j). |
14509 | 88 ## @end ifnottex |
89 ## @end group | |
90 ## @end example | |
91 ## | |
92 ## @seealso{interp1, unmkpp, ppval, spline, pchip, ppder, ppint, ppjumps} | |
93 ## @end deftypefn | |
94 | |
95 %!demo | |
96 %! % Noisy data | |
97 %! x = linspace (0, 2*pi, 100); | |
98 %! y = sin (x) + 0.1 * randn (size (x)); | |
99 %! % Breaks | |
100 %! breaks = [0:5, 2*pi]; | |
101 %! % Fit a spline of order 5 | |
102 %! pp = splinefit (x, y, breaks, "order", 4); | |
103 %! clf () | |
104 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
105 %! xlabel ("Independent Variable") | |
106 %! ylabel ("Dependent Variable") | |
107 %! title ("Fit a piece-wise polynomial of order 4"); | |
108 %! legend ({"data", "fit", "breaks"}) | |
109 %! axis tight | |
110 %! ylim auto | |
111 | |
112 %!demo | |
113 %! % Noisy data | |
114 %! x = linspace (0,2*pi, 100); | |
115 %! y = sin (x) + 0.1 * randn (size (x)); | |
116 %! % Breaks | |
117 %! breaks = [0:5, 2*pi]; | |
118 %! % Fit a spline of order 3 with periodic boundary conditions | |
119 %! pp = splinefit (x, y, breaks, "order", 2, "periodic", true); | |
120 %! clf () | |
121 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
122 %! xlabel ("Independent Variable") | |
123 %! ylabel ("Dependent Variable") | |
124 %! title ("Fit a periodic piece-wise polynomial of order 2"); | |
125 %! legend ({"data", "fit", "breaks"}) | |
126 %! axis tight | |
127 %! ylim auto | |
128 | |
129 %!demo | |
130 %! % Noisy data | |
131 %! x = linspace (0, 2*pi, 100); | |
132 %! y = sin (x) + 0.1 * randn (size (x)); | |
133 %! % Breaks | |
134 %! breaks = [0:5, 2*pi]; | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
135 %! % Constraints: y(0) = 0, y'(0) = 1 and y(3) + y"(3) = 0 |
14509 | 136 %! xc = [0 0 3]; |
137 %! yc = [0 1 0]; | |
138 %! cc = [1 0 1; 0 1 0; 0 0 1]; | |
139 %! con = struct ("xc", xc, "yc", yc, "cc", cc); | |
140 %! % Fit a cubic spline with 8 pieces and constraints | |
141 %! pp = splinefit (x, y, 8, "constraints", con); | |
142 %! clf () | |
143 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
144 %! xlabel ("Independent Variable") | |
145 %! ylabel ("Dependent Variable") | |
146 %! title ("Fit a cubic spline with constraints") | |
147 %! legend ({"data", "fit", "breaks"}) | |
148 %! axis tight | |
149 %! ylim auto | |
150 | |
151 %!demo | |
152 %! % Noisy data | |
153 %! x = linspace (0, 2*pi, 100); | |
154 %! y = sin (x) + 0.1 * randn (size (x)); | |
155 %! % Breaks | |
156 %! breaks = [0:5, 2*pi]; | |
157 %! xc = [0 0 3]; | |
158 %! yc = [0 1 0]; | |
159 %! cc = [1 0 1; 0 1 0; 0 0 1]; | |
160 %! con = struct ("xc", xc, "yc", yc, "cc", cc); | |
161 %! % Fit a spline of order 6 with constraints and periodicity | |
162 %! pp = splinefit (x, y, breaks, "constraints", con, "order", 5, "periodic", true); | |
163 %! clf () | |
164 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
165 %! xlabel ("Independent Variable") | |
166 %! ylabel ("Dependent Variable") | |
167 %! title ("Fit a 5th order piece-wise periodic polynomial with constraints") | |
168 %! legend ({"data", "fit", "breaks"}) | |
169 %! axis tight | |
170 %! ylim auto | |
171 | |
172 function pp = splinefit (x, y, breaks, varargin) | |
173 if (nargin > 3) | |
174 n = cellfun (@ischar, varargin, "uniformoutput", true); | |
175 varargin(n) = lower (varargin(n)); | |
176 try | |
177 props = struct (varargin{:}); | |
178 catch | |
179 print_usage (); | |
180 end_try_catch | |
181 else | |
182 props = struct (); | |
183 endif | |
184 fields = fieldnames (props); | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
185 for f = 1:numel (fields) |
14524
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
186 if (! any (strcmp (fields{f}, |
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
187 {"periodic", "robust", "beta", "order", "constraints"}))) |
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
188 error ("splinefit:invalidproperty", |
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
189 "unrecognized property '%s'", fields{f}); |
14509 | 190 endif |
191 endfor | |
192 args = {}; | |
193 if (isfield (props, "periodic") && props.periodic) | |
194 args{end+1} = "p"; | |
195 endif | |
196 if (isfield (props, "robust") && props.robust) | |
197 args{end+1} = "r"; | |
198 endif | |
199 if (isfield (props, "beta")) | |
200 if (0 < props.beta && props.beta < 1) | |
201 args{end+1} = props.beta; | |
202 else | |
14524
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
203 error ("splinefit:invalidbeta", "invalid beta parameter (0 < beta < 1)"); |
14509 | 204 endif |
205 endif | |
206 if (isfield (props, "order")) | |
207 if (props.order >= 0) | |
208 args{end+1} = props.order + 1; | |
209 else | |
14524
27f028a670b4
avoid nesting sprintf inside calls to error
John W. Eaton <jwe@octave.org>
parents:
14509
diff
changeset
|
210 error ("splinefit:invalidorder", "invalid order"); |
14509 | 211 endif |
212 endif | |
213 if (isfield (props, "constraints")) | |
214 args{end+1} = props.constraints; | |
215 endif | |
216 if (nargin < 3) | |
217 print_usage (); | |
218 elseif (! isnumeric (breaks) || ! isvector (breaks)) | |
219 print_usage (); | |
220 endif | |
221 pp = __splinefit__ (x, y, breaks, args{:}); | |
222 endfunction | |
223 | |
224 %!shared xb, yb, x | |
225 %! xb = 0:2:10; | |
226 %! yb = randn (size (xb)); | |
227 %! x = 0:0.1:10; | |
228 | |
229 %!test | |
230 %! y = interp1 (xb, yb, x, "linear"); | |
231 %! assert (ppval (splinefit (x, y, xb, "order", 1), x), y, 10 * eps ()); | |
232 %!test | |
233 %! y = interp1 (xb, yb, x, "spline"); | |
234 %! assert (ppval (splinefit (x, y, xb, "order", 3), x), y, 10 * eps ()); | |
235 %!test | |
236 %! y = interp1 (xb, yb, x, "spline"); | |
237 %! assert (ppval (splinefit (x, y, xb), x), y, 10 * eps ()); | |
238 | |
239 |