Mostrar comentarios más antiguos
Hi all,
I'm struggling to figure out how to create a few 3D shapes, such as a 3D pyramid.
Basically what I want is say for example, in a 25x35 domain, at every point a value between 0 and 1 is produced. And when the 25x35 matrix is plotted using surf for example, a rectangular based pyramid will be plotted.
The following is an example but of a 3D gaussian, and I can't figure out how to do any other shape:
x = linspace(-4,4,25); y = linspace(-4,4,35); [X,Y] = meshgrid(x,y); Z = exp(-X.^2-Y.^2); figure; surf(X,Y,Z);
Thanks very much in advance! Tim
Respuestas (2)
Jarrod Rivituso
el 8 de Abr. de 2011
Looks like you are on the right track and you just need to get the correct equation for the height of the pyramid.
One thing I've found helpful in similar situations is to sometimes define the size of the grid larger than needed and then use NaN to indicate points that I don't want to be drawn. This helps eliminate the need to conform to a square boundary.
Here's a pyramid example
>> x = linspace(-8,8,200);
>> y = linspace(-8,8,200);
>> [X,Y] = meshgrid(x,y);
>> Z = (4-abs(X)) + (4-abs(Y));
>> Z(Z < 0) = NaN;
>> surf(X,Y,Z)
Hope this helps!
2 comentarios
Ayush singhal
el 13 de Abr. de 2021
and how to generate sphere, cube,cylinder?
ALOK
el 14 de Sept. de 2026 a las 17:29
For sphere:-
% Define parameters
r = 5; % Radius
x0 = 10; % Center X
y0 = -2; % Center Y
z0 = 3; % Center Z
% Generate unit sphere coordinates (n=50 increases mesh smooth resolution)
[X, Y, Z] = sphere(50);
% Scale and translate
X_custom = X * r + x0;
Y_custom = Y * r + y0;
Z_custom = Z * r + z0;
% Plot
figure;
surf(X_custom, Y_custom, Z_custom);
axis equal;
grid on;
Nausheen
el 7 de Jul. de 2013
1 voto
We can think of pyramid as a cone. To draw this cone we give it only 4 sides, so it would draw a pyramid. cylinder([1 0],4)
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!