Shade the encircled area

1 visualización (últimos 30 días)
Muhammad Sanwal
Muhammad Sanwal el 29 de Ag. de 2020
Comentada: Star Strider el 2 de Sept. de 2020
Hi. Can anyone please tell me how to shade the encircled area(in red)?
The graph is as follows
And the code is as follows
tx1=-7:0.1:-1;
tx2=-1:0.1:0.5;
tx3=0.5:0.1:3;
tx4=3:0.1:7;
tx=[tx1 tx2 tx3 tx4];
x1=zeros(size(tx1));
x2=0.6.*ones(size(tx2));
x3=0.3.*ones(size(tx3));
x4=zeros(size(tx4));
x=[x1 x2 x3 x4];
th1=-7:0.1:0;
th2=0:0.1:7;
h1=zeros(size(th1));
h2=ones(size(th2));
h3=[h1 h2];
th=[th1 th2];
h4=exp(-th);
h=h3.*h4;
t=0;
plot(tx,x,-th+t,h,'-','linewidth',2)
ylim([-0.1 1.1])
legend('x(\tau)','h(t-\tau)')
grid

Respuesta aceptada

Star Strider
Star Strider el 30 de Ag. de 2020
After the original code in your Question (not your subsequent Comment), add these lines:
hold on
Ltx = (tx >= -1) & (tx <= 0);
Ltht = (-th+t >= -1) & (-th+t <= 0);
xh = min([x(Ltx); h(Ltht)]);
patch([tx(Ltx) flip(tx(Ltx))], [zeros(size(xh)) xh], 'g')
hold off
to get this plot:
.
  9 comentarios
Muhammad Sanwal
Muhammad Sanwal el 2 de Sept. de 2020
Thank you very much!
Star Strider
Star Strider el 2 de Sept. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (3)

the cyclist
the cyclist el 30 de Ag. de 2020
Combining your new code that aligns the values of t (but not using your attempt at creating the patch), and the same basic idea of Star Strider and Image Analyst, this code accurately aligns the patch as I believe you want. But, as Star Strider says, you decide.
t1=-7:0.1:-1;
t2=-1:0.1:0;
t3=0:0.1:0.5;
t4=0.5:0.1:3;
t5=3:0.1:7;
t=[t1 t2 t3 t4 t5];
x1=zeros(size(t1));
x2=0.6.*ones(size(t2));
x3=0.6.*ones(size(t3));
x4=0.3.*ones(size(t4));
x5=zeros(size(t5));
x=[x1 x2 x3 x4 x5];
h1=zeros(size(t1));
h2=zeros(size(t2));
h3=ones(size(t3));
h4=ones(size(t4));
h5=ones(size(t5));
h6=[h1 h2 h3 h4 h5];
h7=exp(-t);
h=h6.*h7;
figure
hold on
plot(t,x,-t,h,'-','linewidth',2)
ylim([-0.1 1.1])
legend('x(\tau)','h(t-\tau)')
grid
lightGreen = [0.85, 1, 0.85];
xh = min([x; flip(h)]);
plot(t,xh)
patch([flip(t) t], [zeros(size(t)) xh], lightGreen)
  2 comentarios
Muhammad Sanwal
Muhammad Sanwal el 30 de Ag. de 2020
both methods work. thanks!
the cyclist
the cyclist el 30 de Ag. de 2020
Glad it worked out. Be aware that the two solutions cover slightly different areas, and this is more evident with the relative large step size (0.1) you are using.
If you use something smaller (e.g. 0.01), both solutions will more sharply align with your lines, visually.

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 30 de Ag. de 2020
Editada: Bruno Luong el 30 de Ag. de 2020
Use polyshape and let polyshape do the work. Replace plot(P1, ...) with normal plot if you don't like the artefact on x-axis.
t1=-7:0.1:-1;
t2=-1:0.1:0;
t3=0:0.1:0.5;
t4=0.5:0.1:3;
t5=3:0.1:7;
t=[t1 t2 t3 t4 t5];
x1=zeros(size(t1));
x2=0.6.*ones(size(t2));
x3=0.6.*ones(size(t3));
x4=0.3.*ones(size(t4));
x5=zeros(size(t5));
x=[x1 x2 x3 x4 x5];
h1=zeros(size(t1));
h2=zeros(size(t2));
h3=ones(size(t3));
h4=ones(size(t4));
h5=ones(size(t5));
h6=[h1 h2 h3 h4 h5];
h7=exp(-t);
h=h6.*h7;
warning('off','MATLAB:polyshape:repairedBySimplify');
P1=polyshape(t,x);
P2=polyshape(-t,h);
close all
figure
hold on
plot(P2,'facecolor','none','edgecolor','r','linewidth',1)
plot(P1,'facecolor','none','edgecolor','b','linewidth',1)
plot(intersect(P1,P2), 'Facecolor', [0.5, 1, 0.5],'linestyle','none');
ylim([-0.1 1.1])
legend('h(t-\tau)','x(\tau)','whatever')
grid
  2 comentarios
Bruno Luong
Bruno Luong el 31 de Ag. de 2020
Editada: Bruno Luong el 31 de Ag. de 2020
Shift annimation:
t1=-7:0.1:-1;
t2=-1:0.1:0;
t3=0:0.1:0.5;
t4=0.5:0.1:3;
t5=3:0.1:7;
t=[t1 t2 t3 t4 t5];
x1=zeros(size(t1));
x2=0.6.*ones(size(t2));
x3=0.6.*ones(size(t3));
x4=0.3.*ones(size(t4));
x5=zeros(size(t5));
x=[x1 x2 x3 x4 x5];
h1=zeros(size(t1));
h2=zeros(size(t2));
h3=ones(size(t3));
h4=ones(size(t4));
h5=ones(size(t5));
h6=[h1 h2 h3 h4 h5];
h7=exp(-t);
h=h6.*h7;
warning('off','MATLAB:polyshape:repairedBySimplify');
close all
figure
for tau=0:0.1:4
cla
hold on
t1 = t;
t2 = tau-t;
P1=polyshape(t1,x);
P2=polyshape(t2,h);
plot(t1,x,'color','r','linewidth',1)
plot(t2,h,'color','b','linewidth',1)
plot(intersect(P1,P2), 'Facecolor', [0.5, 1, 0.5],'linestyle','none');
xlim([-6 12]);
ylim([-0.1 1.1])
legend('h(t-\tau)','x(\tau)','whatever')
grid on
drawnow
end
Muhammad Sanwal
Muhammad Sanwal el 2 de Sept. de 2020
Yes, this code works too. Thankyou!

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 29 de Ag. de 2020
  2 comentarios
the cyclist
the cyclist el 29 de Ag. de 2020
@muhammad:
Note that the solution that @ImageAnalyst posted makes this statement:
% Assume y1 and y2 have the same number of elements located at the same x values.
Your curves do not obey this assumption, which makes your problem significantly more difficult (I think).
If you can define data in a way that they use the same t (number of elements and same values), this will be an easier task.
Next best would be using at least the same number of elements (if not at the same values).
Muhammad Sanwal
Muhammad Sanwal el 30 de Ag. de 2020
I made my vector t of the same length, but the graph I get is as follows
Please help me make changes to the program so that I can get the original encircled area (in red), as mentioned in my question.
The code is as follows
t1=-7:0.1:-1;
t2=-1:0.1:0;
t3=0:0.1:0.5;
t4=0.5:0.1:3;
t5=3:0.1:7;
t=[t1 t2 t3 t4 t5];
x1=zeros(size(t1));
x2=0.6.*ones(size(t2));
x3=0.6.*ones(size(t3));
x4=0.3.*ones(size(t4));
x5=zeros(size(t5));
x=[x1 x2 x3 x4 x5];
h1=zeros(size(t1));
h2=zeros(size(t2));
h3=ones(size(t3));
h4=ones(size(t4));
h5=ones(size(t5));
h6=[h1 h2 h3 h4 h5];
h7=exp(-t);
h=h6.*h7;
plot(t,x,-t,h,'-','linewidth',2)
ylim([-0.1 1.1])
legend('x(\tau)','h(t-\tau)')
grid
% Shade the area between in light green using the patch() function.
lightGreen = [0.85, 1, 0.85];
% Create the boundaries of the upper points and the lower points.
% Assume x and h have the same number of elements located at the same x values.
upperBoundary = max(x, h);
lowerBoundary = min(x, h);
% Now do the actual display of the shaded region.
patch([t fliplr(t)], [upperBoundary fliplr(lowerBoundary)], lightGreen);
% Plot x and h AFTER the patch so the patch does not cover any of those curves.
hold on;
plot(t, x, 'r-', 'LineWidth', 2); % Plot curve 1 in red.
plot(t, h, 'b-', 'LineWidth', 2); % Plot curve 2 in blue.
legend('Patch', 'y1', 'y2');
% Maximize the figure window
g = gcf; % Setting the WindowState on gcf directly doesn't work for some reason.
g.WindowState = 'maximized'

Iniciar sesión para comentar.

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by