MATLAB: How to fill in an area between a curve that is nonlinear

fill areanonlinear function plotting

I have a function that is piecewise defined that I would like to fill the area to the right of. Here is what I have so far:
x0=[-10:0.01:10];
t=(-1./abs(x0));
figure; plot(t,x0)
grid on
xlim([-10 10])
ylim([-10 10])
hold on
% Now shade the Omega region
grayColor = [0.9 0.9 0.9];
h1 = patch([0 0 10 10],[min(ylim) max(ylim) max(ylim) min(ylim)],grayColor);
set(h1,'EdgeColor','none');
I can only get a rectangle to the right of the plot to fill. When I try to fill the area between the top and bottom part of the function, the result is a blank fill area.
h2 = patch(t,x0,grayColor);
set(h2,'EdgeColor','none');
Any ideas?

Best Answer

This was something of a challenge because ‘t’ is not uniformly finite. Nevertheless, a bit ot creative logical indexing works, at least as I understand what you want:
The Code —
x0=[-10:0.01:10];
x0=[-10:0.1:10];
t=(-1./abs(x0));
tle0 = (t<=0) & (isfinite(t));
xle0 = (x0<=0);
txle0 = tle0 & xle0;
figure; plot(t,x0)
grid on
xlim([-10 10])
ylim([-10 10])
hold on
% Now shade the Omega region
grayColor = [0.9 0.9 0.9];
h1 = patch([-0.1*[1 1] 10 10],[min(ylim) max(ylim) max(ylim) min(ylim)],grayColor); % Right Patch
h2 = patch([x0(txle0) fliplr(x0(txle0))],[t(txle0) 0*ones(size(t(txle0)))],grayColor); % Lower Left Patch
h3 = patch([x0(txle0) fliplr(x0(txle0))],[abs(t(txle0)) 0*ones(size(t(txle0)))],grayColor); % Upper Left Patch
set([h1,h2,h3],'EdgeColor','none');
The Plot —
How can I fill in an area between a curve that is nonlinear - 2018 11 15.png