Shaded region inside plot

7 views (last 30 days)
LIM MING HUI
LIM MING HUI on 30 Apr 2022
Commented: Voss on 30 Apr 2022
Hello, I want to ask is it possible to shade thses 4 region with different colour?
x=[0:0.001:20];
y=(1/(2*0.2*9.81))*x.^2;
z=x*(10*3.7/(((3)^(1/2))*0.2*9.81))^(1/2);
plot(x,y,'m-', 'LineWidth', 3);
hold on;
plot(x,z,'k-', 'LineWidth', 3);
hold on;
x2 = [x,fliplr(x)];
y2 = [y,fliplr(z)];
fill(x2,y2,'b','LineStyle','none');

Accepted Answer

Voss
Voss on 30 Apr 2022
x=[0:0.001:20];
y=(1/(2*0.2*9.81))*x.^2;
z=x*(10*3.7/(((3)^(1/2))*0.2*9.81))^(1/2);
colors = [ ...
1 1 0; ... % 1: yellow
0 1 0; ... % 2: green
0 1 1; ... % 3: cyan
0.5 0.5 0.5]; % 4: grey
idx = find(y > z,1);
fill( ...
[x x(end) x(1)], ...
[z(1:idx) y(idx+1:end) 120 120], ...
colors(1,:),'LineStyle','none');
hold on
fill( ...
[x(1:idx-1) x(idx-1:-1:1)], ...
[z(1:idx-1) y(idx-1:-1:1)], ...
colors(2,:),'LineStyle','none');
fill( ...
[x(idx:end) x(end:-1:idx)], ...
[y(idx:end) z(end:-1:idx)], ...
colors(3,:),'LineStyle','none');
fill( ...
[x x(end) x(1)], ...
[y(1:idx) z(idx+1:end) 0 0], ...
colors(4,:),'LineStyle','none');
% plot the y and z lines last so they aren't obscured by the fills
plot(x,y,'m-', 'LineWidth', 3);
plot(x,z,'k-', 'LineWidth', 3);
  2 Comments
LIM MING HUI
LIM MING HUI on 30 Apr 2022
Thanks for your help!
Voss
Voss on 30 Apr 2022
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!