7018
|
1 ## Copyright (C) 2007 David Bateman |
|
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 |
6721
|
19 function interpimages (nm, typ) |
|
20 bury_output (); |
7257
|
21 if (strcmp (typ, "png")) |
|
22 set (0, "defaulttextfontname", "*"); |
|
23 endif |
7262
|
24 if (strcmp (typ, "txt")) |
|
25 image_as_txt (nm); |
|
26 elseif (strcmp (nm, "interpft")) |
6721
|
27 t = 0 : 0.3 : pi; dt = t(2)-t(1); |
|
28 n = length (t); k = 100; |
|
29 ti = t(1) + [0 : k-1]*dt*n/k; |
|
30 y = sin (4*t + 0.3) .* cos (3*t - 0.1); |
|
31 yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); |
|
32 plot (ti, yp, 'g', ti, interp1(t, y, ti, 'spline'), 'b', ... |
|
33 ti, interpft (y, k), 'c', t, y, 'r+'); |
|
34 legend ('sin(4t+0.3)cos(3t-0.1','spline','interpft','data'); |
|
35 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
36 elseif (strcmp (nm, "interpn")) |
|
37 x = y = z = -1:1; |
|
38 f = @(x,y,z) x.^2 - y - z.^2; |
|
39 [xx, yy, zz] = meshgrid (x, y, z); |
|
40 v = f (xx,yy,zz); |
|
41 xi = yi = zi = -1:0.1:1; |
|
42 [xxi, yyi, zzi] = ndgrid (xi, yi, zi); |
|
43 vi = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); |
6723
|
44 mesh (zi, yi, squeeze (vi(1,:,:))); |
6721
|
45 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6743
|
46 elseif (strcmp (nm, "interpderiv1")) |
|
47 t = -2:2; |
|
48 dt = 1; |
|
49 ti =-2:0.025:2; |
|
50 dti = 0.025; |
|
51 y = sign(t); |
|
52 ys = interp1(t,y,ti,'spline'); |
|
53 yp = interp1(t,y,ti,'pchip'); |
|
54 plot (ti, ys,'r-', ti, yp,'g-'); |
|
55 legend('spline','pchip', 4); |
|
56 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
57 elseif (strcmp (nm, "interpderiv2")) |
|
58 t = -2:2; |
|
59 dt = 1; |
|
60 ti =-2:0.025:2; |
|
61 dti = 0.025; |
|
62 y = sign(t); |
6721
|
63 ddys = diff(diff(interp1(t,y,ti,'spline'))./dti)./dti; |
|
64 ddyp = diff(diff(interp1(t,y,ti,'pchip'))./dti)./dti; |
6743
|
65 plot (ti(2:end-1),ddys,'r*', ti(2:end-1),ddyp,'g+'); |
|
66 legend('spline','pchip'); |
6721
|
67 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
68 endif |
|
69 bury_output (); |
|
70 endfunction |
|
71 |
|
72 ## Use this function before plotting commands and after every call to |
|
73 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
74 ## can't pop output as it can the terminal type). |
|
75 function bury_output () |
|
76 f = figure (1); |
|
77 set (f, "visible", "off"); |
|
78 endfunction |
7262
|
79 |
|
80 ## generate something for the texinfo @image command to process |
|
81 function image_as_txt(nm) |
|
82 fid = fopen (sprintf ("%s.txt", nm), "wt"); |
|
83 fputs (fid, "\n"); |
|
84 fputs (fid, "+---------------------------------+\n"); |
|
85 fputs (fid, "| Image unavailable in text mode. |\n"); |
|
86 fputs (fid, "+---------------------------------+\n"); |
|
87 fclose (fid); |
|
88 endfunction |