Color areas between two curves and the x and y-axis (not polynomial)

2 views (last 30 days)
I have two functions of one variable (p). I am trying to color the areas on the right of each curve. I succeed in doing this on a separate figure for each curve. I want to put those two curves on the same graph. When I try to do this, somehow it is the area on the left of the red curve that is shaded instead of the area of the right side of the red curve.
p = 0:0.001:1;
curve1 = ((3.*p)-2)./p
curve5 = 1./(2.*p);
ax1 = axes;
figure(1)
hold(ax1, 'on')
hold on
plot(p, curve1, 'LineWidth', 1.5,'linestyle','-','color',[0 0.4470 0.7410]);
hold on;
xlim([0 1]);
ylim([0 1]);
grid on;
lowerboundary = min(ylim(ax1));
area(curve1,lowerboundary,'FaceColor','b','FaceAlpha',0.2);
hold on
area(p,curve1,'FaceColor','b','FaceAlpha',0.2);
ax=gca;
figure(2)
hold(ax1, 'on')
hold on
plot(p, curve5, 'LineWidth', 1.5,'linestyle','-','color',[0.6350 0.0780 0.1840]);
hold on;
xlim([0 1]);
ylim([0 1]);
grid on;
upperboundary = max(ylim(ax1));
area(p,curve5,'FaceColor','r','FaceAlpha',0.2);
hold on
area(curve5,upperboundary,'FaceColor','r','FaceAlpha',0.2);
ax=gca;
figure(3)
hold(ax1, 'on')
hold on
plot(p, curve5, 'LineWidth', 1.5,'linestyle','-','color',[0.6350 0.0780 0.1840]);
hold on
plot(p, curve1, 'LineWidth', 1.5,'linestyle','-','color',[0 0.4470 0.7410]);
xlim([0 1]);
ylim([0 1]);
grid on
upperboundary = max(ylim(ax1));
lowerboundary = min(ylim(ax1));
area(curve5,upperboundary,'FaceColor','r','FaceAlpha',0.2);
hold on
area(p,curve5,'FaceColor','r','FaceAlpha',0.2);
hold on
area(curve1,lowerboundary,'FaceColor','b','FaceAlpha',0.2);
hold on
area(p,curve1,'FaceColor','b','FaceAlpha',0.2);
ax=gca;
Here are the outputs for the respectively figure 1, 2 and 3. My goal is to have on figure 3 the red color area on the right side of the red line, as if figures 1 and 2 were superposed/merged.
Any help would be appreciated!

Accepted Answer

Chunru
Chunru on 28 Feb 2024
It seems that area function cannot individually change the basevalue (I have done some separate tests).
One work around is to use two axes of same position in the same figure so that each axes has its one basevalue.
p = 0:0.001:1;
curve1 = ((3.*p)-2)./p;
curve5 = 1./(2.*p);
figure(1)
ax1 = axes;
hold(ax1, 'on')
plot(p, curve1, 'LineWidth', 1.5,'linestyle','-','color',[0 0.4470 0.7410]);
xlim([0 1]);
ylim([0 1]);
grid on;
lowerboundary = min(ylim(ax1));
area(p, curve1, lowerboundary,'FaceColor','b','FaceAlpha',0.2);
figure(2)
ax1 = axes;
hold(ax1, 'on')
plot(p, curve5, 'LineWidth', 1.5,'linestyle','-','color',[0.6350 0.0780 0.1840]);
xlim([0 1]);
ylim([0 1]);
grid on;
upperboundary = max(ylim(ax1));
area(p,curve5,upperboundary,'FaceColor','r','FaceAlpha',0.2);
% Workaround with 2 axes
f = figure(3);
ax1 = axes(f);
hold(ax1, 'on')
plot(ax1, p, curve5, 'LineWidth', 1.5,'linestyle','-','color',[0.6350 0.0780 0.1840]);
xlim(ax1,[0 1]);
ylim(ax1,[0 1]);
grid on
upperboundary = max(ylim(ax1));
area(ax1, p, curve5, upperboundary,'FaceColor','r','FaceAlpha',0.2);
ax2= axes(f, "Position", ax1.Position, "Color", "none");
hold(ax2, 'on')
plot(ax2, p, curve1, 'LineWidth', 1.5,'linestyle','-','color',[0 0.4470 0.7410]);
xlim(ax2,[0 1]);
ylim(ax2,[0 1]);
grid on
lowerboundary = min(ylim(ax2));
area(ax2,p, curve1,lowerboundary,'FaceColor','b','FaceAlpha',0.2);

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!