MATLAB Answers

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

6 views (last 30 days)
Mohammad
Mohammad on 29 May 2020
Commented: Image Analyst on 29 May 2020
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:

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 29 May 2020
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.

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 May 2020

  2 Comments

Mohammad
Mohammad on 29 May 2020
I already checked that. That doesn't address my question. I want to shade area between 3 special curves.
Image Analyst
Image Analyst on 29 May 2020
Actually it did. You accepted Star's answer and you'll notice he used patch(), just like the FAQ did. You just needed to figure out the boundaries of the patch, which he kindly did for you.

Sign in to comment.