MATLAB: How to shade an area bound by 2 curves in an ezplot

ezplotfillMATLABshading

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.

Best Answer

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');