Vertical and horizontal line standard equations
6 ビュー (過去 30 日間)
古いコメントを表示
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/631200/image.png)
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)])
0 件のコメント
採用された回答
Star Strider
2021 年 5 月 26 日
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 = 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)])
%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
%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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!