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 |
6721
|
24 if (strcmp (nm, "interpft")) |
|
25 t = 0 : 0.3 : pi; dt = t(2)-t(1); |
|
26 n = length (t); k = 100; |
|
27 ti = t(1) + [0 : k-1]*dt*n/k; |
|
28 y = sin (4*t + 0.3) .* cos (3*t - 0.1); |
|
29 yp = sin (4*ti + 0.3) .* cos (3*ti - 0.1); |
|
30 plot (ti, yp, 'g', ti, interp1(t, y, ti, 'spline'), 'b', ... |
|
31 ti, interpft (y, k), 'c', t, y, 'r+'); |
|
32 legend ('sin(4t+0.3)cos(3t-0.1','spline','interpft','data'); |
|
33 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
34 elseif (strcmp (nm, "interpn")) |
|
35 x = y = z = -1:1; |
|
36 f = @(x,y,z) x.^2 - y - z.^2; |
|
37 [xx, yy, zz] = meshgrid (x, y, z); |
|
38 v = f (xx,yy,zz); |
|
39 xi = yi = zi = -1:0.1:1; |
|
40 [xxi, yyi, zzi] = ndgrid (xi, yi, zi); |
|
41 vi = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); |
6723
|
42 mesh (zi, yi, squeeze (vi(1,:,:))); |
6721
|
43 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
6743
|
44 elseif (strcmp (nm, "interpderiv1")) |
|
45 t = -2:2; |
|
46 dt = 1; |
|
47 ti =-2:0.025:2; |
|
48 dti = 0.025; |
|
49 y = sign(t); |
|
50 ys = interp1(t,y,ti,'spline'); |
|
51 yp = interp1(t,y,ti,'pchip'); |
|
52 plot (ti, ys,'r-', ti, yp,'g-'); |
|
53 legend('spline','pchip', 4); |
|
54 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
55 elseif (strcmp (nm, "interpderiv2")) |
|
56 t = -2:2; |
|
57 dt = 1; |
|
58 ti =-2:0.025:2; |
|
59 dti = 0.025; |
|
60 y = sign(t); |
6721
|
61 ddys = diff(diff(interp1(t,y,ti,'spline'))./dti)./dti; |
|
62 ddyp = diff(diff(interp1(t,y,ti,'pchip'))./dti)./dti; |
6743
|
63 plot (ti(2:end-1),ddys,'r*', ti(2:end-1),ddyp,'g+'); |
|
64 legend('spline','pchip'); |
6721
|
65 print (strcat (nm, ".", typ), strcat ("-d", typ)) |
|
66 endif |
|
67 bury_output (); |
|
68 endfunction |
|
69 |
|
70 ## Use this function before plotting commands and after every call to |
|
71 ## print since print() resets output to stdout (unfortunately, gnpulot |
|
72 ## can't pop output as it can the terminal type). |
|
73 function bury_output () |
|
74 f = figure (1); |
|
75 set (f, "visible", "off"); |
|
76 endfunction |