Vertical and horizontal line standard equations

Hi all,
I am trying to use the standard equations of the form for both: horizontal and vertical lines and draw them on a mesh (see my code below). The problem is that when using this form, I can't plot them exactly on the mesh. Plus, the undefined gradient for the vertical line doesn't allow plotting in it at all. Is there a way around this? I need this from in specific becasue at some point I want to use the line equations' coeffiecients to find out the intersection point between either of them with other lines.
any help would be apprecited.
Thanks.
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%Step 2: construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
box on
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%horizontal line equation
%coeffieicnts
a = 1 % contributes to y intercept
A = 0;
B = 1;
C = a;
% equation
Nh = (-C/B) + (-A/B) * s; %(-C/B) is the intercept and (-A/B) is the gradient
%plotting the line on the grid
plot(s,Nh,'m','LineWidth',2)
axis([min(s) max(s) min(s) max(s)])
%vertical line equation
%coeffieicnts
b = 1; %contributes to x intercept
A = 1;
B = 0;
C = b;
% equation
Nv = (-C/B) + (-A/B) * s; %(-C/B) is the intercept and (-A/B) is the gradient
%plotting the line on the grid
plot(s,Nv,'b','LineWidth',2)
axis([min(s) max(s) min(s) max(s)])

 Accepted Answer

The problem is that the axes are set to be limited to [0 1] while the lines are all negative. (The second line was originally completely NaN or Inf because of the zeros in the numerator and denominator. I changed the constants so that they woulld be finite.)
Also, to plot them on a surf or mesh plot, it is always best to use plot3 rather than plot, and define the z-value as well, even if it is uniformly 0.
The finite lines plot, however they were not visible on the axes because of these problems.
I changed the line equations slightly and used plot3 to demonstrate that they plot correctly, however they may not be in the positions you want them to be. Since I am not certain what those positions are, I defer to you to correct them.
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%Step 2: construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
box on
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%horizontal line equation
%coeffieicnts
a = 1 % contributes to y intercept
a = 1
A = 0;
B = 1;
C = -a;
% equation
Nh = (-C/B) + (-A/B) * s %(-C/B) is the intercept and (-A/B) is the gradient
Nh = 1×25
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
%plotting the line on the grid
plot3(s,Nh,zeros(size(Nh)),'m','LineWidth',2)
% axis([min(s) max(s) min(s) max(s)])
%vertical line equation
%coeffieicnts
b = 1; %contributes to x intercept
A = 1;
B = 1;
C = -b;
% equation
Nv = (-C/B) + (-A/B) * s %(-C/B) is the intercept and (-A/B) is the gradient
Nv = 1×25
1.0000 0.9583 0.9167 0.8750 0.8333 0.7917 0.7500 0.7083 0.6667 0.6250 0.5833 0.5417 0.5000 0.4583 0.4167 0.3750 0.3333 0.2917 0.2500 0.2083 0.1667 0.1250 0.0833 0.0417 0
%plotting the line on the grid
plot3(s,Nv,zeros(size(Nv)), 'b','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
.

4 Comments

Many thanks for your reply. plot3 does a better job indeed. The horizontal line works fine, but not the vertical. I understand that putting B=0 makes the resulting line to have underfined values Nan, but this is the equation of the vertical line. It should have B=0 as following:
%line equation os the form: Ax+By+C=0
%vertical norm (of the form x=b)
%coefficients
b = 1; %contributes to y intercept
A = 1;
B = 0; %this should be zero to have a vertical line
C = -b;
%equation
Nv = (-C/B) + (-A/B) * s; %(-C/B) is intercept and (-A/B) is gradient
%plotting the norm of the grid
plot3(s,Nv,zeros(size(Nv)),'y','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
I'm wondering if there is a way around this so I can compute the vertical line equation and use it afterwards in the code.
Thanks.
If the vertical line goes along the left side of the plot (from (0,0) to (0,1), the easiest way would be to plot it as such, rather than attempting to produce an expression for it:
% plot3([0 0],[0 1],zeros(size(Nv)),'y','LineWidth',2)
so using that instead produces —
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%Step 2: construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
box on
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%horizontal line equation
%coeffieicnts
a = 1 % contributes to y intercept
a = 1
A = 0;
B = 1;
C = -a;
% equation
Nh = (-C/B) + (-A/B) * s; %(-C/B) is the intercept and (-A/B) is the gradient
%plotting the line on the grid
plot3(s,Nh,zeros(size(Nh)),'m','LineWidth',2)
% axis([min(s) max(s) min(s) max(s)])
%line equation os the form: Ax+By+C=0
%vertical norm (of the form x=b)
%coefficients
b = 1; %contributes to y intercept
A = 1;
B = 0; %this should be zero to have a vertical line
C = -b;
%equation
% % % — THE PROBLEM HERE AGAIN IS THAT 'Nv' IS NON-FINITE BECAUSE IT HAS AN INFINITE SLOPE,
% % % AS CAN BE SEEN BY DISPLAYING IT:
Nv = (-C/B) + (-A/B) * s %(-C/B) is intercept and (-A/B) is gradient
Nv = 1×25
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
%plotting the norm of the grid
plot3(s,Nv,zeros(size(Nv)),'y','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
%plotting the line on the grid
% % % PLOTTING THE VERTICAL LINE DIRECTLY BY SPECIFYING THE BEGINNING AND ENDING POINTS IS
% % % THE ONLY REALISTIC OPTION:
plot3([1 1],[0 1],[0 0],'y','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
EDIT — (27 May 2021 at 11:54)
The only way to use the equation to calculate the line is to have the slope be high but not infinite.
Adding a very small value (such as eps) to ‘B’ will do that —
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%Step 2: construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
box on
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%horizontal line equation
%coeffieicnts
a = 1 % contributes to y intercept
a = 1
A = 0;
B = 1;
C = -a;
% equation
Nh = (-C/B) + (-A/B) * s; %(-C/B) is the intercept and (-A/B) is the gradient
%plotting the line on the grid
plot3(s,Nh,zeros(size(Nh)),'m','LineWidth',2)
% axis([min(s) max(s) min(s) max(s)])
%line equation os the form: Ax+By+C=0
%vertical norm (of the form x=b)
%coefficients
b = 1; %contributes to y intercept
A = 1;
B = 0; %this should be zero to have a vertical line
B = B+eps;
C = -b;
%equation
% % % — THE PROBLEM HERE AGAIN IS THAT 'Nv' IS NON-FINITE BECAUSE IT HAS AN INFINITE SLOPE,
% % % AS CAN BE SEEN BY DISPLAYING IT:
Nv = (-C/B) + (-A/B) * s %(-C/B) is intercept and (-A/B) is gradient
Nv = 1×25
1.0e+15 * 4.5036 4.3159 4.1283 3.9406 3.7530 3.5653 3.3777 3.1900 3.0024 2.8147 2.6271 2.4394 2.2518 2.0641 1.8765 1.6888 1.5012 1.3135 1.1259 0.9382 0.7506 0.5629 0.3753 0.1876 0
%plotting the norm of the grid
plot3(s,Nv,zeros(size(Nv)),'y','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
%plotting the line on the grid
% % % PLOTTING THE VERTICAL LINE DIRECTLY BY SPECIFYING THE BEGINNING AND ENDING POINTS IS
% % % THE ONLY REALISTIC OPTION:
% plot3([0 0],[0 1],[0 0],'y','LineWidth',2)
axis([min(s) max(s) min(s) max(s) -1 1])
.
That's a very smart idea indeed. I was thinking about this earlier since I need the equation of the vertical line afterward in my code, so istead of the denominator to be zero resulting into an infinite gradient, it can be very small.
Many thanks for you help and time.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!