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. |
5642
|
26 ## @seealso{plot, mesh, meshgrid} |
3368
|
27 ## @end deftypefn |
4
|
28 |
2314
|
29 ## Author: jwe |
|
30 |
3737
|
31 function contour (x, y, z, n) |
4
|
32 |
5775
|
33 ## FIXME -- these plot states should really just be set |
3063
|
34 ## temporarily, probably inside an unwind_protect block, but there is |
|
35 ## no way to determine their current values. |
|
36 |
4
|
37 if (nargin == 1 || nargin == 2) |
3737
|
38 z = x; |
|
39 if (nargin == 1) |
|
40 n = 10; |
|
41 else |
|
42 n = y; |
|
43 endif |
4030
|
44 if (ismatrix (z)) |
5252
|
45 __gnuplot_raw__ ("set nosurface;\n"); |
|
46 __gnuplot_raw__ ("set contour;\n"); |
|
47 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
4030
|
48 if (isscalar (n)) |
5252
|
49 command = sprintf ("set cntrparam levels %d;\n", n); |
4030
|
50 elseif (isvector (n)) |
3426
|
51 tmp = sprintf ("%f", n(1)); |
|
52 for i = 2:length (n) |
|
53 tmp = sprintf ("%s, %f", tmp, n(i)); |
|
54 endfor |
5252
|
55 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
4147
|
56 else |
|
57 error ("contour: levels must be a scalar or vector") ; |
3131
|
58 endif |
5252
|
59 __gnuplot_raw__ (command); |
|
60 __gnuplot_set__ parametric; |
|
61 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
5215
|
62 __gnuplot_splot__ z w l 1; |
4
|
63 else |
4147
|
64 error ("contour: z of contour (z, levels) must be a matrix"); |
4
|
65 endif |
3882
|
66 elseif (nargin == 3 || nargin == 4) |
|
67 if (nargin == 3) |
|
68 n = 10; |
|
69 endif |
4147
|
70 if (ismatrix (z)) |
|
71 size_msg = ["contour: columns(z) must be the same as length(x) and\n" \ |
|
72 "rows(z) must be the same as length(y),\n" \ |
|
73 "or x, y, and z must be matrices with the same size"]; |
|
74 if (isvector (x) && isvector (y)) |
|
75 xlen = length (x); |
|
76 ylen = length (y); |
|
77 if (xlen == columns (z) && ylen == rows (z)) |
|
78 if (rows (x) == 1) |
|
79 x = x'; |
|
80 endif |
|
81 len = 3 * ylen; |
|
82 zz = zeros (xlen, len); |
|
83 k = 1; |
|
84 for i = 1:3:len |
|
85 zz(:,i) = x; |
|
86 zz(:,i+1) = y(k) * ones (xlen, 1); |
|
87 zz(:,i+2) = z(k,:)'; |
|
88 k++; |
|
89 endfor |
|
90 else |
|
91 error (size_msg); |
|
92 endif |
|
93 else |
|
94 z_size = size (z); |
|
95 if (z_size == size (x) && z_size == size (y)) |
|
96 nc = 3*z_size(1); |
|
97 zz = zeros (z_size(2), nc); |
|
98 zz(:,1:3:nc) = x'; |
|
99 zz(:,2:3:nc) = y'; |
|
100 zz(:,3:3:nc) = z'; |
|
101 else |
|
102 error (size_msg); |
|
103 endif |
|
104 endif |
5252
|
105 __gnuplot_raw__ ("set nosurface;\n"); |
|
106 __gnuplot_raw__ ("set contour;\n"); |
|
107 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
4147
|
108 if (isscalar (n)) |
5252
|
109 command = sprintf ("set cntrparam levels %d;\n", n); |
4147
|
110 elseif (isvector (n)) |
|
111 tmp = sprintf ("%f", n(1)); |
|
112 for i = 2:length (n) |
|
113 tmp = sprintf ("%s, %f", tmp, n(i)); |
4
|
114 endfor |
5252
|
115 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
4
|
116 else |
4147
|
117 error ("contour: levels must be a scalar or vector") ; |
4
|
118 endif |
5252
|
119 __gnuplot_raw__ (command); |
5215
|
120 __gnuplot_set__ parametric; |
5252
|
121 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
5215
|
122 __gnuplot_splot__ zz w l 1; |
4
|
123 else |
3063
|
124 error ("contour: x and y must be vectors and z must be a matrix"); |
2325
|
125 endif |
4
|
126 else |
4147
|
127 usage ("contour (x, y, z, levels) or contour (z, levels)"); |
4
|
128 endif |
|
129 |
|
130 endfunction |