Mercurial > octave-nkf
annotate doc/interpreter/splineimages.m @ 19742:1170c849952b
don't force gnuplot for creating figures for documentation
* geometryimages.m, interpimages.m, plotimages.m, sparseimages.m,
splineimages.m: Don't set graphics_toolkit to "gnuplot".
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 13 Feb 2015 14:02:06 -0500 |
parents | 4197fc428c7d |
children | 606b05352351 |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19627
diff
changeset
|
1 ## Copyright (C) 2012-2015 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 function splineimages (nm, typ) | |
20 set_print_size (); | |
21 hide_output (); | |
22 if (strcmp (typ, "png")) | |
23 set (0, "defaulttextfontname", "*"); | |
24 endif | |
25 if (strcmp (typ, "eps")) | |
26 d_typ = "-depsc2"; | |
27 else | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
28 d_typ = ["-d" typ]; |
14509 | 29 endif |
30 | |
31 if (strcmp (typ, "txt")) | |
32 image_as_txt (nm); | |
33 elseif (strcmp (nm, "splinefit1")) ## Breaks and Pieces | |
34 x = 2 * pi * rand (1, 200); | |
35 y = sin (x) + sin (2 * x) + 0.2 * randn (size (x)); | |
36 ## Uniform breaks | |
37 breaks = linspace (0, 2 * pi, 41); ## 41 breaks, 40 pieces | |
38 pp1 = splinefit (x, y, breaks); | |
39 ## Breaks interpolated from data | |
40 pp2 = splinefit (x, y, 10); ## 11 breaks, 10 pieces | |
41 ## Plot | |
42 xx = linspace (0, 2 * pi, 400); | |
43 y1 = ppval (pp1, xx); | |
44 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
|
45 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
|
46 axis tight; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
47 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
|
48 legend ("data", "41 breaks, 40 pieces", "11 breaks, 10 pieces"); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
49 print ([nm "." typ], d_typ); |
14509 | 50 elseif (strcmp (nm, "splinefit2")) ## Spline orders |
51 ## Data (200 points) | |
52 x = 2 * pi * rand (1, 200); | |
53 y = sin (x) + sin (2 * x) + 0.1 * randn (size (x)); | |
54 ## Splines | |
55 pp1 = splinefit (x, y, 8, "order", 0); ## Piecewise constant | |
56 pp2 = splinefit (x, y, 8, "order", 1); ## Piecewise linear | |
57 pp3 = splinefit (x, y, 8, "order", 2); ## Piecewise quadratic | |
58 pp4 = splinefit (x, y, 8, "order", 3); ## Piecewise cubic | |
59 pp5 = splinefit (x, y, 8, "order", 4); ## Etc. | |
60 ## Plot | |
61 xx = linspace (0, 2 * pi, 400); | |
62 y1 = ppval (pp1, xx); | |
63 y2 = ppval (pp2, xx); | |
64 y3 = ppval (pp3, xx); | |
65 y4 = ppval (pp4, xx); | |
66 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
|
67 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
|
68 axis tight; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
69 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
|
70 legend ({"data", "order 0", "order 1", "order 2", "order 3", "order 4"}); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
71 print ([nm, "." typ], d_typ); |
14509 | 72 elseif (strcmp (nm, "splinefit3")) |
73 ## Data (100 points) | |
74 x = 2 * pi * [0, (rand (1, 98)), 1]; | |
75 y = sin (x) - cos (2 * x) + 0.2 * randn (size (x)); | |
76 ## No constraints | |
77 pp1 = splinefit (x, y, 10, "order", 5); | |
78 ## Periodic boundaries | |
79 pp2 = splinefit (x, y, 10, "order", 5, "periodic", true); | |
80 ## Plot | |
81 xx = linspace (0, 2 * pi, 400); | |
82 y1 = ppval (pp1, xx); | |
83 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
|
84 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
|
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 3]); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
87 legend ({"data", "no constraints", "periodic"}); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
88 print ([nm "." typ], d_typ); |
14509 | 89 elseif (strcmp (nm, "splinefit4")) |
90 ## Data (200 points) | |
91 x = 2 * pi * rand (1, 200); | |
92 y = sin (2 * x) + 0.1 * randn (size (x)); | |
93 ## Breaks | |
94 breaks = linspace (0, 2 * pi, 10); | |
95 ## Clamped endpoints, y = y" = 0 | |
96 xc = [0, 0, 2*pi, 2*pi]; | |
97 cc = [(eye (2)), (eye (2))]; | |
98 con = struct ("xc", xc, "cc", cc); | |
99 pp1 = splinefit (x, y, breaks, "constraints", con); | |
100 ## Hinged periodic endpoints, y = 0 | |
101 con = struct ("xc", 0); | |
102 pp2 = splinefit (x, y, breaks, "constraints", con, "periodic", true); | |
103 ## Plot | |
104 xx = linspace (0, 2 * pi, 400); | |
105 y1 = ppval (pp1, xx); | |
106 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
|
107 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
|
108 axis tight; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
109 ylim ([-1.5 1.5]); |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
110 legend({"data", "clamped", "hinged periodic"}); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
111 print ([nm "." typ], d_typ); |
14509 | 112 elseif (strcmp (nm, "splinefit5")) |
113 ## Truncated data | |
114 x = [0, 1, 2, 4, 8, 16, 24, 40, 56, 72, 80] / 80; | |
115 y = [0, 28, 39, 53, 70, 86, 90, 79, 55, 22, 2] / 1000; | |
116 xy = [x; y]; | |
117 ## Curve length parameter | |
118 ds = sqrt (diff (x).^2 + diff (y).^2); | |
119 s = [0, cumsum(ds)]; | |
120 ## Constraints at s = 0: (x,y) = (0,0), (dx/ds,dy/ds) = (0,1) | |
121 con = struct ("xc", [0 0], "yc", [0 0; 0 1], "cc", eye (2)); | |
122 ## Fit a spline with 4 pieces | |
123 pp = splinefit (s, xy, 4, "constraints", con); | |
124 ## Plot | |
125 ss = linspace (0, s(end), 400); | |
126 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
|
127 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
|
128 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
|
129 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
|
130 axis tight; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
131 ylim ([0 0.1]); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
132 print ([nm "." typ], d_typ); |
14509 | 133 elseif (strcmp (nm, "splinefit6")) |
134 ## Data | |
135 x = linspace (0, 2*pi, 200); | |
136 y = sin (x) + sin (2 * x) + 0.05 * randn (size (x)); | |
137 ## Add outliers | |
138 x = [x, linspace(0,2*pi,60)]; | |
139 y = [y, -ones(1,60)]; | |
140 ## Fit splines with hinged conditions | |
141 con = struct ("xc", [0, 2*pi]); | |
142 pp1 = splinefit (x, y, 8, "constraints", con, "beta", 0.25); ## Robust fitting | |
143 pp2 = splinefit (x, y, 8, "constraints", con, "beta", 0.75); ## Robust fitting | |
144 pp3 = splinefit (x, y, 8, "constraints", con); ## No robust fitting | |
145 ## Plot | |
146 xx = linspace (0, 2*pi, 400); | |
147 y1 = ppval (pp1, xx); | |
148 y2 = ppval (pp2, xx); | |
149 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
|
150 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
|
151 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
|
152 "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
|
153 axis tight; |
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
154 ylim ([-2 2]); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14526
diff
changeset
|
155 print ([nm "." typ], d_typ); |
14509 | 156 endif |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
157 hide_output (); |
14509 | 158 endfunction |
159 | |
160 function set_print_size () | |
161 image_size = [5.0, 3.5]; # in inches, 16:9 format | |
162 border = 0; # For postscript use 50/72 | |
163 set (0, "defaultfigurepapertype", "<custom>"); | |
164 set (0, "defaultfigurepaperorientation", "landscape"); | |
165 set (0, "defaultfigurepapersize", image_size + 2*border); | |
166 set (0, "defaultfigurepaperposition", [border, border, image_size]); | |
167 endfunction | |
168 | |
169 ## Use this function before plotting commands and after every call to | |
170 ## print since print() resets output to stdout (unfortunately, gnpulot | |
171 ## can't pop output as it can the terminal type). | |
172 function hide_output () | |
173 f = figure (1); | |
174 set (f, "visible", "off"); | |
175 endfunction | |
176 | |
177 ## generate something for the texinfo @image command to process | |
178 function image_as_txt(nm) | |
14526
e12945668746
doc: Stop constant rebuild of spline images in txt format.
Rik <octave@nomad.inbox5.com>
parents:
14509
diff
changeset
|
179 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
14509 | 180 fputs (fid, "\n"); |
181 fputs (fid, "+---------------------------------+\n"); | |
182 fputs (fid, "| Image unavailable in text mode. |\n"); | |
183 fputs (fid, "+---------------------------------+\n"); | |
184 fclose (fid); | |
185 endfunction | |
186 | |
17504
8b692d9ea54e
Use Octave coding conventions for m-files which generate images for manual.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
187 |
14509 | 188 %!demo |
189 %! for s = 1:6 | |
190 %! splineimages (sprintf ("splinefit##d", s), "pdf") | |
191 %! endfor | |
192 |