annotate scripts/polynomial/splinefit.m @ 15538:94d21131fefd

maint: periodic merge of stable to default
author John W. Eaton <jwe@octave.org>
date Wed, 17 Oct 2012 11:51:35 -0400
parents 902a8e27b2a2
children 36dba9be680b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
1 ## Copyright (C) 2012 Ben Abbott, Jonas Lundgren
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This file is part of Octave.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## your option) any later version.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## General Public License for more details.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
27 ## may be evaluated using @code{ppval}.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
28 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "periodic", @var{periodic})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "robust", @var{robust})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "beta", @var{beta})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "order", @var{order})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "constraints", @var{constraints})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 ## The optional property @var{periodic} is a logical value which specifies
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ## The default value is @code{false}.
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 ## The optional property @var{robust} is a logical value which specifies
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 ## if robust fitting is to be applied to reduce the influence of outlying
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 ## data points. Three iterations of weighted least squares are performed.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 ## Weights are computed from previous residuals. The sensitivity of outlier
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 ## identification is controlled by the property @var{beta}. The value of
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 ## @var{beta} is stricted to the range, 0 < @var{beta} < 1. The default
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 ## value is @var{beta} = 1/2. Values close to 0 give all data equal
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 ## weighting. Increasing values of @var{beta} reduce the influence of
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 ## outlying data. Values close to unity may cause instability or rank
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 ## deficiency.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 ## The default is a cubic, @var{order}=3. A spline with P pieces has
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 ## P+@var{order} degrees of freedom. With periodic boundary conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59 ## the degrees of freedom are reduced to P.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62 ## linear constraints on the fit. The structure has three fields, "xc",
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 ## "yc", and "cc".
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 ## @table @asis
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 ## @item "cc"
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 ## @end table
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 ## Constraints are linear combinations of derivatives of order 0 to
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 ## @var{order}-1 according to
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 ## @example
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 ## @group
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85 ## @end tex
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 ## @end ifnottex
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 ## @end group
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 ## @end example
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92 ## @seealso{interp1, unmkpp, ppval, spline, pchip, ppder, ppint, ppjumps}
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 ## @end deftypefn
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 %!demo
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 %! % Noisy data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 %! x = linspace (0, 2*pi, 100);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98 %! y = sin (x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 %! % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 %! breaks = [0:5, 2*pi];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 %! % Fit a spline of order 5
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 %! pp = splinefit (x, y, breaks, "order", 4);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103 %! clf ()
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
105 %! xlabel ("Independent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106 %! ylabel ("Dependent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107 %! title ("Fit a piece-wise polynomial of order 4");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 %! legend ({"data", "fit", "breaks"})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 %! axis tight
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 %! ylim auto
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112 %!demo
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 %! % Noisy data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 %! x = linspace (0,2*pi, 100);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115 %! y = sin (x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 %! % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 %! breaks = [0:5, 2*pi];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
118 %! % Fit a spline of order 3 with periodic boundary conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
119 %! pp = splinefit (x, y, breaks, "order", 2, "periodic", true);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120 %! clf ()
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
121 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
122 %! xlabel ("Independent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
123 %! ylabel ("Dependent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
124 %! title ("Fit a periodic piece-wise polynomial of order 2");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
125 %! legend ({"data", "fit", "breaks"})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
126 %! axis tight
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
127 %! ylim auto
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
128
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
129 %!demo
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
130 %! % Noisy data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
131 %! x = linspace (0, 2*pi, 100);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
132 %! y = sin (x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
133 %! % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
136 %! xc = [0 0 3];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
137 %! yc = [0 1 0];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
138 %! cc = [1 0 1; 0 1 0; 0 0 1];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
139 %! con = struct ("xc", xc, "yc", yc, "cc", cc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
140 %! % Fit a cubic spline with 8 pieces and constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
141 %! pp = splinefit (x, y, 8, "constraints", con);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
142 %! clf ()
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
143 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
144 %! xlabel ("Independent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
145 %! ylabel ("Dependent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
146 %! title ("Fit a cubic spline with constraints")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
147 %! legend ({"data", "fit", "breaks"})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
148 %! axis tight
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
149 %! ylim auto
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
150
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
151 %!demo
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
152 %! % Noisy data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
153 %! x = linspace (0, 2*pi, 100);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
154 %! y = sin (x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
155 %! % Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
156 %! breaks = [0:5, 2*pi];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
157 %! xc = [0 0 3];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
158 %! yc = [0 1 0];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
159 %! cc = [1 0 1; 0 1 0; 0 0 1];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
160 %! con = struct ("xc", xc, "yc", yc, "cc", cc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
161 %! % Fit a spline of order 6 with constraints and periodicity
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
162 %! pp = splinefit (x, y, breaks, "constraints", con, "order", 5, "periodic", true);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
163 %! clf ()
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
164 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
165 %! xlabel ("Independent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
166 %! ylabel ("Dependent Variable")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
167 %! title ("Fit a 5th order piece-wise periodic polynomial with constraints")
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
168 %! legend ({"data", "fit", "breaks"})
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
169 %! axis tight
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
170 %! ylim auto
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
171
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
172 function pp = splinefit (x, y, breaks, varargin)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
173 if (nargin > 3)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
174 n = cellfun (@ischar, varargin, "uniformoutput", true);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
175 varargin(n) = lower (varargin(n));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
176 try
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
177 props = struct (varargin{:});
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
178 catch
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
179 print_usage ();
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
180 end_try_catch
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
181 else
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
182 props = struct ();
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
183 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
190 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
191 endfor
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
192 args = {};
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
193 if (isfield (props, "periodic") && props.periodic)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
194 args{end+1} = "p";
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
195 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
196 if (isfield (props, "robust") && props.robust)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
197 args{end+1} = "r";
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
198 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
199 if (isfield (props, "beta"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
200 if (0 < props.beta && props.beta < 1)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
201 args{end+1} = props.beta;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
204 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
205 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
206 if (isfield (props, "order"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
207 if (props.order >= 0)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
208 args{end+1} = props.order + 1;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
211 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
212 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
213 if (isfield (props, "constraints"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
214 args{end+1} = props.constraints;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
215 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
216 if (nargin < 3)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
217 print_usage ();
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
218 elseif (! isnumeric (breaks) || ! isvector (breaks))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
219 print_usage ();
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
220 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
221 pp = __splinefit__ (x, y, breaks, args{:});
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
222 endfunction
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
223
15082
902a8e27b2a2 splinefit.m: Increase tolerance for %!tests. Use rand, not randn, for generating test data.
Rik <rik@octave.org>
parents: 15077
diff changeset
224
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
225 %!shared xb, yb, x
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
226 %! xb = 0:2:10;
15082
902a8e27b2a2 splinefit.m: Increase tolerance for %!tests. Use rand, not randn, for generating test data.
Rik <rik@octave.org>
parents: 15077
diff changeset
227 %! yb = 2*rand (size (xb)) - 1;
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
228 %! x = 0:0.1:10;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
229
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
230 %!test
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
231 %! y = interp1 (xb, yb, x, "linear");
15082
902a8e27b2a2 splinefit.m: Increase tolerance for %!tests. Use rand, not randn, for generating test data.
Rik <rik@octave.org>
parents: 15077
diff changeset
232 %! assert (ppval (splinefit (x, y, xb, "order", 1), x), y, 15 * eps ());
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
233 %!test
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
234 %! y = interp1 (xb, yb, x, "spline");
15082
902a8e27b2a2 splinefit.m: Increase tolerance for %!tests. Use rand, not randn, for generating test data.
Rik <rik@octave.org>
parents: 15077
diff changeset
235 %! assert (ppval (splinefit (x, y, xb, "order", 3), x), y, 15 * eps ());
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
236 %!test
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
237 %! y = interp1 (xb, yb, x, "spline");
15082
902a8e27b2a2 splinefit.m: Increase tolerance for %!tests. Use rand, not randn, for generating test data.
Rik <rik@octave.org>
parents: 15077
diff changeset
238 %! assert (ppval (splinefit (x, y, xb), x), y, 15 * eps ());
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
239