matlab:沿着图形和轴线移动圆圈相等

您所在的位置:网站首页 matlab绘制直线 matlab:沿着图形和轴线移动圆圈相等

matlab:沿着图形和轴线移动圆圈相等

2023-03-14 02:24| 来源: 网络整理| 查看: 265

你好,请原谅我,如果我的英语有点生疏了.我正在尝试创建一个沿参数函数移动的圆(坐标存储在向量中).我已经写了一个用于绘制圆的函数,我知道你可以在matlab中使用轴相等命令来创建圆形并避免使用椭圆.我的问题是,当我这样做时,图形窗口相对于绘制的图形变得非常宽.任何输入都表示赞赏.

主要代码:

t = linspace(0,3); x = 30*cos(pi/4)/2*(1-exp(-0.5*t)); y = (30*sin(pi/4)/2 + 9.81/0.5^2)*(1-exp(0.5*t)) - 9.81*t/0.5; for i = 1:length(t) plot(x,y) axis equal hold on cirkel(x(i),y(i),1,1,'r') % argument #3 is the radius #4 is 1 for fill hold off pause(0.01) end

循环代码:

function cirkel(x,y,r,f,c) angle = linspace(0, 2*pi, 360); xp = x + r*cos(angle); yp = y + r*sin(angle); plot(x,y) if f == 1 && nargin == 5 fill(xp,yp,c) end

Suever.. 5

当你调用axis equal它时,x轴的一个单位与y轴的一个单位的大小相同.你看到的是什么,因为你的y值比x值的范围大得多.

解决这个问题的一种方法是查询当前轴的纵横比和x/y限制,如本答案第二部分所示.但是,更简单的方法是fill使用scatter圆形标记而不是用于绘制圆形,而不管轴的纵横比如何都是圆形的.

t = linspace(0,3); x = 30*cos(pi/4)/2*(1-exp(-0.5*t)); y = (30*sin(pi/4)/2 + 9.81/0.5^2)*(1-exp(0.5*t)) - 9.81*t/0.5; % Plot the entire curve hplot = plot(x, y); hold on; % Create a scatter plot where the area of the marker is 50. Store the handle to the plot % in the variable hscatter so we can update the position inside of the loop hscatter = scatter(x(1), y(1), 50, 'r', 'MarkerFaceColor', 'r'); for k = 1:length(t) % Update the location of the scatter plot set(hscatter, 'XData', x(k), ... % Set the X Position of the circle to x(k) 'YData', y(k)) % Set the Y Position of the circle to y(k) % Refresh the plot drawnow end

在此输入图像描述

作为旁注,最好更新现有的绘图对象而不是创建新的绘图对象.

1> Suever..:

当你调用axis equal它时,x轴的一个单位与y轴的一个单位的大小相同.你看到的是什么,因为你的y值比x值的范围大得多.

解决这个问题的一种方法是查询当前轴的纵横比和x/y限制,如本答案第二部分所示.但是,更简单的方法是fill使用scatter圆形标记而不是用于绘制圆形,而不管轴的纵横比如何都是圆形的.

t = linspace(0,3); x = 30*cos(pi/4)/2*(1-exp(-0.5*t)); y = (30*sin(pi/4)/2 + 9.81/0.5^2)*(1-exp(0.5*t)) - 9.81*t/0.5; % Plot the entire curve hplot = plot(x, y); hold on; % Create a scatter plot where the area of the marker is 50. Store the handle to the plot % in the variable hscatter so we can update the position inside of the loop hscatter = scatter(x(1), y(1), 50, 'r', 'MarkerFaceColor', 'r'); for k = 1:length(t) % Update the location of the scatter plot set(hscatter, 'XData', x(k), ... % Set the X Position of the circle to x(k) 'YData', y(k)) % Set the Y Position of the circle to y(k) % Refresh the plot drawnow end

在此输入图像描述

作为旁注,最好更新现有的绘图对象而不是创建新的绘图对象.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3