How to set multiple limits for multiple lines in a single graph with given data

I currently have a graph that has 2 sloped lines. I cannot simply set the boundaries of X because the data was given in matrix El. I want to have a line go from X = 9 to 13 and another line from X = 13 to 16. El is a matrix of 2x1601 (2 is for 2 lines) and I just want to keep the portion between those limits. Here is the code (may be a bit long)
I tried linspace so that I could even plot it, but I see now that it just squishes the values of El into the desired limits, giving me the wrong line.
I am trying to automatically create graphs (pourbaix diagrams) that can be pretty complex.
This is what I want:
This is what I'm getting:
Basically, the code is set up to calculate the slope of those 2 lines, where it will then calculate the intersection of them. This intersection is then set as a lower limit for the line that goes from 13 to 16 (the line with a higher slope). So I want the line with the lesser slope to be in the range of X = 9-13
%setting up sloped lines
s1a = El(1,:);
s2a = El(2,:);
ab1 = linspace(b,a,length(s1a));
ab2 = linspace(a,mx,length(s2a));
s1 = diff(s1a)/diff(ab1)
s2 = diff(s2a)/diff(ab1)
%finding intersection of the 2 lines so boundary can be found for vert line
[x1,y1] = polyxpoly(pH,s1a,pH,s2a);
if s1 > s2
plot(ab2,s1a);
plot(ab1,s2a);
else
plot(ab1,s1a);
plot(ab2,s2a);
end

6 Comments

Yes, the first is what I want:
and as you can see, the lines are "squished"
I'm not sure if I can do that, when I say the data is given, I mean that the values were previously calculated in a loop that was then stored into the 2x1601 matrix called El, which is what I'm trying to plot. This is the overall code the calcs are in a for loop and then stored into El:
El was preallocated.
El = ones(1,1601);
cc = 1;
cb = 1;
cc = 1;
pH = 0:0.01:16
E = -2:0.01:2
for %(this loop will store the data into matrix El when the else statement is triggered)
(some calculations and user inputs)
%calculations for vertical, horizontal, or sloped lines
if n == 0
pH0 = -g01/(C*m); %pH0 for the rxn, which is independent of charge
C1 = 1/m *(logk); %constant of ions and logK
pHa(ca,:) = pH0 - C1; %value that needs to be recorded
plot(pHa(ca,:)*ones(size(E)),E,'LineWidth',2); %since pH is constant, we must expand pH to have the same size as E
ca = ca + 1;
elseif m == 0
Eh1(cb,:) = E0 + (CF/n)*logk;
plot(pH, Eh1(cb,:)*ones(size(pH)),'LineWidth',2);
cb = cb + 1;
else
C2 = E0 - CF/n*logk;
El(cc,:) = -m/n*CF*pH + C2;
plot(pH, El(cc,:),'LineWidth',2);
cc = cc + 1;
end
%at the end of the loop, the program will then attempt to construct
%boundaries
end
%The basic outline of the graph is generated, where the automatically
%constructed graph should form
hold off
figure
plot(pH,h,'k--', pH, o ,'k--');
axis([0 16 -2 2]);
xlabel('pH');
ylabel('E (V)');
hold on
%the recorded arrays will then be used to find the boundaries of the graphs
%and it should automatically generate these no matter the order of inputted
%equations
%setting up the horizontal line
a = max(pHa); %the max pH will act as a starting point for the line with the highest slope
b = min(pHa); %starting point of line with lower slope
mx = 16; %graph max window
u = 0:0.01:b; %range for horizonatal line
bo = a:0.01:16; %range for last domain
yb = Eh1*ones(size(u)); %making u,yb plottable
plot(u,yb);
%setting up sloped lines
p = b:0.01:a; %boundaries for lesser sloped lines
s1a = El(1,:); %the 1st row of the lines
s2a = El(2,:); %2nd row
ab1 = linspace(b,a,length(s1a)); %attempt to match sizes of data, doesn't work
ab2 = linspace(a,mx,length(s2a));
%slopes of inputted lines
s1 = diff(s1a)/diff(ab1);
s2 = diff(s2a)/diff(ab1);
%finding intersection of the 2 lines so boundary can be found for vert line
[x1,y1] = polyxpoly(pH,s1a,pH,s2a);
%this statement will check the slopes of the lines and determine what
%domain it should fall under (ranges of pH)
if s1 > s2
plot(ab2,s1a,'LineWidth',2); %if slope1 > slope2, then slope 1 will be placed in the outer region
plot(ab1,s2a,'LineWidth',2);
else
plot(ab1,s1a,'LineWidth',2);
plot(ab2,s2a,'LineWidth',2);
end
Im completely lost. There is too much information for me. Can you simplify the question? Maybe another picture?
The program I'm writing will automatically construct 2 graphs. Heres' the first:
The second is the "polished" version of the first graph, which is automatically found after determining where the line stops. I want a rough version of this:
but instead I get this:
The first graph is made from user inputs, and the second is an attempt at getting the proper graphs. I just simply want to take the data from the for loop, and plot it so that it looks like the 2nd picture. But I don't know how to automatically set the limits of the lines from the calculated data.

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 27 Apr 2020

Commented:

on 27 Apr 2020

Community Treasure Hunt

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

Start Hunting!