I would like to visualize the 3D volume under a surface generated by a 2-variable function. So far I can generate the surface but I don't know how to actually visualize the volume.
funCube = @(x,y)2.6207.*(sin(x)+cos(x)).*cos(y);
funCylinder = @(x, y) 3.078677852.*cos(y);
cubePlot = ezsurf(funCube, [0, 0.26, 0, 0.26], 120);
hold on;
cylinderPlot = ezsurf(funCylinder, [0, 0.26, 0, 0.26], 120);
This is a solution using filled polygons (patch
objects). The idea is that in addition to the surface we create 5 polygons to form "4 walls and a floor" while the surface itself acts as a "ceiling".
The result:
I'd say it gives the impression of volume quite well.
function q47361071
%% Definitions:
% Define a surface equation: z = f(x,y)
funCube = @(x,y)2.6207.*(sin(x)+cos(x)).*cos(y);
% Evaluate the surface equation at a grid of points:
X = 0:0.01:0.26; Y = X;
[YY,XX] = meshgrid(X,Y);
ZZ = funCube(XX,YY);
%% Visualization:
figure(); surf(XX,YY,ZZ); hAx = gca; hold(hAx,'on'); view([-50 35]);
draw5Poly(hAx,XX,YY,ZZ);
end
function draw5Poly(hAx,XX,YY,ZZ)
P = {[XX(1,1), YY(1,1), 0; [XX(:,1) YY(:,1) ZZ(:,1) ]; XX(end,1),YY(end,1), 0],...
[XX(1,end), YY(1,end),0; [XX(:,end) YY(:,end) ZZ(:,end) ]; XX(end,1),YY(end,end), 0],...
[XX(1,1), YY(1,1), 0; [XX(1,:).' YY(1,:).' ZZ(1,:).' ]; XX(1,end),YY(1,end), 0],...
[XX(end,1), YY(end,1),0; [XX(end,:).' YY(end,:).' ZZ(end,:).']; XX(end,end),YY(end,end),0],...
[XX(1,1),YY(1,1),0; XX(1,end),YY(1,end),0; XX(end,end),YY(end,end),0; XX(end,1),YY(end,1),0]};
for indP = 1:numel(P)
patch(hAx, P{indP}(:,1),P{indP}(:,2),P{indP}(:,3),'k', 'FaceColor', 'y', 'FaceAlpha', 0.7);
end
end
As you might notice, the helper function draw5Poly
is designed for a scenario where you only need to visualize one such volume per axes. If you do this with two surfaces/volumes it might be difficult to understand if all "walls" are yellow - for this reason you might want to make FaceColor
an input to the function (so you could paint different volumes with a different color).