MATLAB Answers

Vertical and horizontal line standard equations

1 view (last 30 days)
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

Star Strider
Star Strider on 26 May 2021
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

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!