Plotting a 3D arrow using vectarrow

74 visualizzazioni (ultimi 30 giorni)
Carolin Bock
Carolin Bock il 27 Ago 2020
Modificato: Matt J il 27 Ago 2020
Hello,
I'm trying to plat a 3D arrow using the vectarrow function. I don't get any arrow messages but there is just now arrow in my plot. It would be great to get some tipps:)
My code is the following:
%50 J
[X1,Y1] = meshgrid(1:0.025:1.7,0.05:0.05:0.25);
p00_1 = 1.289;
p10_1 = -0.3693;
p01_1 = 0.7187;
p20_1 = 0.1467;
p11_1 = -1.188;
p02_1 = 1.188;
colormap(flipud(jet));
Z1 = p00_1 + p10_1.*X1 + p01_1.*Y1 + p20_1.*X1.^2 + p11_1.*X1.*Y1 + p02_1.*Y1.^2
s1 = surf(X1,Y1,Z1,'FaceColor','interp','EdgeColor', 'none')
colorbar;
hold on;
%Patch
Z2 = 0.9 + 0*X1 + 0*Y1
s2 = surf(X1,Y1,Z2,'FaceColor','none','FaceAlpha',1.0,'EdgeColor','none')
v = [1.7 0.05 0.9; 1.7 0.1689 0.9; 1 0.05 0.9; 1.3909 0.25 0.9 ; 1 0.25 0.9; 1 0.05 0.9];
f = [1 2 3 4 5 6];
p = patch('Faces',f,'Vertices',v,'FaceColor',[17 17 17]/30,'FaceAlpha',1.0,'EdgeColor','k','LineWidth',0.5);
zdiff = Z1 - Z2;
C = contours(X1, Y1, zdiff, [0 0]);
% Extract the x- and y-locations from the contour matrix C.
xL = C(1, 2:end);
yL = C(2, 2:end);
% Interpolate on the first surface to find z-locations for the intersection line.
zL = interp2(X1, Y1, Z1, xL, yL);
% Visualize the line.
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 1.0);
% Label axes
xlabel( 'f_{u}/f_{y}');
ylabel([char(949) '_{u}']);
zlabel( 'F_{max}/F_{soll}');
xlim([1 1.7])
ylim([0.05 0.25])
zlim([0.0 1.1])
grid on
title('S355 LP=0 KV=100J')
v = [0.9 0.9]
%contour(X1,Y1,Z1,v)
M1 = contour(X1,Y1,Z1,v,'red','LineWidth',2.0)
%Achsenrichtung
view(axes1,[124.8 23.8]);
grid(axes1,'on');
hold(axes1,'off');
set(gca,'xticklabel',num2str(get(gca,'xtick')','%.2f'))
set(gca,'yticklabel',num2str(get(gca,'ytick')','%.2f'))
set(gca,'zticklabel',num2str(get(gca,'ztick')','%.2f')
function vectarrow(p0,p1)
%Arrowline 3-D vector plot.
% vectarrow(p0,p1) plots a line vector with arrow pointing from point p0
% to point p1. The function can plot both 2D and 3D vector with arrow
% depending on the dimension of the input
%
% Example:
% 3D vector
p0 = [1.7 0.16 0.9]; % Coordinate of the first point p0
p1 = [1.7 0.16 0.0]; % Coordinate of the second point p1
vectarrow(p0,p1)
if max(size(p0))==3
if max(size(p1))==3
x0 = p0(1);
y0 = p0(2);
z0 = p0(3);
x1 = p1(1);
y1 = p1(2);
z1 = p1(3);
plot3([x0;x1],[y0;y1],[z0;z1]); % Draw a line between p0 and p1
p = p1-p0;
alpha = 0.1; % Size of arrow head relative to the length of the vector
beta = 0.1; % Width of the base of the arrow head relative to the length
hu = [x1-alpha*(p(1)+beta*(p(2)+eps)); x1; x1-alpha*(p(1)-beta*(p(2)+eps))];
hv = [y1-alpha*(p(2)-beta*(p(1)+eps)); y1; y1-alpha*(p(2)+beta*(p(1)+eps))];
hw = [z1-alpha*p(3);z1;z1-alpha*p(3)];
hold on
plot3(hu(:),hv(:),hw(:)) % Plot arrow head
grid on
xlabel('x')
ylabel('y')
zlabel('z')
hold off
else
error('p0 and p1 must have the same dimension')
end
elseif max(size(p0))==2
if max(size(p1))==2
x0 = p0(1);
y0 = p0(2);
x1 = p1(1);
y1 = p1(2);
plot([x0;x1],[y0;y1]); % Draw a line between p0 and p1
p = p1-p0;
alpha = 0.1; % Size of arrow head relative to the length of the vector
beta = 0.1; % Width of the base of the arrow head relative to the length
hu = [x1-alpha*(p(1)+beta*(p(2)+eps)); x1; x1-alpha*(p(1)-beta*(p(2)+eps))];
hv = [y1-alpha*(p(2)-beta*(p(1)+eps)); y1; y1-alpha*(p(2)+beta*(p(1)+eps))];
hold on
plot(hu(:),hv(:)) % Plot arrow head
grid on
xlabel('x')
ylabel('y')
hold off
else
error('p0 and p1 must have the same dimension')
end
else
error('this function only accepts 2D or 3D vector')
end
end

Risposta accettata

Matt J
Matt J il 27 Ago 2020
It might be easier just to use this File Exchange submission:
  4 Commenti
Carolin Bock
Carolin Bock il 27 Ago 2020
I'm sorry, but I'm quite knew to Matlab and I've never used a function before. This doesn't work for me:
function hn=arrow3(p1,p2,s,w,h,ip,alpha,beta)
arrow3([1.7 0.16 0.9],[1.7 0.16 0.0])
end
Matt J
Matt J il 27 Ago 2020
Modificato: Matt J il 27 Ago 2020
As long as you download the .m file from the link I gave you to somewhere Matlab can see it, it will work like any of the other Matlab commands that you have been using. Here is my modification of your code:
[X1,Y1] = meshgrid(1:0.025:1.7,0.05:0.05:0.25);
p00_1 = 1.289;
p10_1 = -0.3693;
p01_1 = 0.7187;
p20_1 = 0.1467;
p11_1 = -1.188;
p02_1 = 1.188;
colormap(flipud(jet));
Z1 = p00_1 + p10_1.*X1 + p01_1.*Y1 + p20_1.*X1.^2 + p11_1.*X1.*Y1 + p02_1.*Y1.^2
s1 = surf(X1,Y1,Z1,'FaceColor','interp','EdgeColor', 'none')
colorbar;
hold on;
%Patch
Z2 = 0.9 + 0*X1 + 0*Y1
s2 = surf(X1,Y1,Z2,'FaceColor','none','FaceAlpha',1.0,'EdgeColor','none')
v = [1.7 0.05 0.9; 1.7 0.1689 0.9; 1 0.05 0.9; 1.3909 0.25 0.9 ; 1 0.25 0.9; 1 0.05 0.9];
f = [1 2 3 4 5 6];
p = patch('Faces',f,'Vertices',v,'FaceColor',[17 17 17]/30,'FaceAlpha',1.0,'EdgeColor','k','LineWidth',0.5);
zdiff = Z1 - Z2;
C = contours(X1, Y1, zdiff, [0 0]);
% Extract the x- and y-locations from the contour matrix C.
xL = C(1, 2:end);
yL = C(2, 2:end);
% Interpolate on the first surface to find z-locations for the intersection line.
zL = interp2(X1, Y1, Z1, xL, yL);
% Visualize the line.
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 1.0);
% Label axes
xlabel( 'f_{u}/f_{y}');
ylabel([char(949) '_{u}']);
zlabel( 'F_{max}/F_{soll}');
xlim([1 1.7])
ylim([0.05 0.25])
zlim([0.0 1.1])
grid on
title('S355 LP=0 KV=100J')
v = [0.9 0.9]
%contour(X1,Y1,Z1,v)
M1 = contour(X1,Y1,Z1,v,'red','LineWidth',2.0)
%Achsenrichtung
set(gca,'xticklabel',num2str(get(gca,'xtick')','%.2f'))
set(gca,'yticklabel',num2str(get(gca,'ytick')','%.2f'))
set(gca,'zticklabel',num2str(get(gca,'ztick')','%.2f'))
arrow3( [1.7 0.16 0.9] , [1.7 0.16 0.0] ) %<----- ADDED by Matt J
and here is what I got:

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by