在 MATLAB 中将 2D 向量的 3D 演化绘制为带状

发布于 2024-09-28 04:00:04 字数 195 浏览 1 评论 0原文

我想绘制二维矢量的幅度和方向如何随时间变化的图。为此,我想创建一个让人想起规范 E & 的图表。您可能还记得电学和磁学入门课程中的 B 场图。

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

I would like to plot how the amplitude and orientation of a 2D vector evolves over time. To do this I would like to create a graph reminiscent of the canonical E & B field graphs you may recall from an introductory electricity and magnetism class.

Specifically, I would like to connect my 2D vector points with a ribbon, so that they are easy to see. Is there a simple way to do this in MATLAB? quiver3 is pretty close, but it lacks the ribbon. Perhaps some sort of parametric surface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-10-05 04:00:04

您可以使用绘图函数 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

这是结果图:

替代文字

You can use the plotting functions FILL3 and QUIVER3 to do something like this:

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

And here's the resulting plot:

alt text

々眼睛长脚气 2024-10-05 04:00:04

这是一个在 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{:});

here's a solution that draws a ribbon between any two lines in 3D space. you can plot your quiver over it & adjust the opacity using 'FaceAlpha' as in gnovice's solution

To make the function clearer, I am first posting it without error-checking and resizing functions (which make up most of the body of the function & aren't particularly interesting)

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{:});

use this error-checking version in your actual code:

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{:});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文