How to shade an area bound by 2 curves in an ezplot?

7 ビュー (過去 30 日間)
Nicholas Wong
Nicholas Wong 2013 年 9 月 3 日
回答済み: Christopher Creutzig 2014 年 3 月 31 日
I have a plot depicting two symbolic functions:
syms x
y1 = 0.05*x;
y2 = log10(x);
figure
ezplot(y1)
hold on
ezplot(y2)
hold off
I need to shade two bounded regions: the area below y1 and to the left of y2, and the area above y1 and to the right of y2. I tried to use the `fill' function, but I don't think that works for symbolic math functions in ezplot. Any help will be appreciated.

採用された回答

Christopher Creutzig
Christopher Creutzig 2014 年 3 月 31 日
You can get the x and y data from a plot and then use fill or patch, although it is not necessarily straightforward and you need to find the intersection point:
syms x
f1 = 0.05*x;
f2 = log10(x);
x0 = double(solve(f1 == f2)); % and manually check it is a single value in the range we want
figure
h1 = ezplot(f1);
hold on
h2 = ezplot(f2);
xd1 = get(h1, 'XData'); xd2 = get(h2, 'XData');
yd1 = get(h1, 'YData'); yd2 = get(h2, 'YData');
% limit to finite values
yd2 = max(yd2, -5);
% revert one line, since we need to “circle” around the areas to fill
xd2 = xd2(end:-1:1);
yd2 = yd2(end:-1:1);
fill([xd1(xd1 < x0) xd2(xd2 < x0)], [yd1(xd1 < x0) yd2(xd2 < x0)], 'b');
fill([xd1(xd1 > x0) xd2(xd2 > x0)], [yd1(xd1 > x0) yd2(xd2 > x0)], 'r');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by