I have a problem involving textbook MATLAB and use of handles and functions
I’m having problems figuring out how to get some MATLAB code from a textbook to run. The code follows. The only original code statements are the first seven lines. I was trying to provide some basic data to get the code to draw a plane. I did have to add some "end" statements to the code, including the end at the bottom. I would like to get this code to draw an image and move it around a screen, but this code is supposed to do that. I need to learn how to get the code to run. I have a number of additional (or perhaps core) questions.
Is "fred" an appropriate handle reference?
Do I need to define a handle reference?
How do I call drawPlaneBody?
Can you help me to get this code (from a textbook) to work?
Thank you most sincerely.
pn = 20;
pe=0;
pd=0;
phi =0;
theta=0;
psi=0;
handle="fred";
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,:),’Erasemode’, mode);
else
set(handle, ‘XData’, XYZ(1,:), ‘YData’,XYZ(2,:), ‘ZData’,XYZ(3,:));
drawnow
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
coordinates
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 2;%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;
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end
endI’m having problems figuring out how to get some MATLAB code from a textbook to run. The code follows. The only original code statements are the first seven lines. I was trying to provide some basic data to get the code to draw a plane. I did have to add some "end" statements to the code, including the end at the bottom. I would like to get this code to draw an image and move it around a screen, but this code is supposed to do that. I need to learn how to get the code to run. I have a number of additional (or perhaps core) questions.
Is "fred" an appropriate handle reference?
Do I need to define a handle reference?
How do I call drawPlaneBody?
Can you help me to get this code (from a textbook) to work?
Thank you most sincerely.
pn = 20;
pe=0;
pd=0;
phi =0;
theta=0;
psi=0;
handle="fred";
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,:),’Erasemode’, mode);
else
set(handle, ‘XData’, XYZ(1,:), ‘YData’,XYZ(2,:), ‘ZData’,XYZ(3,:));
drawnow
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
coordinates
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 2;%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;
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end
end I’m having problems figuring out how to get some MATLAB code from a textbook to run. The code follows. The only original code statements are the first seven lines. I was trying to provide some basic data to get the code to draw a plane. I did have to add some "end" statements to the code, including the end at the bottom. I would like to get this code to draw an image and move it around a screen, but this code is supposed to do that. I need to learn how to get the code to run. I have a number of additional (or perhaps core) questions.
Is "fred" an appropriate handle reference?
Do I need to define a handle reference?
How do I call drawPlaneBody?
Can you help me to get this code (from a textbook) to work?
Thank you most sincerely.
pn = 20;
pe=0;
pd=0;
phi =0;
theta=0;
psi=0;
handle="fred";
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,:),’Erasemode’, mode);
else
set(handle, ‘XData’, XYZ(1,:), ‘YData’,XYZ(2,:), ‘ZData’,XYZ(3,:));
drawnow
end
function XYZ = airplanepoints
%define points on the aircraft in local NED
coordinates
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 2;%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;
end
function XYZ = translate(XYZ, pn, pe, pd)
XYZ = XYZ + repmat([pn;pe;pd],1,size(XYZ,2));
end
end handle, matlab function, how to use functions MATLAB Answers — New Questions