Plotting a single contour to divide a region into two

In the figure I attached, I have oscillatory region and non-oscillatory region. Pleas how can I plot a single contour line that will divide the oscillarory region from the non-oscillatory region.
openfig('figure.fig');

 Accepted Answer

Here's an example:
% create vectors for x/y non/oscillatory
n = 5;
o_idx = [1 2 6 7 8 11 12 16 21 22];
[xo,yo] = meshgrid(1:n);
xo = xo(:);
yo = yo(:);
xn = xo;
yn = yo;
idx = ismember(1:n^2,o_idx);
xo = xo(idx);
yo = yo(idx);
xn = xn(~idx);
yn = yn(~idx);
% plot the points
figure
plot(xo,yo,'.r','MarkerSize',12)
hold on
plot(xn,yn,'.c','MarkerSize',12)
xlim([0 n+1])
ylim([0 n+1])
% make a contour dividing the regions
c = zeros(n);
c(idx) = 1;
contour(c,'LevelList',0.5,'Color','k')

8 Comments

Thank you so much for your response. Please why did you assign 1 for c(idx) as coded in the line (c(idx) = 1)?
You're welcome!
c is an n-by-n matrix that has value 1 in the oscillatory region and value 0 in the non-oscillatory region.
First, I initialize c to be all zeros and then assign 1 for the oscillatory region, given by idx, which is calculated in a previous line.
Thank you for your clarifiaction. Please is there any reason why used 0.5 as the contour level?
Only because it is halfway between 0 and 1 (the values in c), so I thought that would look best.
% Thank you Voss. I couldn't get what I want using my data. I have attached
% data file. Please could you help out.
data = load("sol_osc_data.mat");
l_Osc = data.osc_lvalplanarright;
l_Non_osc= data.non_osc_lvalplanarright;
xi_Osc = data.osc_xivalplanarright;
xi_Non_osc= data.non_osc_xivalplanarright;
figure
hold on
for i=1:length(xi_Non_osc)
p4a = plot(l_Non_osc(i),xi_Non_osc(i), 'c.', 'LineWidth', 2, 'MarkerSize', 10);
end
for i=1:length(l_Osc)
p4a = plot(l_Osc(i), xi_Osc(i), 'r.', 'LineWidth', 2, 'MarkerSize', 10);
end
The best solution I can come up with right now is to draw a tight boundary around the oscillatory points (and Answers is not letting me run code at the moment, but you can run it on your machine to see the result):
data = load('sol_osc_data.mat')
l_Osc = data.osc_lvalplanarright;
l_Non_osc= data.non_osc_lvalplanarright;
xi_Osc = data.osc_xivalplanarright;
xi_Non_osc= data.non_osc_xivalplanarright;
figure
hold on
plot(l_Non_osc, xi_Non_osc, 'c.', 'LineWidth', 2, 'MarkerSize', 10) % these lines replace
plot(l_Osc, xi_Osc, 'r.', 'LineWidth', 2, 'MarkerSize', 10) % your plotting loops
% calculate and plot the boundary of the oscillatory points:
idx = boundary(l_Osc(:),xi_Osc(:),1);
plot(l_Osc(idx),xi_Osc(idx),'k')
Thank you so much, this works.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Asked:

on 30 Mar 2024

Commented:

on 31 Mar 2024

Community Treasure Hunt

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

Start Hunting!