Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/ellipse.m

raw
1 function [x,y,z] = ellipse(sigma,mu,k)
2 %ELLIPSE Creates an ellipsoid
3 %[X,Y,Z] = ELLIPSE(SIGMA,MU,K) Creates an arbitrary
4 %ellipsoid in 2 or 3 dimensions. The ellipsoid represents
5 %the solutions to the quadratic equation:
6 %
7 % (x-MU)'*SIGMA*(x-MU) = K
8 %
9 %SIGMA is a positive definate matrix of size 2x2 or 3x3.
10 %SIGMA determines the shape of the ellipsoid.
11 %MU is a vector conformable with SIGMA; either 2x1 or 3x1.
12 %MU determines the location of the ellipsoid.
13 %K is a real constant.
14 %K determines the size of the ellipsoid.
15 %The vector x=[X;Y] in 2D or x=[X,Y,Z] in 3D.
16 %
17 %If no output aruments are specified, the resulting
18 %ellipsoid is plotted. One can create these plots manually
19 %using PLOT(X,Y) in the 2 dimensional case and
20 %using MESH(X,Y,Z) in the 3 dimensional case.
21
22 % QPLOT was written by Clark A. Burdick of the research
23 % department of the Federal Reserve Bank of Atlanta.
24 % Original: August 19, 1997
25 % Last Modified: August 19, 1997
26 %
27 % Copyright (C) 1997-2012 Clark A. Burdick
28 %
29 % This free software: you can redistribute it and/or modify
30 % it under the terms of the GNU General Public License as published by
31 % the Free Software Foundation, either version 3 of the License, or
32 % (at your option) any later version.
33 %
34 % It is distributed in the hope that it will be useful,
35 % but WITHOUT ANY WARRANTY; without even the implied warranty of
36 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 % GNU General Public License for more details.
38 %
39 % If you did not received a copy of the GNU General Public License
40 % with this software, see <http://www.gnu.org/licenses/>.
41 %
42
43 % TO BE DONE:
44 % I'm open to suggestions.
45
46 n=50; % <<>>
47 theta=pi*(-n:2:n)/n;
48 phi=(pi/2)*(-n:2:n)'/n;
49
50 R=chol(sigma);
51 rho=sqrt(k);
52
53 if length(mu) == 2
54 X = rho*cos(theta);
55 Y = rho*sin(theta);
56
57 for i = 1:length(theta)
58 circlevec = [X(i);Y(i)];
59 ellipsevec = R\circlevec; % T.A. Zha, 8/16/98
60 x(i) = mu(1) + ellipsevec(1);
61 y(i) = mu(2) + ellipsevec(2);
62 end
63 if nargout == 0
64 plot(x,y)
65 end
66 z='This was only a 2 dimensional ellipse';
67 end
68
69 if length(mu) == 3
70 X = rho*cos(phi)*cos(theta);
71 Y = rho*cos(phi)*sin(theta);
72 Z = rho*sin(phi)*ones(size(theta));
73
74 for i = 1:length(phi)
75 for j = 1:length(theta)
76 spherevec = [X(i,j); Y(i,j); Z(i,j)];
77 ellipsevec = R\spherevec; % T.A. Zha, 8/16/98
78 x(i,j) = mu(1) + ellipsevec(1);
79 y(i,j) = mu(2) + ellipsevec(2);
80 z(i,j) = mu(3) + ellipsevec(3);
81 end
82 end
83 if nargout == 0
84 mesh(x,y,z)
85 end
86 end