plot an ellipsoid with matlab

조회 수: 14 (최근 30일)
ProgramMatlab
ProgramMatlab 2013년 9월 11일
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
Roger Stafford
Roger Stafford 2013년 9월 11일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Roger Stafford
Roger Stafford 2013년 9월 11일
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
ProgramMatlab
ProgramMatlab 2013년 9월 11일
Hello, Could you give me the theoretical that let us write
x = a*sin(theta).*cos(phi); y = b*sin(theta).*sin(phi); z = c*cos(theta); thanks!
Roger Stafford
Roger Stafford 2013년 9월 12일
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
Roger Stafford 2013년 9월 11일
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)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by