4147
|
1 ## Copyright (C) 1996, 1997, 2002 John W. Eaton |
2313
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
245
|
19 |
3368
|
20 ## -*- texinfo -*- |
3737
|
21 ## @deftypefn {Function File} {} contour (@var{z}, @var{n}) |
|
22 ## @deftypefnx {Function File} {} contour (@var{x}, @var{y}, @var{z}, @var{n}) |
3368
|
23 ## Make a contour plot of the three-dimensional surface described by |
|
24 ## @var{z}. Someone needs to improve @code{gnuplot}'s contour routines |
|
25 ## before this will be very useful. |
|
26 ## @end deftypefn |
5053
|
27 ## |
3407
|
28 ## @seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, |
5214
|
29 ## bar, stairs, replot, xlabel, ylabel, and title} |
4
|
30 |
2314
|
31 ## Author: jwe |
|
32 |
3737
|
33 function contour (x, y, z, n) |
4
|
34 |
3063
|
35 ## XXX FIXME XXX -- these plot states should really just be set |
|
36 ## temporarily, probably inside an unwind_protect block, but there is |
|
37 ## no way to determine their current values. |
|
38 |
4
|
39 if (nargin == 1 || nargin == 2) |
3737
|
40 z = x; |
|
41 if (nargin == 1) |
|
42 n = 10; |
|
43 else |
|
44 n = y; |
|
45 endif |
4030
|
46 if (ismatrix (z)) |
5252
|
47 __gnuplot_raw__ ("set nosurface;\n"); |
|
48 __gnuplot_raw__ ("set contour;\n"); |
|
49 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
4030
|
50 if (isscalar (n)) |
5252
|
51 command = sprintf ("set cntrparam levels %d;\n", n); |
4030
|
52 elseif (isvector (n)) |
3426
|
53 tmp = sprintf ("%f", n(1)); |
|
54 for i = 2:length (n) |
|
55 tmp = sprintf ("%s, %f", tmp, n(i)); |
|
56 endfor |
5252
|
57 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
4147
|
58 else |
|
59 error ("contour: levels must be a scalar or vector") ; |
3131
|
60 endif |
5252
|
61 __gnuplot_raw__ (command); |
|
62 __gnuplot_set__ parametric; |
|
63 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
5215
|
64 __gnuplot_splot__ z w l 1; |
4
|
65 else |
4147
|
66 error ("contour: z of contour (z, levels) must be a matrix"); |
4
|
67 endif |
3882
|
68 elseif (nargin == 3 || nargin == 4) |
|
69 if (nargin == 3) |
|
70 n = 10; |
|
71 endif |
4147
|
72 if (ismatrix (z)) |
|
73 size_msg = ["contour: columns(z) must be the same as length(x) and\n" \ |
|
74 "rows(z) must be the same as length(y),\n" \ |
|
75 "or x, y, and z must be matrices with the same size"]; |
|
76 if (isvector (x) && isvector (y)) |
|
77 xlen = length (x); |
|
78 ylen = length (y); |
|
79 if (xlen == columns (z) && ylen == rows (z)) |
|
80 if (rows (x) == 1) |
|
81 x = x'; |
|
82 endif |
|
83 len = 3 * ylen; |
|
84 zz = zeros (xlen, len); |
|
85 k = 1; |
|
86 for i = 1:3:len |
|
87 zz(:,i) = x; |
|
88 zz(:,i+1) = y(k) * ones (xlen, 1); |
|
89 zz(:,i+2) = z(k,:)'; |
|
90 k++; |
|
91 endfor |
|
92 else |
|
93 error (size_msg); |
|
94 endif |
|
95 else |
|
96 z_size = size (z); |
|
97 if (z_size == size (x) && z_size == size (y)) |
|
98 nc = 3*z_size(1); |
|
99 zz = zeros (z_size(2), nc); |
|
100 zz(:,1:3:nc) = x'; |
|
101 zz(:,2:3:nc) = y'; |
|
102 zz(:,3:3:nc) = z'; |
|
103 else |
|
104 error (size_msg); |
|
105 endif |
|
106 endif |
5252
|
107 __gnuplot_raw__ ("set nosurface;\n"); |
|
108 __gnuplot_raw__ ("set contour;\n"); |
|
109 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
4147
|
110 if (isscalar (n)) |
5252
|
111 command = sprintf ("set cntrparam levels %d;\n", n); |
4147
|
112 elseif (isvector (n)) |
|
113 tmp = sprintf ("%f", n(1)); |
|
114 for i = 2:length (n) |
|
115 tmp = sprintf ("%s, %f", tmp, n(i)); |
4
|
116 endfor |
5252
|
117 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
4
|
118 else |
4147
|
119 error ("contour: levels must be a scalar or vector") ; |
4
|
120 endif |
5252
|
121 __gnuplot_raw__ (command); |
5215
|
122 __gnuplot_set__ parametric; |
5252
|
123 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
5215
|
124 __gnuplot_splot__ zz w l 1; |
4
|
125 else |
3063
|
126 error ("contour: x and y must be vectors and z must be a matrix"); |
2325
|
127 endif |
4
|
128 else |
4147
|
129 usage ("contour (x, y, z, levels) or contour (z, levels)"); |
4
|
130 endif |
|
131 |
|
132 endfunction |