How to plot stairstep between two line?

Source: Semantic Scholar
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.

 Accepted Answer

DGM
DGM on 16 May 2021
Edited: DGM on 16 May 2021
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 Comments

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
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!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!