如何在Maple/MATLAB中绘制点密度随正弦值变化的正弦图俯视图?
Hey there! Let's figure out how to create that top-down plot where points are densest at the peaks and troughs of the sine wave, and sparser near the zero crossings. Your initial attempt with plot3d(sin(x), linestyle=dot) uses uniform sampling, which is why you aren't seeing the density variation you want. We need to manually generate non-uniform points that cluster where |sin(x)| is large. Here's how to do it in both Maple and MATLAB:
Maple Solution
Instead of relying on plot3d's automatic grid, we'll build a custom set of points with density tied to the sine wave's magnitude. Here's the code:
# Define the x range we want to plot x_min := 0; x_max := 4*Pi; # Generate a base set of evenly spaced points (for overall coverage) base_x := linspace(x_min, x_max, 100); # Create a dense set of points, then filter to keep only those where |sin(x)| is large high_density_candidates := linspace(x_min, x_max, 500); filtered_high_x := select(x -> abs(sin(x)) > 0.8, high_density_candidates); # Combine and sort all points to maintain order all_x := sort([op(base_x), op(filtered_high_x)]); # Set y-values to 0 (since we want a top-down view, y doesn't change) y_vals := Array([seq(0, x in all_x)]); # Compute the corresponding sine values for z z_vals := Array([seq(sin(x), x in all_x)]); # Plot as a 3D scatter plot, then switch to top-down perspective scatter3d(all_x, y_vals, z_vals, style=point, symbol=circle, symbolsize=5); # Adjust view to look straight down along the z-axis view = [x_min..x_max, -0.1..0.1, -1..1]; orientation = [0, 90];
How this works:
- We start with a sparse base grid to ensure we cover the entire range.
- We add extra points specifically where
|sin(x)| > 0.8(near the peaks and troughs), making those areas much denser. - The
orientation = [0, 90]setting switches the plot to a top-down view, so you'll see the point density variation clearly.
MATLAB Solution
The approach is similar: we generate non-uniform points clustered at the sine wave's extremes, then plot them and adjust the view. Here's the code:
% Define the x range x_min = 0; x_max = 4*pi; % Create a base set of evenly spaced points base_x = linspace(x_min, x_max, 100); % Generate a dense set of points, filter to keep those near sine peaks/troughs high_density_candidates = linspace(x_min, x_max, 500); filtered_high_x = high_density_candidates(abs(sin(high_density_candidates)) > 0.8); % Combine and sort all points all_x = sort([base_x, filtered_high_x]); % Set y-values to 0 (constant for top-down view) y_vals = zeros(size(all_x)); % Compute sine values for z z_vals = sin(all_x); % Create a filled scatter plot scatter3(all_x, y_vals, z_vals, 10, 'filled'); % Switch to top-down view (look along z-axis) view(0, 90); % Add labels and clean up the plot xlabel('x'); ylabel('y'); zlabel('sin(x)'); axis([x_min x_max -0.1 0.1 -1 1]); grid on;
How this works:
- Just like in Maple, we combine a sparse base grid with extra points in high-magnitude sine regions.
- The
view(0, 90)command gives the top-down perspective, so you can easily see the dense clusters atsin(x) = ±1and sparse points nearsin(x) = 0.
内容的提问来源于stack exchange,提问作者Jerry

