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)) |
6004
|
45 unwind_protect |
|
46 __gnuplot_raw__ ("set nosurface;\n"); |
|
47 __gnuplot_raw__ ("set contour;\n"); |
|
48 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
|
49 if (isscalar (n)) |
|
50 command = sprintf ("set cntrparam levels %d;\n", n); |
|
51 elseif (isvector (n)) |
|
52 tmp = sprintf ("%f", n(1)); |
|
53 for i = 2:length (n) |
|
54 tmp = sprintf ("%s, %f", tmp, n(i)); |
|
55 endfor |
|
56 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
|
57 else |
|
58 error ("contour: levels must be a scalar or vector") ; |
|
59 endif |
|
60 __gnuplot_raw__ (command); |
|
61 __gnuplot_set__ parametric; |
|
62 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
|
63 __plt3__ (z, "", "", [gnuplot_command_with, " l 1"]); |
|
64 unwind_protect_cleanup |
|
65 __gnuplot_set__ noparametric; |
|
66 end_unwind_protect |
|
67 |
4
|
68 else |
4147
|
69 error ("contour: z of contour (z, levels) must be a matrix"); |
4
|
70 endif |
3882
|
71 elseif (nargin == 3 || nargin == 4) |
|
72 if (nargin == 3) |
|
73 n = 10; |
|
74 endif |
4147
|
75 if (ismatrix (z)) |
|
76 size_msg = ["contour: columns(z) must be the same as length(x) and\n" \ |
|
77 "rows(z) must be the same as length(y),\n" \ |
|
78 "or x, y, and z must be matrices with the same size"]; |
|
79 if (isvector (x) && isvector (y)) |
|
80 xlen = length (x); |
|
81 ylen = length (y); |
|
82 if (xlen == columns (z) && ylen == rows (z)) |
|
83 if (rows (x) == 1) |
|
84 x = x'; |
|
85 endif |
|
86 len = 3 * ylen; |
|
87 zz = zeros (xlen, len); |
|
88 k = 1; |
|
89 for i = 1:3:len |
|
90 zz(:,i) = x; |
|
91 zz(:,i+1) = y(k) * ones (xlen, 1); |
|
92 zz(:,i+2) = z(k,:)'; |
|
93 k++; |
|
94 endfor |
|
95 else |
|
96 error (size_msg); |
|
97 endif |
|
98 else |
|
99 z_size = size (z); |
|
100 if (z_size == size (x) && z_size == size (y)) |
|
101 nc = 3*z_size(1); |
|
102 zz = zeros (z_size(2), nc); |
|
103 zz(:,1:3:nc) = x'; |
|
104 zz(:,2:3:nc) = y'; |
|
105 zz(:,3:3:nc) = z'; |
|
106 else |
|
107 error (size_msg); |
|
108 endif |
|
109 endif |
6004
|
110 unwind_protect |
|
111 __gnuplot_raw__ ("set nosurface;\n"); |
|
112 __gnuplot_raw__ ("set contour;\n"); |
|
113 __gnuplot_raw__ ("set cntrparam bspline;\n"); |
|
114 if (isscalar (n)) |
|
115 command = sprintf ("set cntrparam levels %d;\n", n); |
|
116 elseif (isvector (n)) |
|
117 tmp = sprintf ("%f", n(1)); |
|
118 for i = 2:length (n) |
|
119 tmp = sprintf ("%s, %f", tmp, n(i)); |
|
120 endfor |
|
121 command = sprintf ("set cntrparam levels discrete %s;\n", tmp); |
|
122 else |
|
123 error ("contour: levels must be a scalar or vector") ; |
|
124 endif |
|
125 __gnuplot_raw__ (command); |
|
126 __gnuplot_set__ parametric; |
|
127 __gnuplot_raw__ ("set view 0, 0, 1, 1;\n"); |
|
128 __plt3__ (zz, "", "", [gnuplot_command_with, " l 1"]); |
|
129 unwind_protect_cleanup |
|
130 __gnuplot_set__ noparametric; |
|
131 end_unwind_protect |
4
|
132 else |
3063
|
133 error ("contour: x and y must be vectors and z must be a matrix"); |
2325
|
134 endif |
4
|
135 else |
4147
|
136 usage ("contour (x, y, z, levels) or contour (z, levels)"); |
4
|
137 endif |
|
138 |
|
139 endfunction |