How to plot boundary for overlapping region between 2 areas , each area bounded by a pair of curves?
5 views (last 30 days)
Show older comments
I have used ezplot to plot 4 curves. I want to plot the boundary for the overlapping region between regions (1) and (2), where region(1) is the area between curve1 and curve2 & region(2) is the area between curve3 and curve4. Please suggest.
0 Comments
Answers (1)
nick
on 1 Feb 2024
Hi Supanna,
I understand from your query that you are interested in visualising the boundary for overlapping region between region1, overlapping region between curve1 and curve2, and region2, overlapping region between curve3 and curve4.
To find and plot the boundary of the overlapping region between region1 and region2, you need to determine where the regions defined by these curves intersect. Below is an example MATLAB code that finds the boundary of the overlapping region between four curves and plots the result :
% Define the functions for the curves
curve1 = @(x) sin(x) + 1;
curve2 = @(x) 0.5 * sin(x + 1);
curve3 = @(x) cos(x);
curve4 = @(x) 0.5 * cos(x + 1);
% Define the range for x values
xRange = linspace(-2*pi, 2*pi, 1000); % A range of x values
% Evaluate the curves over the x range
y1 = curve1(xRange);
y2 = curve2(xRange);
y3 = curve3(xRange);
y4 = curve4(xRange);
% Plot the curves
figure;
hold on;
plot(xRange, y1, 'b-', 'LineWidth', 2);
plot(xRange, y2, 'r-', 'LineWidth', 2);
plot(xRange, y3, 'g-', 'LineWidth', 2);
plot(xRange, y4, 'm-', 'LineWidth', 2);
% Find the upper and lower bounds of the regions
upperBound1 = max(y1, y2);
lowerBound1 = min(y1, y2);
upperBound2 = max(y3, y4);
lowerBound2 = min(y3, y4);
% Find the intersection of the two regions
upperBoundOverlap = min(upperBound1, upperBound2);
lowerBoundOverlap = max(lowerBound1, lowerBound2);
% Identify the points where the upper bound is above the lower bound (valid overlap)
inOverlap = upperBoundOverlap > lowerBoundOverlap;
% Find the boundaries of the overlapping region
xOverlap = xRange(inOverlap);
yUpperOverlap = upperBoundOverlap(inOverlap);
yLowerOverlap = lowerBoundOverlap(inOverlap);
% Plot the boundary of the overlapping region
plot(xOverlap, yUpperOverlap, 'k-', 'LineWidth', 3);
plot(xOverlap, yLowerOverlap, 'k-', 'LineWidth', 3);
% Fill the overlapping region with color
fill([xOverlap, fliplr(xOverlap)], [yUpperOverlap, fliplr(yLowerOverlap)], 'y', 'FaceAlpha', 0.5);
% Add labels and legend
xlabel('x');
ylabel('y');
title('Boundary of Overlapping Region Between Four Curves');
legend('Curve 1', 'Curve 2', 'Curve 3', 'Curve 4', 'Overlap Boundary');
hold off;
In this code, the overlapping region is calculated by finding the maximum of the lower bounds and the minimum of the upper bounds of the two areas defined by the curves. The boundary of the overlapping region is then plotted using the "plot" function, and the area is filled with color using "fill".
Hope this helps.
0 Comments
See Also
Categories
Find more on Interpolation 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!