Mercurial > hg > octave-nkf
annotate scripts/polynomial/splinefit.m @ 19775:c8cb111e727e
maint: Periodic merge of gui-release to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 14 Jan 2015 13:54:28 -0500 |
parents | d63878346099 |
children | 4197fc428c7d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17386
diff
changeset
|
1 ## Copyright (C) 2012-2013 Ben Abbott, Jonas Lundgren |
14509 | 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 -*- | |
16491
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
20 ## @deftypefn {Function File} {@var{pp} =} splinefit (@var{x}, @var{y}, @var{breaks}) |
16489
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@var{x}, @var{y}, @var{p}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "periodic", @var{periodic}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "robust", @var{robust}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "beta", @var{beta}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
25 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "order", @var{order}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
26 ## @deftypefnx {Function File} {@var{pp} =} splinefit (@dots{}, "constraints", @var{constraints}) |
36dba9be680b
doc: Make documentation compatible with Texinfo 5.0 (bug #38392)
Amod Mulay <amodmulay1@gmail.com>
parents:
15082
diff
changeset
|
27 ## |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
28 ## Fit a piecewise cubic spline with breaks (knots) @var{breaks} to the |
14509 | 29 ## noisy data, @var{x} and @var{y}. @var{x} is a vector, and @var{y} |
16491
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
30 ## is a vector or N-D array. If @var{y} is an N-D array, then @var{x}(j) |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
31 ## is matched to @var{y}(:,@dots{},:,j). |
14509 | 32 ## |
14620
cd375519eab0
doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents:
14551
diff
changeset
|
33 ## The fitted spline is returned as a piecewise polynomial, @var{pp}, and |
14509 | 34 ## may be evaluated using @code{ppval}. |
35 ## | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
36 ## @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
|
37 ## 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
|
38 ## differ by no more than 1. |
14509 | 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 |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16491
diff
changeset
|
62 ## linear constraints on the fit. The structure has three fields, @qcode{"xc"}, |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16491
diff
changeset
|
63 ## @qcode{"yc"}, and @qcode{"cc"}. |
14509 | 64 ## |
65 ## @table @asis | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16491
diff
changeset
|
66 ## @item @qcode{"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 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16491
diff
changeset
|
69 ## @item @qcode{"yc"} |
16491
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
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 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16491
diff
changeset
|
73 ## @item @qcode{"cc"} |
14509 | 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 | |
16491
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
95 function pp = splinefit (x, y, breaks, varargin) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
96 if (nargin > 3) |
17386
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
97 n = cellfun ("isclass", varargin, "char"); |
16491
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
98 varargin(n) = lower (varargin(n)); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
99 try |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
100 props = struct (varargin{:}); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
101 catch |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
102 print_usage (); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
103 end_try_catch |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
104 else |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
105 props = struct (); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
106 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
107 fields = fieldnames (props); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
108 for f = 1:numel (fields) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
109 if (! any (strcmp (fields{f}, |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
110 {"periodic", "robust", "beta", "order", "constraints"}))) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
111 error ("splinefit:invalidproperty", |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
112 "unrecognized property '%s'", fields{f}); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
113 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
114 endfor |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
115 args = {}; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
116 if (isfield (props, "periodic") && props.periodic) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
117 args{end+1} = "p"; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
118 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
119 if (isfield (props, "robust") && props.robust) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
120 args{end+1} = "r"; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
121 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
122 if (isfield (props, "beta")) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
123 if (0 < props.beta && props.beta < 1) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
124 args{end+1} = props.beta; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
125 else |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
126 error ("splinefit:invalidbeta", "invalid beta parameter (0 < beta < 1)"); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
127 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
128 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
129 if (isfield (props, "order")) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
130 if (props.order >= 0) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
131 args{end+1} = props.order + 1; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
132 else |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
133 error ("splinefit:invalidorder", "invalid order"); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
134 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
135 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
136 if (isfield (props, "constraints")) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
137 args{end+1} = props.constraints; |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
138 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
139 if (nargin < 3) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
140 print_usage (); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
141 elseif (! isnumeric (breaks) || ! isvector (breaks)) |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
142 print_usage (); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
143 endif |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
144 pp = __splinefit__ (x, y, breaks, args{:}); |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
145 endfunction |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
146 |
b10a23fe80bb
doc: Tweak docstrings of functions changed for Texinfo 5.0 compatibility.
Rik <rik@octave.org>
parents:
16489
diff
changeset
|
147 |
14509 | 148 %!demo |
149 %! % Noisy data | |
150 %! x = linspace (0, 2*pi, 100); | |
151 %! y = sin (x) + 0.1 * randn (size (x)); | |
152 %! % Breaks | |
153 %! breaks = [0:5, 2*pi]; | |
154 %! % Fit a spline of order 5 | |
155 %! pp = splinefit (x, y, breaks, "order", 4); | |
156 %! clf () | |
157 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
158 %! xlabel ("Independent Variable") | |
159 %! ylabel ("Dependent Variable") | |
160 %! title ("Fit a piece-wise polynomial of order 4"); | |
161 %! legend ({"data", "fit", "breaks"}) | |
162 %! axis tight | |
163 %! ylim auto | |
164 | |
165 %!demo | |
166 %! % Noisy data | |
167 %! x = linspace (0,2*pi, 100); | |
168 %! y = sin (x) + 0.1 * randn (size (x)); | |
169 %! % Breaks | |
170 %! breaks = [0:5, 2*pi]; | |
171 %! % Fit a spline of order 3 with periodic boundary conditions | |
172 %! pp = splinefit (x, y, breaks, "order", 2, "periodic", true); | |
173 %! clf () | |
174 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
175 %! xlabel ("Independent Variable") | |
176 %! ylabel ("Dependent Variable") | |
177 %! title ("Fit a periodic piece-wise polynomial of order 2"); | |
178 %! legend ({"data", "fit", "breaks"}) | |
179 %! axis tight | |
180 %! ylim auto | |
181 | |
182 %!demo | |
183 %! % Noisy data | |
184 %! x = linspace (0, 2*pi, 100); | |
185 %! y = sin (x) + 0.1 * randn (size (x)); | |
186 %! % Breaks | |
187 %! breaks = [0:5, 2*pi]; | |
14551
60ed9260399a
Improve/correct documentation for splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
14531
diff
changeset
|
188 %! % Constraints: y(0) = 0, y'(0) = 1 and y(3) + y"(3) = 0 |
14509 | 189 %! xc = [0 0 3]; |
190 %! yc = [0 1 0]; | |
191 %! cc = [1 0 1; 0 1 0; 0 0 1]; | |
192 %! con = struct ("xc", xc, "yc", yc, "cc", cc); | |
193 %! % Fit a cubic spline with 8 pieces and constraints | |
194 %! pp = splinefit (x, y, 8, "constraints", con); | |
195 %! clf () | |
196 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
197 %! xlabel ("Independent Variable") | |
198 %! ylabel ("Dependent Variable") | |
199 %! title ("Fit a cubic spline with constraints") | |
200 %! legend ({"data", "fit", "breaks"}) | |
201 %! axis tight | |
202 %! ylim auto | |
203 | |
204 %!demo | |
205 %! % Noisy data | |
206 %! x = linspace (0, 2*pi, 100); | |
207 %! y = sin (x) + 0.1 * randn (size (x)); | |
208 %! % Breaks | |
209 %! breaks = [0:5, 2*pi]; | |
210 %! xc = [0 0 3]; | |
211 %! yc = [0 1 0]; | |
212 %! cc = [1 0 1; 0 1 0; 0 0 1]; | |
213 %! con = struct ("xc", xc, "yc", yc, "cc", cc); | |
214 %! % Fit a spline of order 6 with constraints and periodicity | |
215 %! pp = splinefit (x, y, breaks, "constraints", con, "order", 5, "periodic", true); | |
216 %! clf () | |
217 %! plot (x, y, "s", x, ppval (pp, x), "r", breaks, ppval (pp, breaks), "+r") | |
218 %! xlabel ("Independent Variable") | |
219 %! ylabel ("Dependent Variable") | |
220 %! title ("Fit a 5th order piece-wise periodic polynomial with constraints") | |
221 %! legend ({"data", "fit", "breaks"}) | |
222 %! axis tight | |
223 %! ylim auto | |
224 | |
225 %!shared xb, yb, x | |
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 | 228 %! x = 0:0.1:10; |
229 | |
230 %!test | |
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 | 233 %!test |
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 | 236 %!test |
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 | 239 |