I have a surface which is an ellipsoid, and I would like to display the cross-section of the ellipsoid at specific planes.

 채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 7월 20일

0 개 추천

The SLICE command requires a volume of data defined on a uniformly spaced grid. Since a surface is defined by a set of vertices (usually on a nonuniformly spaced grid), use the GRIDDATA3 function to fit the surface to a uniformly spaced grid. You can then use the SLICE function to plot a slice through this volume, as illustrated by the following example:
[x, y, z] = ellipsoid(0,0,0,10,10,10,20);
ti = -10:.25:10;
[XI,YI, ZI] = meshgrid(ti,ti, ti);
v = ones(size(x));
V = griddata3(x,y,z,v,XI,YI, ZI);
figure;
h = surf(x, y, z); hold on
set(h, 'FaceAlpha', 0.2,'EdgeAlpha', 0.2)
h1 = slice(XI, YI, ZI, V, [], [9 0 -9], []);
for i = 1:numel(h1)
hi = h1(i);
cdata = get(hi, 'CData');
cdata(~isnan(cdata))=1;
set(hi, 'CData', cdata, 'EdgeColor', 'none')
end
The method described above works for a general surface, but the time required to fit the surface to a fine grid is large. Alternatively, if the surface has a mathematical definition, as in the case of the ellipsoid, you can calculate the volume directly, as in the following example:
xc = 0;
yc = 0;
zc = 0;
xr = 10;
yr = 10;
zr = 10;
[x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr,20);
ti = -10:.25:10;
[XI,YI, ZI] = meshgrid(ti,ti, ti);
V = (XI - xc).^2/xr^2+(YI - yc).^2/yr^2+(ZI - zc).^2/zr^2;
V(V>1)=NaN;
figure;
h = surf(x, y, z); hold on
set(h, 'FaceAlpha', 0.2,'EdgeAlpha', 0.2)
h1 = slice(XI, YI, ZI, V, [], [9 0 -9], []);
for i = 1:numel(h1)
hi = h1(i);
cdata = get(hi, 'CData');
cdata(~isnan(cdata))=1;
set(hi, 'CData', cdata, 'EdgeColor', 'none')
end
This code displays three slices through an ellipsoid.

댓글 수: 1

Jonathan Kwang
Jonathan Kwang 2016년 4월 25일
편집: Jonathan Kwang 2016년 4월 25일
You can call figure() before plot to have each plot in 3 separate figures. Example:
figure()
plot(x,y)
figure()
plot(y,z)
figure()
plot(x,z)
Or you can have 3 separate plots in 1 figure. Example:
subplot(2,2,1);
plot(x,y);
title('x vs y');
subplot(2,2,2);
plot(y,z);
title('y vs z');
subplot(2,2,3);
plot(x,z);
title('x vs z');

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

추가 답변 (0개)

카테고리

제품

릴리스

R2009a

Community Treasure Hunt

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

Start Hunting!

Translated by