How to plot stairstep between two line?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천

Hello everyone. I currently writing a codes for EXIT chart. I've created the curves, and now i want to create the stairstep between two curves. How to plot it so the stairs doesn't cross the other curves, and what is the method to do that? Thank you.
채택된 답변
I've never had to do this before, but this is what I came up with.
% just some similarly-shaped curves
x = linspace(0.1,1,10);
a = x.^0.2;
b = x.^0.3;
plot(x,a); hold on
plot(x,b,'k:');
xs = x(1);
ys = a(1);
n = 1;
while ~isnan(xs(n)) && n<100
xs(n+1) = interp1(b,x,ys(n));
ys(n+1) = interp1(x,b,xs(n+1));
ys(n+2) = interp1(x,a,xs(n+1));
xs(n+2) = interp1(a,x,ys(n+2));
n = n+2;
end
% if loop walks off the end of a non-converging dataset
xs = xs(~isnan(xs));
ys = ys(~isnan(ys));
% if the curves don't converge, bring the line to the x-limit
if xs(end) < x(end)
xs = [xs x(end)];
ys = [ys ys(end)];
end
plot(xs,ys,'b')
If the curves always converge, then some of this could be removed. Note the use of interp1 makes sure the plots touch even if the resolution is low. If the goal here is to count the number of steps or something, you'll have to figure that out. I just picked some arbitrary limit of 100.

댓글 수: 7
Hello DGM, thank you for the answer. Seems like your codes above have three different input, but mine have four different input. Lets say it's:
plot(x,a); hold on
plot(y,b,'k:');
What should i modify from your codes above? Thank you
Jenjen Ahmad Zaeni
2021년 5월 16일
편집: Jenjen Ahmad Zaeni
2021년 5월 16일
something like this
p = linspace(0, 1, 100);
der_lambda = ((3/10)*p+(4/10)*p.^3)
q = 0.5.*der_lambda
q2 = linspace(0, 1, 100);
der_omega=(1-q2).^3
p2 = 1-der_omega
iavnd=1-p %x1
ievnd=1-q %y1
iecnd=1-p2 %x2
iacnd=1-q2 %y2
axis([0 1 0 1])
plot(iavnd,ievnd,'linewidth',1.5,'color','red','LineSmoothing','on')
hold on
plot(iecnd,iacnd,'linewidth',1.5,'color','black','LineSmoothing','on')
grid on
You can adapt it like so
p = linspace(0, 1, 100);
der_lambda = ((3/10)*p+(4/10)*p.^3)
q = 0.5.*der_lambda
q2 = linspace(0, 1, 100);
der_omega=(1-q2).^3
p2 = 1-der_omega
iavnd=1-p %x1
ievnd=1-q %y1
iecnd=1-p2 %x2
iacnd=1-q2 %y2
xs = iavnd(end);
ys = ievnd(end);
n = 1;
while ~isnan(xs(n)) && n<100
xs(n+1) = interp1(iacnd,iecnd,ys(n));
ys(n+1) = interp1(iecnd,iacnd,xs(n+1));
ys(n+2) = interp1(iavnd,ievnd,xs(n+1));
xs(n+2) = interp1(ievnd,iavnd,ys(n+2));
n = n+2;
end
axis([0 1 0 1])
plot(iavnd,ievnd,'linewidth',1.5,'color','red','LineSmoothing','on')
hold on
plot(iecnd,iacnd,'linewidth',1.5,'color','black','LineSmoothing','on')
grid on
plot(xs,ys,'b')

Thank you very much! It works perfectly with any other curves.
Hello again DGM. Sorry, but i have some problem here. I change the curve equation like this:
der_lambda = ((66*p)+(147*p.^2)+(72*p.^3))/285;
der_omega = ((62*(1-q2).^61)+(68*(1-q2).^67)+(77*(1-q2).^76)+(78*(1-q2).^77))/285;
And this is my program
p = linspace(0, 1, 1000);
der_lambda = ((66*p)+(147*p.^2)+(72*p.^3))/285;
q = 0.032.*der_lambda;
q2 = linspace(0, 1, 1000);
der_omega=((62*(1-q2).^61)+(68*(1-q2).^67)+(77*(1-q2).^76)+(78*(1-q2).^77))/285;
p2 = 1-der_omega;
iavnd=1-p; %x1
ievnd=1-q; %y1
iecnd=1-p2; %x2
iacnd=1-q2; %y2
xs = iavnd(end);
ys = ievnd(end);
n = 1;
while ~isnan(xs(n)) && n<100
xs(n+1) = interp1(iacnd,iecnd,ys(n));
ys(n+1) = interp1(iecnd,iacnd,xs(n+1));
ys(n+2) = interp1(iavnd,ievnd,xs(n+1));
xs(n+2) = interp1(ievnd,iavnd,ys(n+2));
n = n+2;
end
plot(iavnd,ievnd,'linewidth',1.5,'color','red','LineSmoothing','on')
hold on
plot(iecnd,iacnd,'linewidth',1.5,'color','black','LineSmoothing','on')
hold on
plot(xs,ys,'linewidth',2,'color','blue','LineSmoothing','off')
hold off
grid on
xlabel('I_{A,VND}, I_{E,CND}');
ylabel('I_{E,VND}, I_{A,CND}');
title('EXIT Chart','fontweight','bold','fontsize',12);
axis([0 1 0.9 1])
And here is the plot without xs and ys:

When i run the code, there are error like this:
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 183)
F = griddedInterpolant(X,V,method);
Error in exit_fix (line 22)
ys(n+1) = interp1(iecnd,iacnd,xs(n+1));
From the image above, it appears that from 0 to 0.9, has the value of 0. I've plot a tight plot before this and your codes works perfectly, but right now seems have an error. I don't really understand but i think it is because the plot has much zeros before it curved. Do you know how to fix it? Thank you so much for your help.
You're right. The region where iecnd is zero corresponds to a vertical region in the curve. The interpolation doesn't work there. It's outside the plot area, so it can just be trimmed off.
p = linspace(0, 1, 1000);
der_lambda = ((66*p)+(147*p.^2)+(72*p.^3))/285;
q = 0.032.*der_lambda;
q2 = linspace(0, 1, 1000);
der_omega=((62*(1-q2).^61)+(68*(1-q2).^67)+(77*(1-q2).^76)+(78*(1-q2).^77))/285;
p2 = 1-der_omega;
iavnd=1-p; %x1
ievnd=1-q; %y1
iecnd=1-p2; %x2
iacnd=1-q2; %y2
% trim vectors where curve is vertical (it's outside the plot region anyway)
% if you need these variables for something else later, just make a copy
mk = iacnd>=0.85;
iecnd = iecnd(mk);
iacnd = iacnd(mk);
xs = iavnd(end);
ys = ievnd(end);
n = 1;
while ~isnan(xs(n)) && n<100
xs(n+1) = interp1(iacnd,iecnd,ys(n));
ys(n+1) = interp1(iecnd,iacnd,xs(n+1));
ys(n+2) = interp1(iavnd,ievnd,xs(n+1));
xs(n+2) = interp1(ievnd,iavnd,ys(n+2));
n = n+2;
end
plot(iavnd,ievnd,'linewidth',1.5,'color','red','LineSmoothing','on')
hold on
plot(iecnd,iacnd,'linewidth',1.5,'color','black','LineSmoothing','on')
hold on
plot(xs,ys,'linewidth',2,'color','blue','LineSmoothing','off')
hold off
grid on
xlabel('I_{A,VND}, I_{E,CND}');
ylabel('I_{E,VND}, I_{A,CND}');
title('EXIT Chart','fontweight','bold','fontsize',12);
axis([0 1 0.9 1])
It works. Thank you very much again!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
