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