Shade the encircled area
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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
Más respuestas (3)
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
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.
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
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
Image Analyst
el 29 de Ag. de 2020
See the FAQ and adapt as needed.
2 comentarios
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).
Ver también
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!