Plot a region based on inequalities

조회 수: 35 (최근 30일)
Dimitrios Anagnostou
Dimitrios Anagnostou 2023년 6월 6일
댓글: Dyuman Joshi 2023년 6월 7일
This comes from the book Mathematics for Engineers and Scientists of Alan Jeffrey.
To create the graphic with Matlab I wrote the following script.
clc, clear, clf % preliminary settings
[x, y] = meshgrid(-5:0.01:5, -5:0.01:5); % Create a grid of x and y values
expression = x.^2 + y.^2; % Compute the expression x^2 + y^2
mask = expression > 1 & y > x & x <= 2; % Define a logical mask for the region
scatter(x(mask), y(mask), 'filled'); % Plot the points satisfying the mask
hold on
% Plotting the boundary lines
% Part x^2 + y^2 = 1, y = x (outside the region) with dashed line
t1 = linspace(-5, -sqrt(2)/2, 100); % x values for the dashed line segment
plot(t1, t1, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
t = linspace(pi/4, 5*pi/4, 100); % Parameter for the circle segment
x_circle = cos(t); % x values for the circle segment
y_circle = sin(t); % y values for the circle segment
plot(x_circle, y_circle, '-.', 'LineWidth', 1.5, 'Color', 'b'); % Plot the circle segment
t2 = linspace(sqrt(2)/2, 2, 100); % x values for the dashed line segment
plot(t2, t2, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
% Part x = 2 (inside the region) with thick line
y_line = linspace(2, max(y(mask(:))), 100); % y values for the thick line segment
x_line = ones(size(y_line)) * 2; % x values for the thick line segment
plot(x_line, y_line, 'LineWidth', 2, 'Color', 'b'); % Plot the thick line segment
xlabel('x'); ylabel('y'); % Label the x and y axes
title('Domaine de définition'); % Set the plot title
legend('x^2 + y^2 > 1, y > x, x <= 2', 'y = x (outside)', ...
'x^2 + y^2 = 1 (ibid)', 'y = x (ibid)', ...
'x = 2 (inside)', 'Location','best'); % Set the legend
xlim([-5 5]); ylim([-5 5]); % Set the x and y axis limits
axis square % Set the aspect ratio to be square
ax = gca; % Get the current axes handle
ax.XAxisLocation = 'origin'; % Set the x-axis location to the origin
ax.YAxisLocation = 'origin'; % Set the y-axis location to the origin
set(gca,'Layer','top') % Set the grid to be on top of the plot
grid on, grid minor % Display both major and minor grids
hold off % Release the hold on the plot
As you see the scatter plot does not clip properly with the boundaries of the region.
Any ideas? Thank you very much.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 6월 7일
편집: Dyuman Joshi 2023년 6월 7일
That happens because the markers of the scatter plot are circle in shape with a finite/comparable size. You can see that by ploting the line y=x, as I have done below -
clc, clear, clf % preliminary settings
[x, y] = meshgrid(-5:0.01:5, -5:0.01:5); % Create a grid of x and y values
expression = x.^2 + y.^2; % Compute the expression x^2 + y^2
mask = expression > 1 & y > x & x <= 2; % Define a logical mask for the region
scatter(x(mask), y(mask)); % Plot the points satisfying the mask
hold on
fplot(@(x) x)
As you can see that the area extends beyond the line, which is not according to the inequality. To tackle this, input a smaller size to the markers of the scatter plot
%Edited code
figure
[x, y] = meshgrid(-5:0.01:5, -5:0.01:5); % Create a grid of x and y values
expression = x.^2 + y.^2; % Compute the expression x^2 + y^2
mask = expression > 1 & y > x & x <= 2; % Define a logical mask for the region
%%Size of the marker specified (the default size is 36)
%%Adjust the value as per requirement
scatter(x(mask), y(mask), 5, 'filled'); % Plot the points satisfying the mask
hold on
% Plotting the boundary lines
% Part x^2 + y^2 = 1, y = x (outside the region) with dashed line
t1 = linspace(-5, -sqrt(2)/2, 100); % x values for the dashed line segment
plot(t1, t1, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
hold on
t = linspace(pi/4, 5*pi/4, 100); % Parameter for the circle segment
x_circle = cos(t); % x values for the circle segment
y_circle = sin(t); % y values for the circle segment
plot(x_circle, y_circle, '-.', 'LineWidth', 1.5, 'Color', 'b'); % Plot the circle segment
t2 = linspace(sqrt(2)/2, 2, 100); % x values for the dashed line segment
plot(t2, t2, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
% Part x = 2 (inside the region) with thick line
y_line = linspace(2, max(y(mask(:))), 100); % y values for the thick line segment
x_line = ones(size(y_line)) * 2; % x values for the thick line segment
plot(x_line, y_line, 'LineWidth', 2, 'Color', 'b'); % Plot the thick line segment
xlabel('x'); ylabel('y'); % Label the x and y axes
title('Domaine de définition'); % Set the plot title
legend('x^2 + y^2 > 1, y > x, x <= 2', 'y = x (outside)', ...
'x^2 + y^2 = 1 (ibid)', 'y = x (ibid)', ...
'x = 2 (inside)', 'Location','best'); % Set the legend
xlim([-5 5]); ylim([-5 5]); % Set the x and y axis limits
axis square % Set the aspect ratio to be square
ax = gca; % Get the current axes handle
ax.XAxisLocation = 'origin'; % Set the x-axis location to the origin
ax.YAxisLocation = 'origin'; % Set the y-axis location to the origin
set(gca,'Layer','top') % Set the grid to be on top of the plot
grid on, grid minor % Display both major and minor grids
hold off % Release the hold on the plot
  댓글 수: 3
Dimitrios Anagnostou
Dimitrios Anagnostou 2023년 6월 7일
I found a workarround solution.
lightBlue = [91, 207, 244] / 255;
scatter(x(mask), y(mask), 5, 'filled', 'MarkerFaceColor', ...
lightBlue, 'MarkerEdgeColor', 'none');
But I am wondering if we can still modify directly the opacity.
Dyuman Joshi
Dyuman Joshi 2023년 6월 7일
MarkerFaceAlpha works for me (R2023a here and R2021a on my PC)
What error are you getting?
lightBlue = [91, 207, 244] / 255;
figure
[x, y] = meshgrid(-5:0.01:5, -5:0.01:5); % Create a grid of x and y values
expression = x.^2 + y.^2; % Compute the expression x^2 + y^2
mask = expression > 1 & y > x & x <= 2; % Define a logical mask for the region
%%0.01 MarkerFaceAlpha
scatter(x(mask), y(mask), 5, 'filled', 'MarkerFaceColor', lightBlue, 'MarkerFaceAlpha', 0.01); % Plot the points satisfying the mask
%%0.1 MarkerFaceAlpha
figure
scatter(x(mask), y(mask), 5, 'filled', 'MarkerFaceColor', lightBlue, 'MarkerFaceAlpha', 0.1); % Plot the points satisfying the mask
%Edited code
figure
[x, y] = meshgrid(-5:0.01:5, -5:0.01:5); % Create a grid of x and y values
expression = x.^2 + y.^2; % Compute the expression x^2 + y^2
mask = expression > 1 & y > x & x <= 2; % Define a logical mask for the region
%%0.01 MarkerFaceAlpha
scatter(x(mask), y(mask), 5, lightBlue, 'filled', 'MarkerFaceAlpha', 0.01); % Plot the points satisfying the mask
hold on
% Plotting the boundary lines
% Part x^2 + y^2 = 1, y = x (outside the region) with dashed line
t1 = linspace(-5, -sqrt(2)/2, 100); % x values for the dashed line segment
plot(t1, t1, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
hold on
t = linspace(pi/4, 5*pi/4, 100); % Parameter for the circle segment
x_circle = cos(t); % x values for the circle segment
y_circle = sin(t); % y values for the circle segment
plot(x_circle, y_circle, '-.', 'LineWidth', 1.5, 'Color', 'b'); % Plot the circle segment
t2 = linspace(sqrt(2)/2, 2, 100); % x values for the dashed line segment
plot(t2, t2, '--', 'LineWidth', 1.5, 'Color', 'b'); % Plot the dashed line segment
% Part x = 2 (inside the region) with thick line
y_line = linspace(2, max(y(mask(:))), 100); % y values for the thick line segment
x_line = ones(size(y_line)) * 2; % x values for the thick line segment
plot(x_line, y_line, 'LineWidth', 2, 'Color', 'b'); % Plot the thick line segment
xlabel('x'); ylabel('y'); % Label the x and y axes
title('Domaine de définition'); % Set the plot title
legend('x^2 + y^2 > 1, y > x, x <= 2', 'y = x (outside)', ...
'x^2 + y^2 = 1 (ibid)', 'y = x (ibid)', ...
'x = 2 (inside)', 'Location','best'); % Set the legend
xlim([-5 5]); ylim([-5 5]); % Set the x and y axis limits
axis square % Set the aspect ratio to be square
ax = gca; % Get the current axes handle
ax.XAxisLocation = 'origin'; % Set the x-axis location to the origin
ax.YAxisLocation = 'origin'; % Set the y-axis location to the origin
set(gca,'Layer','top') % Set the grid to be on top of the plot
grid on, grid minor % Display both major and minor grids
hold off % Release the hold on the plot

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by