Lab 2 - Part 1 - Visualizing Models in Matlab
Getting Started
Let's start by creating an empty script and we'll start with the basic cleaning commands. Add these to your script.
clear deletes all of the variables in your workspace - this makes sure that every time we run our code, we're always using the data from this run of the script, and not a previous run.
clc deletes all of the old messages in the console or command window. This makes sure that the only messages we see on our command window come from this run of the script, and not from a previous run.
close all closes all of the previous plots and graph windows. This way you know that the plots you see generated are new, and not old data.
Let's load up our aircraft into Matlab. Make sure to download Lab_Airplane.mat and place it into your active working directory
We can run our script now to clear our system and load up our matlab file.
On the Command Window, we can also run some commands - type the following into the Command window to take a look at the new variables in your workspace
who -file Lab_Airplane.mat
Your variables are:
airplane f
The 'who' command prints a list of variables - in this case, we've asked it about the Lab_Airplane.mat file
Making an Airplane
Still working in the Command Window, lets take a closer look at the 'airplane' variable
Airplane is a 36 row by 3 column matrix. This variable represents the x, y, and z coordinates of every corner in our airplane model. There are 36 points within our airplane model.
The first point in our model occurs at the 3d point: -6.8 on the x-axis, 0.5 on the y-axis and -.8 on the z-axis
3D Visualization
One point isn't useful, lets grab the first 20 points of the airplane model. To do this, we'll ask for the 1st 20 rows of data, and either the 1st, 2nd or 3rd column (representing x, y and z).
more_x_points = airplane(1:20,1);
more_y_points = airplane(1:20,2);
more_z_points = airplane(1:20,3);
Now let's plot it.
plot3(more_x_points,more_y_points,more_z_points,'.','MarkerSize',10);
To plot in 3D, we need to use the plot3 command followed by the x coordinates, the y coordinates and the z coordinates. We've also specified to use a Dot for the point, and with a size of 10 to make it more visible. In the following lines, we turned the grid on and labeled the axes. The axis equal command makes sure that all our grids are square shaped, and not a rectangle.
What do we see? If you look closely, we have an engine in the back, and the left and right wing. But it's a little hard to see without drawing the lines between the points.
Let's plot a couple.
plot3([more_x_points(1) more_x_points(2)],[more_y_points(1) more_y_points(2)],[more_z_points(1) more_z_points(2)],'k');
plot3([more_x_points(2) more_x_points(6)],[more_y_points(2) more_y_points(6)],[more_z_points(2) more_z_points(6)],'k');
plot3([more_x_points(6) more_x_points(5)],[more_y_points(6) more_y_points(5)],[more_z_points(6) more_z_points(5)],'k');
plot3([more_x_points(5) more_x_points(1)],[more_y_points(5) more_y_points(1)],[more_z_points(5) more_z_points(1)],'k');
plot3([more_x_points(8) more_x_points(10)],[more_y_points(8) more_y_points(10)],[more_z_points(8) more_z_points(10)],'k');
plot3([more_x_points(10) more_x_points(12)],[more_y_points(10) more_y_points(12)],[more_z_points(10) more_z_points(12)],'k');
plot3([more_x_points(12) more_x_points(19)],[more_y_points(12) more_y_points(19)],[more_z_points(12) more_z_points(19)],'k');
plot3([more_x_points(19) more_x_points(8)],[more_y_points(19) more_y_points(8)],[more_z_points(19) more_z_points(8)],'k');
That's enough - this is getting tedious. But its just like connecting the dots. Luckily, there's a better way to do this.
Drawing a Polygon
To draw polygons in Matlab, we can use the patch function.
patch('Faces',f,'Vertices',airplane,'FaceColor','red');
The patch function takes as inputs the vertices of the airplane, and an additional matrix of where all the lines connect to. That's what's in the 34 x 4 matrix f. Each row is a different polygon and each column lists the numbers of the verticies that are connected.
xlabel('Body Frame - Length (meters)');
ylabel('Body Frame - Wingspan (meters)');
zlabel('Body Frame - Height (meters');
As with all figures, we must give it a title, and label the axes. This model was draw in meters, with the origin (0,0,0) representing the center of gravity.
To get us a more interested view, we set the default view from this angle
Notice that our plane is upside down - we've drawn it in the forward, right, down orientation. In order for matlab to display this right side up, we will have to adjust our plot settings - more on this later.
Our model aircraft is drawn in the BODY Frame. In order for us to use it correctly, we'll need to convert the airplane into the WORLD frame.
Local Copy
If you want to run this code on your computer in a regular matlab script, make sure you download the Lab_Airplane.mat file. Below is a template code:
Modifying the Airplane
You can modify the color of the aircraft by specifying a different FaceColor. Search the Matlab specifications for all the different color options you have available.
What to Submit:
- Screenshot of aircraft in a color other than Red