annotate doc/interpreter/splineimages.m @ 31246:43a6be589387

doc: New documentation for memoization techniques (bug #60860) vectorize.texi: New section on memoization octave.texi: List new section
author Arun Giridhar <arungiridhar@gmail.com>
date Thu, 29 Sep 2022 20:31:52 -0400
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
3 ## Copyright (C) 2012-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27847
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## This file is part of Octave.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24462
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24462
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
14509
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ##
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24462
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
26 function splineimages (d, nm, typ)
28620
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
27
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
28 if (strcmp (typ, "txt"))
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
29 image_as_txt (d, nm);
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
30 return;
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
31 endif
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
32
20335
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
33 set_graphics_toolkit ();
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 set_print_size ();
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 hide_output ();
20316
d8992a16643c doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents: 20307
diff changeset
36 outfile = fullfile (d, [nm "." typ]);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 if (strcmp (typ, "png"))
24462
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
38 set (groot, "defaulttextfontname", "*");
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 if (strcmp (typ, "eps"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 d_typ = "-depsc2";
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 else
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 14526
diff changeset
43 d_typ = ["-d" typ];
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 endif
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45
28620
45a9dcee45db doc: Short-circuit building txt images for manual (bug #57591).
Rik <rik@octave.org>
parents: 27923
diff changeset
46 if (strcmp (nm, "splinefit1")) ## Breaks and Pieces
23633
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
47 rand ("state", 1);
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
48 randn ("state", 1);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 x = 2 * pi * rand (1, 200);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 y = sin (x) + sin (2 * x) + 0.2 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 ## Uniform breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 breaks = linspace (0, 2 * pi, 41); ## 41 breaks, 40 pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 pp1 = splinefit (x, y, breaks);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 ## Breaks interpolated from data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 pp2 = splinefit (x, y, 10); ## 11 breaks, 10 pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 xx = linspace (0, 2 * pi, 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 y1 = ppval (pp1, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59 y2 = ppval (pp2, xx);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
60 plot (x, y, ".", xx, [y1; y2]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
61 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
62 ylim ([-2.5 2.5]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
63 legend ("data", "41 breaks, 40 pieces", "11 breaks, 10 pieces");
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
64 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 elseif (strcmp (nm, "splinefit2")) ## Spline orders
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 ## Data (200 points)
23633
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
67 rand ("state", 1);
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
68 randn ("state", 1);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 x = 2 * pi * rand (1, 200);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 y = sin (x) + sin (2 * x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 ## Splines
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 pp1 = splinefit (x, y, 8, "order", 0); ## Piecewise constant
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 pp2 = splinefit (x, y, 8, "order", 1); ## Piecewise linear
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 pp3 = splinefit (x, y, 8, "order", 2); ## Piecewise quadratic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 pp4 = splinefit (x, y, 8, "order", 3); ## Piecewise cubic
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 pp5 = splinefit (x, y, 8, "order", 4); ## Etc.
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 xx = linspace (0, 2 * pi, 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 y1 = ppval (pp1, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 y2 = ppval (pp2, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 y3 = ppval (pp3, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 y4 = ppval (pp4, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 y5 = ppval (pp5, xx);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
84 plot (x, y, ".", xx, [y1; y2; y3; y4; y5]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
85 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
86 ylim ([-2.5 2.5]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
87 legend ({"data", "order 0", "order 1", "order 2", "order 3", "order 4"});
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
88 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 elseif (strcmp (nm, "splinefit3"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 ## Data (100 points)
23633
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
91 rand ("state", 1);
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
92 randn ("state", 1);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 x = 2 * pi * [0, (rand (1, 98)), 1];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 y = sin (x) - cos (2 * x) + 0.2 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 ## No constraints
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 pp1 = splinefit (x, y, 10, "order", 5);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 ## Periodic boundaries
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98 pp2 = splinefit (x, y, 10, "order", 5, "periodic", true);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 xx = linspace (0, 2 * pi, 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 y1 = ppval (pp1, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 y2 = ppval (pp2, xx);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
103 plot (x, y, ".", xx, [y1; y2]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
104 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
105 ylim ([-2 3]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
106 legend ({"data", "no constraints", "periodic"});
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
107 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 elseif (strcmp (nm, "splinefit4"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 ## Data (200 points)
23633
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
110 rand ("state", 1);
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
111 randn ("state", 1);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112 x = 2 * pi * rand (1, 200);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 y = sin (2 * x) + 0.1 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 ## Breaks
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115 breaks = linspace (0, 2 * pi, 10);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 ## Clamped endpoints, y = y" = 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 xc = [0, 0, 2*pi, 2*pi];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
118 cc = [(eye (2)), (eye (2))];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
119 con = struct ("xc", xc, "cc", cc);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120 pp1 = splinefit (x, y, breaks, "constraints", con);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
121 ## Hinged periodic endpoints, y = 0
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
122 con = struct ("xc", 0);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
123 pp2 = splinefit (x, y, breaks, "constraints", con, "periodic", true);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
124 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
125 xx = linspace (0, 2 * pi, 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
126 y1 = ppval (pp1, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
127 y2 = ppval (pp2, xx);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
128 plot (x, y, ".", xx, [y1; y2]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
129 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
130 ylim ([-1.5 1.5]);
20316
d8992a16643c doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents: 20307
diff changeset
131 legend ({"data", "clamped", "hinged periodic"});
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
132 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
133 elseif (strcmp (nm, "splinefit5"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
134 ## Truncated data
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
135 x = [0, 1, 2, 4, 8, 16, 24, 40, 56, 72, 80] / 80;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
136 y = [0, 28, 39, 53, 70, 86, 90, 79, 55, 22, 2] / 1000;
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
137 xy = [x; y];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
138 ## Curve length parameter
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
139 ds = sqrt (diff (x).^2 + diff (y).^2);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
140 s = [0, cumsum(ds)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
141 ## Constraints at s = 0: (x,y) = (0,0), (dx/ds,dy/ds) = (0,1)
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
142 con = struct ("xc", [0 0], "yc", [0 0; 0 1], "cc", eye (2));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
143 ## Fit a spline with 4 pieces
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
144 pp = splinefit (s, xy, 4, "constraints", con);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
145 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
146 ss = linspace (0, s(end), 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
147 xyfit = ppval (pp, ss);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
148 xyb = ppval (pp, pp.breaks);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
149 plot (x, y, ".", xyfit(1,:), xyfit(2,:), "r", xyb(1,:), xyb(2,:), "ro");
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
150 legend ({"data", "spline", "breaks"});
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
151 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
152 ylim ([0 0.1]);
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
153 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
154 elseif (strcmp (nm, "splinefit6"))
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
155 ## Data
23633
271d34c20678 splineimages.m: generate the same plots for documentation each time (bug #51254)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
156 randn ("state", 1);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
157 x = linspace (0, 2*pi, 200);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
158 y = sin (x) + sin (2 * x) + 0.05 * randn (size (x));
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
159 ## Add outliers
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
160 x = [x, linspace(0,2*pi,60)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
161 y = [y, -ones(1,60)];
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
162 ## Fit splines with hinged conditions
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
163 con = struct ("xc", [0, 2*pi]);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
164 pp1 = splinefit (x, y, 8, "constraints", con, "beta", 0.25); ## Robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
165 pp2 = splinefit (x, y, 8, "constraints", con, "beta", 0.75); ## Robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
166 pp3 = splinefit (x, y, 8, "constraints", con); ## No robust fitting
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
167 ## Plot
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
168 xx = linspace (0, 2*pi, 400);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
169 y1 = ppval (pp1, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
170 y2 = ppval (pp2, xx);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
171 y3 = ppval (pp3, xx);
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
172 plot (x, y, ".", xx, [y1; y2; y3]);
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
173 legend ({"data with outliers","robust, beta = 0.25", ...
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
174 "robust, beta = 0.75", "no robust fitting"});
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
175 axis tight;
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
176 ylim ([-2 2]);
20307
c3c1fb44f9b5 eliminate recursive make invocations in doc directory tree
John W. Eaton <jwe@octave.org>
parents: 19717
diff changeset
177 print (outfile, d_typ);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
178 endif
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
179 hide_output ();
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
180 endfunction
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
181
20335
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
182 ## This function no longer sets the graphics toolkit; That is now done
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
183 ## automatically by C++ code which will ordinarily choose 'qt', but might
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
184 ## choose gnuplot on older systems. Only a complete lack of plotting is a
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
185 ## problem.
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
186 function set_graphics_toolkit ()
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
187 if (isempty (available_graphics_toolkits ()))
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
188 error ("no graphics toolkit available for plotting");
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
189 elseif (strcmp ("qt", graphics_toolkit ())
25428
2b3c3c8c8360 Properly test Qt offscreen printing during manual figures generation.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 25054
diff changeset
190 && __have_feature__ ("QT_OFFSCREEN"))
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
191 ## Use qt with QOffscreenSurface for plot
27847
56d94f86a659 Don't test presence of OSMESA which will always fail (bug #57424)
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 26376
diff changeset
192 elseif (! strcmp ("gnuplot", graphics_toolkit ()))
20885
e5f78891ad9e Allow doc's images to be generated using gnuplot when osmesa isn't available.
Ben Abbott <bpabbott@mac.com>
parents: 20335
diff changeset
193 if (! any (strcmp ("gnuplot", available_graphics_toolkits ())))
e5f78891ad9e Allow doc's images to be generated using gnuplot when osmesa isn't available.
Ben Abbott <bpabbott@mac.com>
parents: 20335
diff changeset
194 error ("no graphics toolkit available for offscreen plotting");
e5f78891ad9e Allow doc's images to be generated using gnuplot when osmesa isn't available.
Ben Abbott <bpabbott@mac.com>
parents: 20335
diff changeset
195 else
e5f78891ad9e Allow doc's images to be generated using gnuplot when osmesa isn't available.
Ben Abbott <bpabbott@mac.com>
parents: 20335
diff changeset
196 graphics_toolkit ("gnuplot");
e5f78891ad9e Allow doc's images to be generated using gnuplot when osmesa isn't available.
Ben Abbott <bpabbott@mac.com>
parents: 20335
diff changeset
197 endif
20335
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
198 endif
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
199 endfunction
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
200
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
201 function set_print_size ()
25998
8d2b3e3ff182 print.m: Implement "-opengl", "-painters" and "-RGBImage" options (bug #52866)
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 25428
diff changeset
202 image_size = [5.0, 3.5]; # in inches, 16:9 format
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
203 border = 0; # For postscript use 50/72
24462
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
204 set (groot, "defaultfigurepapertype", "<custom>");
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
205 set (groot, "defaultfigurepaperorientation", "landscape");
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
206 set (groot, "defaultfigurepapersize", image_size + 2*border);
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
207 set (groot, "defaultfigurepaperposition", [border, border, image_size]);
22037
782ecfde4859 doc: Create images for manual with legends that don't extend out of axes box.
Rik <rik@octave.org>
parents: 21580
diff changeset
208 ## FIXME: Required until listener for legend exists (bug #39697)
24462
8ff3c13d14c1 groot.m: New function to return handle of graphics root object.
Rik <rik@octave.org>
parents: 23633
diff changeset
209 set (groot, "defaultfigureposition", [ 72*[border, border, image_size] ]);
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
210 endfunction
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
211
20335
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
212 ## Use this function before plotting commands and after every call to print
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
213 ## since print() resets output to stdout (unfortunately, gnuplot can't pop
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
214 ## output as it can the terminal type).
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
215 function hide_output ()
20335
b439ccc9a162 doc: Improve building of images for the Manual.
Rik <rik@octave.org>
parents: 20316
diff changeset
216 hf = figure (1, "visible", "off");
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
217 endfunction
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
218
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
219 ## generate something for the texinfo @image command to process
20316
d8992a16643c doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents: 20307
diff changeset
220 function image_as_txt (d, nm)
d8992a16643c doc: Clean up m-files which generate images for Manual.
Rik <rik@octave.org>
parents: 20307
diff changeset
221 fid = fopen (fullfile (d, [nm ".txt"]), "wt");
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
222 fputs (fid, "\n");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
223 fputs (fid, "+---------------------------------+\n");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
224 fputs (fid, "| Image unavailable in text mode. |\n");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
225 fputs (fid, "+---------------------------------+\n");
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
226 fclose (fid);
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
227 endfunction
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
228
17504
8b692d9ea54e Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents: 16994
diff changeset
229
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
230 %!demo
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
231 %! for s = 1:6
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21226
diff changeset
232 %! splineimages (sprintf ("splinefit##d", s), "pdf");
14509
a88f8e4fae56 New Function, splinefit.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
233 %! endfor