Shaded region inside plot
7 views (last 30 days)
Show older comments
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');
0 Comments
Accepted Answer
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);
More Answers (0)
See Also
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!