MATLAB: Shade area between a curve and a vertical line and the x axis

areafaqfillshade area

I have the following code:
clc
clear
close all
x = [0:.001:.2];
y = normpdf(x,.1,.02);
subplot(211)
plot(x,y,'linewidth',2);grid;box on;
hold on
YL = get(gca, 'ylim');
YR = YL(2) - YL(1);
YL = [YL(1) - 1000 * YR, YL(2) + 1000 * YR];
line([.14 .14], YL, 'YLimInclude', 'off', 'linewidth', 2, 'Color', ...
'black','LineStyle','-.');
I want to shade the area shown below:

Best Answer

Add these lines:
xa = linspace(0.14, 0.2, 25); % Define ‘x’ For ‘patch’

ya = normpdf(xa,.1,.02); % Define ‘y’ For ‘patch’

patch([xa fliplr(xa)], [zeros(size(ya)) fliplr(ya)], 'r')
so the compllete revised code is now:
x = [0:.001:.2];
y = normpdf(x,.1,.02);
subplot(211)
plot(x,y,'linewidth',2);grid;box on;
hold on
YL = get(gca, 'ylim');
YR = YL(2) - YL(1);
YL = [YL(1) - 1000 * YR, YL(2) + 1000 * YR];
line([.14 .14], YL, 'YLimInclude', 'off', 'linewidth', 2, 'Color', ...
'black','LineStyle','-.');
xa = linspace(0.14, 0.2, 25); % Define ‘x’ For ‘patch’
ya = normpdf(xa,.1,.02); % Define ‘y’ For ‘patch’
patch([xa fliplr(xa)], [zeros(size(ya)) fliplr(ya)], 'r')
hold off
That should do what you want.
Related Question