How to shade with upper and lower limits along a curve line? (see example in image below)

7 views (last 30 days)

Accepted Answer

albara
albara on 26 Apr 2023
To shade the area between two curves in MATLAB, you can use the fill() function. Here is an example code that demonstrates how to do this:
% Define x and y values for the curves
x = linspace(0, 4*pi, 100);
y1 = sin(x);
y2 = cos(x);
% Define the upper and lower limits
y_upper = y1 + 0.5;
y_lower = y2 - 0.5;
% Plot the curves
plot(x, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x, y2, 'r-', 'LineWidth', 2);
% Fill the area between the curves
fill([x fliplr(x)], [y_upper fliplr(y1)], 'b', 'FaceAlpha', 0.2);
fill([x fliplr(x)], [y_lower fliplr(y2)], 'r', 'FaceAlpha', 0.2);
% Add legend and axis labels
legend('y1', 'y2');
xlabel('x');
ylabel('y');
In this example, the fill() function is used twice to fill the area between the two curves. The fliplr() function is used to create a mirrored set of x values so that the area is filled in properly. The 'FaceAlpha' parameter is used to set the transparency of the filled area.
You can modify the y_upper and y_lower arrays to define your upper and lower limits for your specific application.
is this helps?
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes

More Answers (1)

chicken vector
chicken vector on 26 Apr 2023
N = 100;
x = linspace(0,2*pi,N);
y = 1 + cos(x);
yNoiseUp = y + 0.5 + .25*(sin(x) + .25*(rand(1,N) - 0.5));
yNoiseDown = y - 0.5 - .25*(sin(x) + .25*(rand(1,N) - 0.5));
figure;
grid on;
hold on;
fill([x flip(x)],[yNoiseUp flip(yNoiseDown)],[.8 .8 .8],'FaceAlpha',.7)
plot(x,y,'k','LineWidth',2);
plot(x,yNoiseUp,'k');
plot(x,yNoiseDown,'k');
hold off;
xlim([0,x(end)]);
xlabel('\theta_3 [deg]')
ylabel('C_{p,3}')
Result:

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!