繁体   English   中英

在MATLAB中绘制3D矢量在3D中的演变

[英]Plotting evolution of 2D vector in 3D as a ribbon in MATLAB

我想绘制2D矢量的幅度和方向如何随时间演变。 要做到这一点,我想创建一个让人想起规范的E&B场图的图表,您可以从介绍性的电力和磁力课程中回忆起这些图表。

具体来说,我想用带子连接我的2D矢量点,这样它们很容易看到。 在MATLAB中有一个简单的方法吗? quiver3非常接近,但缺少功能区。 也许是某种参数曲面?

您可以使用绘图功能FILL3QUIVER3执行以下操作:

x = linspace(0,4*pi,30);  %# Create some x data
y1 = sin(x);              %# Create wave 1
y2 = sin(x-pi);           %# Create wave 2
u = zeros(size(x));       %# Create a vector of zeroes

hRibbon1 = fill3(x,y1,u,'r');     %# Plot wave 1 and fill underneath with color
set(hRibbon1,'EdgeColor','r',...  %# Change the edge color and
             'FaceAlpha',0.5);    %#   make the colored patch transparent
hold on;                          %# Add to the existing plot
quiver3(x,u,u,u,y1,u,0,'r');      %# Plot the arrows

hRibbon2 = fill3(x,u,y2,'b');     %# Plot wave 2 and fill underneath with color
set(hRibbon2,'EdgeColor','b',...  %# Change the edge color and
             'FaceAlpha',0.5);    %#   make the colored patch transparent
quiver3(x,u,u,u,u,y2,0,'b');      %# Plot the arrows
axis equal;                       %# Use equal axis scaling

这是由此产生的情节:

替代文字

这是一个在3D空间中的任意两条线之间绘制色带的解决方案。 你可以用它绘制你的箭袋并使用'FaceAlpha'调整不透明度,就像在gnovice的解决方案中一样

为了使函数更清晰,我首先发布它而没有错误检查和调整函数大小(它构成函数体的大部分并且不是特别有趣)

function h = filledRibbon (x,y,z,u,v,w,c, varargin)
%function filledRibbon (x,y,z,u,v,w,c, varargin)
%
%plots a ribbon spanning the area between the lines x,y,z and x+u,y+v,z+w
%in the color c
%varargin is passed directly to patch
%returns a handle to the patch graphic created

%make up a set of regions that span the space between the lines

xr = [x(1:end-1); x(1:end-1) + u(1:end-1); x(2:end) + u(2:end); x(2:end)];
yr = [y(1:end-1); y(1:end-1) + v(1:end-1); y(2:end) + v(2:end); y(2:end)];
zr = [z(1:end-1); z(1:end-1) + w(1:end-1); z(2:end) + w(2:end); z(2:end)];

%plot the regions with no edges
h = patch(xr,yr,zr,c, 'LineStyle','none', varargin{:});

在您的实际代码中使用此错误检查版本:

function h = filledRibbon (x,y,z,u,v,w,c, varargin)
%function filledRibbon (x,y,z,u,v,w,c, varargin)
%
%plots a ribbon spanning the area between the lines x,y,z and x+u,y+v,z+w
%in the color c
%varargin is passed directly to patch
%returns a handle to the patch graphic created


if ~exist('w', 'var') || isempty(w)
    w = 0;
end
if ~exist('u', 'var') || isempty(u)
    u = 0;
end
if ~exist('v', 'var') || isempty(v)
    v = 0;
end
if ~exist('c', 'var') || isempty(c)
    c = 'b';
end


%make all vectors 1xN 
x = reshape(x,1,[]);
y = reshape(y,1,[]);
z = reshape(z,1,[]);

%if any offsets are scalar, expand to a vector
if all(size(u) == 1)
    u = repmat(u, size(x));
end

if all(size(v) == 1)
    v = repmat(v, size(x));
end
if all(size(w) == 1)
    w = repmat(w, size(x));
end

%make up a set of regions that span the space between the lines

xr = [x(1:end-1); x(1:end-1) + u(1:end-1); x(2:end) + u(2:end); x(2:end)];
yr = [y(1:end-1); y(1:end-1) + v(1:end-1); y(2:end) + v(2:end); y(2:end)];
zr = [z(1:end-1); z(1:end-1) + w(1:end-1); z(2:end) + w(2:end); z(2:end)];

%plot the regions with no edges
h = patch(xr,yr,zr,c, 'LineStyle','none', varargin{:});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM