plot an ellipsoid with matlab
58 views (last 30 days)
Show older comments
Hello,
I have an equation of an ellipsoid x^2+y^2+z^2-1=0 and I need to plot without using the ellipsoid function of matlab. thanks
1 Comment
Roger Stafford
on 11 Sep 2013
I'm sorry. I erased my original response and your first comment by mistake. They were:
My original response:
- - - - - -
[theta,phi] = ndgrid(linspace(0,pi),linspace(0,2*pi));
x = sin(theta).*cos(phi);
y = sin(theta).*sin(phi);
z = cos(theta);
surf(x,y,z)
Note: Your particular ellipsoid is called a 'sphere'.
- - - - -
Your first comment:
could you, please, explain me again. I did not understand your response.
Suppose that I have another ellipsoid not canonic like x^2/3+Y^2/4+z^2/3-2=0. how I can plot it
Answers (2)
Roger Stafford
on 11 Sep 2013
Your second ellipsoid's equation can be rewritten in the form:
x^2/a^2 + y^2/b^2 + z^2/c^2 = 1
where a, b, and c are defined as below:
a = sqrt(6);
b = sqrt(8);
c = sqrt(6);
[theta,phi] = ndgrid(linspace(0,pi),linspace(0,2*pi));
x = a*sin(theta).*cos(phi);
y = b*sin(theta).*sin(phi);
z = c*cos(theta);
surf(x,y,z)
3 Comments
Roger Stafford
on 12 Sep 2013
To begin with, suppose we have a sphere of radius r centered at the origin
x^2/r^2 + y^2/r^2 + z^2/r^2 = 1.
The quantities r, theta and phi are spherical coordinates that satisfy the three equations
x = r*sin(theta)*cos(phi)
y = r*sin(theta)*sin(phi)
z = r*cos(theta)
as you can verify by looking up references on spherical coordinates. Now imagine stretching (or contracting) the sphere by a ratio of a/r in the x-direction, by b/r in the y direction, and by c/r in the z direction. Then you would have the three equations
x = a*sin(theta)*cos(phi)
y = b*sin(theta)*sin(phi)
z = c*cos(theta)
which satisfy the equation
x^2/a^2 + y^2/b^2 + z^2/c^2 = 1
and the sphere has become an ellipsoid. The theta and phi parameters are no longer spherical coordinates but they serve very nicely as the needed two parameters for representing the surface of the ellipsoid for plotting by 'surf'. The two families of curves you see in the surface plot represent contours of constant theta and constant phi, respectively, in accordance with their values in the grid from 'ndgrid'.
Roger Stafford
on 11 Sep 2013
It depends on whether R or T is to be performed first. I will assume you want to first rotate by multiplying by the 3 x 3 matrix R from the left, and then translate the center by adding the 3 x 1 column vector T.
% Generate x, y, and z as before
P = bsxfun(@plus,R*[x(:),y(:),z(:)]',T); % Rotate, then translate
X = reshape(P(1,:),size(x,1),size(x,2));
Y = reshape(P(2,:),size(y,1),size(y,2));
Z = reshape(P(3,:),size(z,1),size(z,2));
surf(X,Y,Z)
0 Comments
See Also
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!