Matlab颤抖箭头长度(Matlab quiver arrow length)

编程入门 行业动态 更新时间:2024-10-20 21:12:49
Matlab颤抖箭头长度(Matlab quiver arrow length)

我正在处理以下代码:

function myquiver clc R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(0,25,10),linspace(0,0.5,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); quiver(T,VC,1./L,S./L,'r','ShowArrowhead','off','AutoScale','off') axis([0,25,0,0.5]) shg function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

这会产生以下图像:

现在,我想要做的是根据轴使每个箭袋箭头长度相同。 我不想改变轴([0,25,0.05])。 这正是我想要的。 但是,如何缩放每个箭袋箭头以使它们在此特定图像中的长度相同?

我希望看到一个我可以使用的想法,如果我用say轴([0,10,-30,50])做另一个情节。

我希望看到完整的代码示例和图像。

谢谢。

I am working on the following code:

function myquiver clc R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(0,25,10),linspace(0,0.5,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); quiver(T,VC,1./L,S./L,'r','ShowArrowhead','off','AutoScale','off') axis([0,25,0,0.5]) shg function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

This produces the following image:

Now, what I want to do is make each of the quiver arrows the same length based on the axes. I don't want to change axis([0,25,0.05]). That is exactly what I want. But how can I scale each quiver arrow so that they are all the same length in this particular image?

I am hoping to see an idea that I can use should I do another plot with say axis([0,10,-30,50]).

I'm hoping to see a full code example and an image.

Thanks.

最满意答案

我们考虑两个坐标系。 首先,数据所在的世界坐标系 。例如1./L和S./L是世界坐标表示。 其次,我们定义的绘图坐标系使得一个距离单位是绘图窗口的宽度和高度。 即,在绘图坐标中具有单位长度的水平向量将精确地跨越整个绘图宽度,并且类似地,在绘图坐标中具有单位长度的垂直向量将精确地跨越整个绘图高度。

你似乎要问的是如何缩放每个向量,使它们在绘图坐标中都是一些固定的长度。 不难看出,对于世界坐标(x_world, y_world)定义的某些矢量(x_world, y_world)以下函数将世界坐标转换为绘图坐标。

x_plot = x_world / x_span y_plot = y_world / y_span

其中x_span和y_span是世界坐标中图的宽度和高度(分别为25和0.5 )。

我们想知道应用于x_world和y_world比例因子,以便(x_plot, y_plot)是一些固定长度。 请注意,我们必须在x和y中应用相同的缩放因子以保留原始方向 。 在数学上,这可以用等式来描述

d = sqrt((s*x_world / x_span)^2 + (s*y_plot / y_span)^2)

其中d是绘图坐标中所需的长度, s是我们需要确定的比例因子。

求解s得到的方程式

s = d/sqrt((x_world / x_span)^2 + (y_plot / y_span)^2)

这给了我们一个解决方案。

function myquiver xmin = 0; xmax = 25; ymin = 0; ymax = 0.5; R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(xmin,xmax,10),linspace(ymin,ymax,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); x_span = xmax - xmin; y_span = ymax - ymin; % Compute the scale factor so that vectors are 10% of plot window d = 0.1; X_world = 1./L; Y_world = S./L; s = d ./ sqrt((X_world / x_span).^2 + (Y_world / y_span).^2); quiver(T,VC,s.*X_world,s.*Y_world,'r','ShowArrowhead','off','AutoScale','off') axis([xmin,xmax,ymin,ymax]) % force the axis to be square axis('square'); function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

在此处输入图像描述


这里需要注意的一点是我们使用了axis('square') 。 这对于确保绘图坐标屏幕/像素坐标之间的相等缩放关系是必要的。 因为这可能不是理想的(也许我们不想要方形图!),那么我们可以通过将x_span除以x_span的纵横比(以像素为单位)来解决这个问题 。 不幸的是,在创建绘图之前我们无法获得宽高比。 为了实现通用解决方案,我们基本上有两个选择。

编写一个函数处理程序,以便在每次调整图形大小时自动重新绘制。 这是更多的工作,但一般化。 玩图直到它是所需的大小,然后查询大小,然后使用此信息重新绘制所需的图形大小。

我选择了第二种选择。 首先我绘制没有 axis('square') ,然后使用窗口播放,直到它与您发布的原始图形大小相同。 接下来,我查询了图形和纵横比的位置如下

>> get(gcf, 'Position') ans = 666 223 672 505 >> set(gca, 'Units', 'Pixels'); >> get(gca, 'Position') ans = 88.3600 56.5500 520.8000 411.5750

这告诉我们,对于此图形位置,轴的纵横比(以像素为单位)为520.8 / 411.575 。 使用这些知识,我们修改函数以绘制该非方轴的相等长度矢量,如下所示。

function myquiver xmin = 0; xmax = 25; ymin = 0; ymax = 0.5; R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(xmin,xmax,10),linspace(ymin,ymax,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); aspect = 520.8 / 411.575; x_span = (xmax - xmin) / aspect; y_span = ymax - ymin; % Compute the scale factor so that vectors are 10% of y-direction of plot window d = 0.1; X_world = 1./L; Y_world = S./L; s = d ./ sqrt((X_world / x_span).^2 + (Y_world / y_span).^2); quiver(T,VC,s.*X_world,s.*Y_world,'r','ShowArrowhead','off','AutoScale','off') axis([xmin,xmax,ymin,ymax]) % Make the figure have the desired position/size set(gcf, 'Position', [666 223 672 505]); function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

在此处输入图像描述

Let's consider two coordinate systems. First, the world coordinate system, which the data are in. For example 1./L and S./L are world coordinate representations. Second, the plot coordinate system which we define such that one unit of distance is the width and height of the plot window. I.e. a horizontal vector with unit length in plot coordinates would exactly span the entire plot width, and similarly a vertical vector with unit length in plot coordinates would exactly span the entire plot height.

What you appear to be asking is how to scale each vector so that they are all some fixed length in plot coordinates. It's not difficult to see that for some vector defined in world coordinates (x_world, y_world) the following functions transform the world coordinate to plot coordinates.

x_plot = x_world / x_span y_plot = y_world / y_span

Where x_span and y_span are the width and height of the plot in world coordinates (in your case 25 and 0.5 respectively).

We would like to know what scaling factor to apply to x_world and y_world so that (x_plot, y_plot) is some fixed length. Observe that we must apply the same scaling factor in x and y to retain the original direction. Mathematically this can be described by the equation

d = sqrt((s*x_world / x_span)^2 + (s*y_plot / y_span)^2)

where d is the desired length in plot coordinates and s is the scale factor we need to determine.

Solving the equation for s we get

s = d/sqrt((x_world / x_span)^2 + (y_plot / y_span)^2)

which gives us a solution.

function myquiver xmin = 0; xmax = 25; ymin = 0; ymax = 0.5; R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(xmin,xmax,10),linspace(ymin,ymax,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); x_span = xmax - xmin; y_span = ymax - ymin; % Compute the scale factor so that vectors are 10% of plot window d = 0.1; X_world = 1./L; Y_world = S./L; s = d ./ sqrt((X_world / x_span).^2 + (Y_world / y_span).^2); quiver(T,VC,s.*X_world,s.*Y_world,'r','ShowArrowhead','off','AutoScale','off') axis([xmin,xmax,ymin,ymax]) % force the axis to be square axis('square'); function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

enter image description here


One thing to note here is that we used axis('square'). This was necessary to ensure an equal scaling relationship between plot coordinates and screen/pixel coordinates. Since this may not be desirable (maybe we don't want a square plot!), then we can fix this by dividing x_span by the aspect ratio of the axis (in pixels). Unfortunately, we can't get the aspect ratio before the plot is created. In order to implement the generalized solution we basically have two options.

Write a function handler to automatically re-plot each time the figure is resized. This is more work but generalized. Play with the figure until it is the desired size, then query the size, then use this information to re-plot for the desired figure size.

I chose to pursue option two. First I plotted without axis('square'), then played with the window until it was about the same size as the original figure that you posted. Next, I queried the position of the figure and aspect ratio as follows

>> get(gcf, 'Position') ans = 666 223 672 505 >> set(gca, 'Units', 'Pixels'); >> get(gca, 'Position') ans = 88.3600 56.5500 520.8000 411.5750

This tells us that for this figure position the aspect ratio of the axis in pixels is 520.8 / 411.575. Using this knowledge, we modify the function to plot equal length vectors for this non-square axis as follows.

function myquiver xmin = 0; xmax = 25; ymin = 0; ymax = 0.5; R=2.3; C=1.2; V=@(t) 3*sqw(t,1,10); F=@(t,VC) (V(t)-VC)/(R*C); [T,VC]=meshgrid(linspace(xmin,xmax,10),linspace(ymin,ymax,10)); S=(3*sqw(T,1,10)-VC)/(R*C); L=sqrt(1+S.^2); aspect = 520.8 / 411.575; x_span = (xmax - xmin) / aspect; y_span = ymax - ymin; % Compute the scale factor so that vectors are 10% of y-direction of plot window d = 0.1; X_world = 1./L; Y_world = S./L; s = d ./ sqrt((X_world / x_span).^2 + (Y_world / y_span).^2); quiver(T,VC,s.*X_world,s.*Y_world,'r','ShowArrowhead','off','AutoScale','off') axis([xmin,xmax,ymin,ymax]) % Make the figure have the desired position/size set(gcf, 'Position', [666 223 672 505]); function y=sqw(t,T,d) r=mod(t,T); y=(r<d*T/100);

enter image description here

更多推荐

本文发布于:2023-08-04 12:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:箭头   长度   Matlab   length   arrow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!