How to draw a vector/line in a mesh

38 visualizaciones (últimos 30 días)
Lama Hamadeh
Lama Hamadeh el 16 de Mzo. de 2021
Comentada: Lama Hamadeh el 19 de Mzo. de 2021
Hi all,
I have the follwoing mesh:
%Defining space variables
L = 4; %boundary legth
ns = 25; %number of points on S axis
np = 25; %number of points on P axis
s = linspace(0,L,ns); %boundary variable
p = linspace(-1,1,np); % direction/angle variable
[S,P] = meshgrid(s,p); %construct coordinates meshgrid
% mesh needs X,Y and Z so create z
Z = zeros(size(S));
%Visualise the grid
figure;
mesh(S,P,Z,'Marker','o','MarkerFaceColor','k','EdgeColor',"k")
axis equal tight
view(2)
xlabel('$S$','Interpreter','latex')
ylabel('$P$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
And I want to draw a line/vector starting from the bottom of the mesh upwards in a way that it's inclined to the right with a certain angle. Could you please help on how to draw such a line in the mesh and how to calculate its angle with the norm (not with the x axis).
Thanks.
Lama
  2 comentarios
darova
darova el 17 de Mzo. de 2021
Can you make a simple drawing or something? Can you show how you want to draw a vector?
Lama Hamadeh
Lama Hamadeh el 17 de Mzo. de 2021
Thanks for your reply.
For simplicity, let's suppose we have a unit length square with an initial vector v0 that makes a angle θ0 with the norm as shown in this illustrative picture:
the code for constructing the square is as follwoing:
%the main coordinates of the square
x1 = 0; y1 = 0;
x2 = 1; y2 = 0;
x3 = 1; y3 = 1;
x4 = 0; y4 = 1;
x5 = 0; y5 = 0;
%the x and y variables
x = [x1 x2 x3 x4 x5];
y = [y1 y2 y3 y4 y5];
%plotting the square
plot(x,y,'b','LineWidth',2)
axis([-0.2 1.2 -0.2 1.2])
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
But how can I create the line/vector, v0, based on its position and the angle that it makes with the norm. I suppose that based on its position and the angle, I can know the equation of the line.
Thanks.

Iniciar sesión para comentar.

Respuesta aceptada

darova
darova el 18 de Mzo. de 2021
Thanks for the drawing, i understand
Here is a way:
[x,y] = meshgrid([0 1]); % coordinates for square
t0 = 45;
[u0,v0] = pol2cart(t0,1); % angle and radius
surf(x,y,x*0,'facecolor','none')
quiver(0.5,0,u0,v0) % start position and components of vector
axis([-1 2 -1 2])
  2 comentarios
Lama Hamadeh
Lama Hamadeh el 19 de Mzo. de 2021
Many thanks for your help!
A question, though, I tried to code the Polar -> Cartesian conversion as follwoing:
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%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
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%---------
%From Polar to Cartesian
theta_deg = 45; %angle from the x-axis in radians
theta_rad = theta_deg*pi/180; %angle in degress
r = 0.7071; %the length of the vector
x = r*cos(theta_rad); %the x projection of the vector
y = r*sin(theta_rad); %the y projection of the vector
quiver(0,0,x,y,'b','LineWidth',2) %show the vector on the mesh
%---------
Although the above code should result a vector that has x=0.5 and y=0.5, but as shown below, they are x=0.45 and y=0.45. That's puzzling!
Lama Hamadeh
Lama Hamadeh el 19 de Mzo. de 2021
I just found out that it relates to scaling factor which is by default embedded in quiver MAtlab funciton. When scale is 'off' or 0, such as quiver(X,Y,U,V,'off'), then automatic scaling is disabled and the arrow reaches the specified x and y correctly.
Thansk for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Analysis and Verification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by