How to color the space between ellipse?
3 views (last 30 days)
Show older comments
I want to shade the following region :
I know how to plot ellipses using the below code .
But I want to shade the region between two ellipses and I only need x>=0 and y>=0.
In general, if how do I shade the region when ? Thank you.
ezplot('x^2 + y^2/4 - 1');hold on; ezplot('x^2 + y^2/4 - 4'); hold off
0 Comments
Answers (3)
Torsten
on 26 Oct 2022
Edited: Torsten
on 26 Oct 2022
v = -6:0.001:6; % plotting range from -5 to 5
[x y] = meshgrid(v); % get 2-D mesh for x and y
cond1 = x.^2 + y.^2/4 >= 1; % check conditions for these values
cond2 = x.^2 + y.^2/4 <= 4;
cond3 = x >= 0;
cond4 = y >= 0;
cond1 = double(cond1); % convert to double for plotting
cond2 = double(cond2);
cond3 = double(cond3);
cond4 = double(cond4);
cond1(cond1 == 0) = NaN; % set the 0s to NaN so they are not plotted
cond2(cond2 == 0) = NaN;
cond3(cond3 == 0) = NaN;
cond4(cond4 == 0) = NaN;
cond = cond1.*cond2.*cond3.*cond4; % multiply the conditions to keep only the common points
surf(x,y,cond)
view(0,90) % change to top view
0 Comments
Vasudev Sridhar
on 26 Oct 2022
Alternative solution in case you are dealing with radial coordinates.
x1 = linspace(0,1,100); % Generate the first set of x coordinates. Change the first two parameters to get different ranges
y1 = 2*sqrt(1-x1.^2);
x2 = linspace(0,2,100); % Generate the second set of x coordinates. Change the first two parameters to get different ranges
y2 = 2*sqrt(4-x2.^2);
x = [x2, fliplr(x1)]; % Get the set of x coordinates for the resultant polygon. The order of the x-coordinates for the second shape shape need to be reversed for this
yeff = [y2, fliplr(y1)]; % Get the set of y coordinates for the resultant polygon. The order of the y-coordinates for the second shape shape need to be reversed for this
fill(x, yeff, 'g'); % Fill the polygon with color green(g)
0 Comments
Matt J
on 26 Oct 2022
Edited: Matt J
on 26 Oct 2022
fn=@(r) scale( nsidedpoly(1000,'Radius',r) ,[1,2] );
region=subtract(fn(2),fn(1));
plot(region); axis equal; axis([0,4, 0,4])
1 Comment
Matt J
on 26 Oct 2022
Or, if you really need the region truncated to the positive quadrant,
fn=@(r) scale( nsidedpoly(1000,'Radius',r) ,[1,2] );
region=subtract(fn(2),fn(1));
region=intersect(region, polyshape([0,0;2,0; 2 4; 0 4]));
plot(region); axis equal;
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!