半径50球体1/4和3/4填充容量的图形绘制技术问询
Visualizing a Sphere with 1/4 and 3/4 Volume Fill Levels in MATLAB
Got it, let's break this down. You start with a basic sphere of radius 50 centered at (0,0,0), and you want to show two states: one filled to 1/4 its total capacity, and another filled to 3/4. Here's a step-by-step solution with code and explanations:
First, we need to calculate the fill height (or the z-coordinate of the fill surface) for each volume level. The volume of a spherical segment (the filled part) can be calculated using integrals or the spherical cap volume formula. We'll use MATLAB's fzero function to solve for the exact z-coordinate where the volume below (or above) that plane equals 1/4 or 3/4 of the total sphere volume.
Full MATLAB Code
% Define sphere parameters r = 50; total_volume = (4/3)*pi*r^3; % Calculate z-coordinates for 1/4 and 3/4 fill levels % For 1/4 fill: solve for z where volume from z=-r to z=z1 equals total_volume/4 z1 = fzero(@(z) pi*(r^2*z - z^3/3 + 2*r^3/3) - total_volume/4, -r); % For 3/4 fill: use symmetry (since 3/4 fill is the complement of 1/4 fill) z2 = -z1; % Generate high-resolution sphere coordinates [x, y, z] = sphere(100); % 100x100 grid for smoother surfaces x = r*x; y = r*y; z = r*z; % Create figure and subplots figure('Position', [100 100 1000 500]) % Subplot 1: 1/4 Volume Fill subplot(1,2,1) % Draw the empty sphere (transparent) surf(x, y, z, 'FaceAlpha', 0.3, 'EdgeColor', 'none'); hold on % Generate fill region: all points below z1 [X_fill, Y_fill] = meshgrid(x(1,:), y(:,1)); Z_fill = repmat(z1, size(X_fill)); % Draw the filled segment (using surf to create a solid cap) z_cap = z; z_cap(z > z1) = NaN; % Hide points above the fill level surf(x, y, z_cap, 'FaceColor', '#1f77b4', 'EdgeColor', 'none'); % Draw the fill plane surf(X_fill, Y_fill, Z_fill, 'FaceColor', 'k', 'FaceAlpha', 0.5, 'EdgeColor', 'none'); % Set plot properties title('Sphere with 1/4 Total Volume Filled', 'FontSize', 12) xlabel('X') ylabel('Y') zlabel('Z') axis equal; grid on; view(30, 30) % Subplot 2: 3/4 Volume Fill subplot(1,2,2) % Draw the empty sphere (transparent) surf(x, y, z, 'FaceAlpha', 0.3, 'EdgeColor', 'none'); hold on % Generate fill region: all points below z2 z_cap = z; z_cap(z > z2) = NaN; % Hide points above the fill level surf(x, y, z_cap, 'FaceColor', '#ff7f0e', 'EdgeColor', 'none'); % Draw the fill plane Z_fill = repmat(z2, size(X_fill)); surf(X_fill, Y_fill, Z_fill, 'FaceColor', 'k', 'FaceAlpha', 0.5, 'EdgeColor', 'none'); % Set plot properties title('Sphere with 3/4 Total Volume Filled', 'FontSize', 12) xlabel('X') ylabel('Y') zlabel('Z') axis equal; grid on; view(30, 30)
Key Explanations:
- Fill Level Calculation: We use
fzeroto solve the integral equation for the volume of the spherical segment. For 3/4 fill, we leverage the sphere's symmetry—since filling 3/4 is the same as leaving 1/4 empty at the top, the fill plane's z-coordinate is just the negative of the 1/4 fill plane's z-coordinate. - High-Resolution Sphere: Using
sphere(100)instead of the defaultsphere()gives a smoother, more detailed surface that makes the fill boundary clearer. - Visual Clarity: The empty sphere is drawn with low transparency (
FaceAlpha=0.3) so you can see the filled segment inside. The fill plane is a semi-transparent black surface to clearly mark where the fill ends. - Color Coding: We use distinct colors for the two fill states (blue for 1/4, orange for 3/4) to make them easy to distinguish at a glance.
What the Output Looks Like:
- Left subplot: A mostly empty sphere with the bottom 1/4 filled in blue, marked by a black semi-transparent plane.
- Right subplot: A mostly filled sphere with the bottom 3/4 filled in orange, marked by a higher black semi-transparent plane.
内容的提问来源于stack exchange,提问作者Python_ler
相关产品推荐
相关产品推荐

