Shading an area between two curves symbolically
3 views (last 30 days)
Show older comments
I want to shade the area under the line y=2 and above the curve y=1+cos(x) from 0 to pi. I'm having trouble doing this. This is my code:
dy1 = 1+cos(x);
dy2 = 2;
fplot(dy1, [0 pi]); hold on;
fplot(dy2, [0 pi]);
patch([x fliplr(x)], [dy2 fliplr(dy1)], 'g');
hold off;
0 Comments
Accepted Answer
Star Strider
on 16 Jun 2025
It is necessary to get the relevant 'x' and 'y' values from the fplot calls first. You can then use them in the patch call.
Try this --
syms x
dy1 = 1+cos(x);
dy2 = 2;
figure
fp1 = fplot(dy1, [0 pi]);
hold on
fp2 = fplot(dy2, [0 pi]);
hold off
ylim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([x1 fliplr(x2)], [y1 fliplr(y2)], 'g');
hold off;
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Calculus 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!