我试图在 Matlab 中模拟目标的运动,其初始 x 和 y 坐标、真实方位和速度(以 m/s 为单位)已指定。我想知道是否有一种方法可以简单地以指定的方位角绘制一条直线以显示目标所采用的路径(如下图所示)
提前致谢!
您最好的选择是依靠内置的极坐标绘图功能之一来执行此操作。我认为与您的需求最相似的是compass
. 它实质上在极坐标图上绘制了一个从中心指向一个点(以笛卡尔坐标定义)的箭头。
theta = deg2rad(130);
% Your speed in m/s
speed = 5;
hax = axes();
c = compass(hax, speed * cos(theta), speed * sin(theta));
% Change the view to orient the axes the way you've drawn
view([90 -90])
然后为了更改轴承和速度,您只需compass
使用新的轴承/速度再次调用该函数。
new_theta = deg2rad(new_angle_degrees);
c = compass(hax, new_speed * cos(new_theta), new_speed * sin(new_theta));
其他极坐标绘图选项包括polar
接受polarplot
极坐标但没有箭头的选项。如果您不喜欢极坐标图,您总是可以使用quiver
笛卡尔坐标轴(确保指定相同的坐标轴)。
编辑 根据您的反馈和要求,以下是行驶距离的极坐标图示例。
% Speed in m/s
speed = 5;
% Time in seconds
time = 1.5;
% Bearing in degrees
theta = 130;
hax = axes();
% Specify polar line from origin (0,0) to target position (bearing, distance)
hpolar = polar(hax, [0 deg2rad(theta)], [0 speed * time], '-o');
% Ensure the axis looks as you mentioned in your question
view([90 -90]);
现在要使用新的方位、速度和时间更新此图,您只需polar
再次调用指定轴即可。
hpolar = polar(hax, [0 theta], [0 speed], '-o');
我不确定我是否正确,这是我的解决方案:
figure;hold on; % Create figure
x_start = 10;% Starting position
y_start = 20;
plot(x_start+[-1 1],[y_start y_start],'k');% Plot crosshairs
plot([x_start x_start],y_start+[-1 1],'k');
angle = -(130-90)*pi/180; % Bearing angle 130° like in your graph
x_target = x_start+10*cos(angle); % Calculation of target position
y_target = y_start+10*sin(angle);
plot(x_target+[-1 1],[y_target y_target],'k');% Plot crosshairs
plot([x_target x_target],y_target+[-1 1],'k');
% Draw line between start and target
plot([x_start x_target],[y_start y_target],'g');
set(gca,'xlim',[0 30],'ylim',[0 30]); % Adjust axes
text(x_start+1,y_start,'Start'); % Write text to points
text(x_target+1,y_target,'End');