
Shade the encircled area
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2020-8-30
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 个评论
更多回答(3 个)
the cyclist
2020-8-30
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 个评论
the cyclist
2020-8-30
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
2020-8-30
编辑:Bruno Luong
2020-8-30
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 个评论
Bruno Luong
2020-8-31
编辑:Bruno Luong
2020-8-31
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
2020-8-29
See the FAQ and adapt as needed.
2 个评论
the cyclist
2020-8-29
@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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


