I'd like to do various things, including controlling the dimensions of the plot box. What I do here with xlim, ylim, and zlim doens't work.

2 次查看(过去 30 天)
I put the following together. It runs, but it doesn't produce an image that moves on the screen. Instead, the screen moves around the image. I'd prefer the former. I try to expand the scope of the plot box using xlim, ylim, and zlim, but it doesn't work. I also have a question regarding what is being done with XYZ and XYZ'. I don't understand what is being done with the single quote mark. Can anyone help me or explain the meaning of the single quote mark? Thank you in advance.
pn=20;
pe=0;
pd=-5;
phi=0;
theta=0;
psi=0;
handle=[];
%Simulation parameters
dpn=-0.2; %Change in position.
dpsi=0.05; %Change in yaw angle.
simlength=100;%Number of Sim Steps
%Draw and update the plane's position.
for k = 1:simlength
pn = pn+dpn;
psi = psi+dpsi;
%Draw or update the plane.
handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle);
pause(0.1); %Pause to visualize the movement
end
function handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle)
%define points on plane in local NED coordintates
NED = airplanepoints;
%rotate plane by (phi; theta; psi)
NED = rotate(NED, phi, theta, psi);
%translate plane to [pn; pe; pd]
NED = translate(NED, pn, pe, pd);
%transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0 , 0;...
0, 0, -1;...
];
XYZ = R* NED';
%plot plane
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim = ([-100 100]);
ylim = ([-100 100]);
zlim = ([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
axis equal;%Maintain aspect ratio
else
set(handle, 'XData', XYZ(1,:), 'YData',XYZ(2,:), 'ZData',XYZ(3,:));
drawnow
end
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
XYZ = [...
0 0 0;%point1
-2 1 1;%point2
-2 1 -1;%point3
0 0 0;%point1
-2 -1 1;%point4
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-2 -1 1;%point4
0 0 0;%point1
-2 1 -1;%point3
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-18 0 0;%point6
-2 1 -1;%point3
-18 0 0;%point6
-2 -1 -1;%point5
-18 0 0;%point6
-2 -1 1;%point4
-18 0 0;%point6
-2 1 1;%point2
0 0 0;%point1
-5 0 0;%point7
-5 -10 0;%point8
-8 -10 0;%point9
-8 10 0;%point10
-5 10 0;%point11
-5 0 0;%point7
-15.5 0 0;%point12
-15.5 2 0;%point13
-17.5 2 0;%point14
-17.5 -2 0;%point15
-15.5 -2 0;%point16
-15.5 0 0;%point12
-18 0 0;%point6
-18 0 -3;%point17
-15.5 0 0;%point15
-18 0 0;%point16
];
end
function XYZ=rotate(XYZ,phi,theta,psi)
%define rotation matrix
R_roll = [
1, 0, 0;
0, cos(phi), -sin(phi);
0, sin(phi), cos(phi)];
R_pitch = [
cos(theta), 0, sin(theta);
0, 1, 0;
-sin(theta), 0, cos(theta)];
R_yaw = [
cos(psi), -sin(psi), 0;
sin(psi), cos(psi), 0;
0, 0, 1];
R = R_roll*R_pitch*R_yaw;
%rotate vertices
XYZ =R*XYZ';
XYZ = XYZ';
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ' + repmat([pn;pe;pd],1,size(XYZ,1));
XYZ=XYZ';
end
  3 个评论
Steven Lord
Steven Lord 2024-5-31
移动:Cris LaPierre 2024-5-31
What does "It craps out" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
DJ V
DJ V 2024-5-31
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-5-31
编辑:Cris LaPierre 2024-5-31
You are creating varlables named xlim, ylim, and zlim rather than setting the corresponding axis properties. Also, axis equal changes the axis limits, so is currently undoing what you are trying to do. Try this instead.
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim([-100 100]);
ylim([-100 100]);
zlim([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
else
  3 个评论
Cris LaPierre
Cris LaPierre 2024-5-31
First, a single quote tranposes your data.
A = 1:3
A = 1x3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A'
ans = 3x1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Your code has the same words, but your syntax is producing a completely different result. Making the recommended change to your code will fix the axis limits so that it is the plane that moves, not the background.
pn=20;
pe=0;
pd=-5;
phi=0;
theta=0;
psi=0;
handle=[];
%Simulation parameters
dpn=-0.2; %Change in position.
dpsi=0.05; %Change in yaw angle.
simlength=100;%Number of Sim Steps
%Draw and update the plane's position.
for k = 1:simlength
pn = pn+dpn;
psi = psi+dpsi;
%Draw or update the plane.
handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle);
pause(0.1); %Pause to visualize the movement
end
function handle = drawPlaneBody(pn,pe,pd,phi,theta,psi,handle)
%define points on plane in local NED coordintates
NED = airplanepoints;
%rotate plane by (phi; theta; psi)
NED = rotate(NED, phi, theta, psi);
%translate plane to [pn; pe; pd]
NED = translate(NED, pn, pe, pd);
%transform vertices from NED to XYZ
R = [...
0, 1, 0;...
1, 0 , 0;...
0, 0, -1;...
];
XYZ = R* NED';
%plot plane
if isempty(handle)
handle = plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'b');
xlim([-100 100]);
ylim([-100 100]);
zlim([0 100]);
grid on;%show grid
xlabel('X');ylabel('Y');zlabel('Z');%label axes
else
set(handle, 'XData', XYZ(1,:), 'YData',XYZ(2,:), 'ZData',XYZ(3,:));
drawnow
end
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
XYZ = [...
0 0 0;%point1
-2 1 1;%point2
-2 1 -1;%point3
0 0 0;%point1
-2 -1 1;%point4
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-2 -1 1;%point4
0 0 0;%point1
-2 1 -1;%point3
-2 -1 -1;%point5
0 0 0;%point1
-2 1 1;%point2
-18 0 0;%point6
-2 1 -1;%point3
-18 0 0;%point6
-2 -1 -1;%point5
-18 0 0;%point6
-2 -1 1;%point4
-18 0 0;%point6
-2 1 1;%point2
0 0 0;%point1
-5 0 0;%point7
-5 -10 0;%point8
-8 -10 0;%point9
-8 10 0;%point10
-5 10 0;%point11
-5 0 0;%point7
-15.5 0 0;%point12
-15.5 2 0;%point13
-17.5 2 0;%point14
-17.5 -2 0;%point15
-15.5 -2 0;%point16
-15.5 0 0;%point12
-18 0 0;%point6
-18 0 -3;%point17
-15.5 0 0;%point15
-18 0 0;%point16
];
end
function XYZ=rotate(XYZ,phi,theta,psi)
%define rotation matrix
R_roll = [
1, 0, 0;
0, cos(phi), -sin(phi);
0, sin(phi), cos(phi)];
R_pitch = [
cos(theta), 0, sin(theta);
0, 1, 0;
-sin(theta), 0, cos(theta)];
R_yaw = [
cos(psi), -sin(psi), 0;
sin(psi), cos(psi), 0;
0, 0, 1];
R = R_roll*R_pitch*R_yaw;
%rotate vertices
XYZ =R*XYZ';
XYZ = XYZ';
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ' + repmat([pn;pe;pd],1,size(XYZ,1));
XYZ=XYZ';
end
DJ V
DJ V 2024-5-31
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2024-5-31
Instead of manually modifying the coordinates, I'd consider using the hgtransform function in conjunction with makehgtform.
  1 个评论
DJ V
DJ V 2024-5-31
Thank you very much. I will be posting this again because of other problems I have encountered. It isn't fair to award a solution based on several different questions on the same code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by