        >>  Lectures >>  Matlab 6     Navigator      ## 6.1 Exercise 2

### 6.1.1 Part 1

For this exercise, we will compute and plot the distribution of the rider's weight and the total distance travelled, given a constant speed of 20km/h.

We create a new m-file for this exercise named exercise_plot1.m. Since we will use most of the variables from exercise_batterypower.m, we include the m-file on the first line. Figure 6.1 Click image to enlarge, or click here to open
We will be evaluating a range of weights for the rider at constant height, hence we define a vector of values from 40kg to 110kg in 1kg increments:

mrider = [40:1:110] The variable mrider has previously been defined already as a single value. By assigning the vector [40:1:110] to the variable name mrider, we are overwriting the previous value. Figure 6.2 Click image to enlarge, or click here to open
At this point, we will re-evaluate equations , , and , which we have used in exercise_batterypower.m. We merely need to copy and paste them into the m-file for this exercise. The only difference at this point is that the variable mrider is no longer a scalar, but a vector. Since we have only one vector and all other variables are scalars, the result will also be a vector. Figure 6.3 Click image to enlarge, or click here to open
Using equation we can now compute the total time travelled given the previously computed value of Ebatteries, and the newly computed range of Power values required to move the Segway at 20 km/h. Equation is rearranged as follows:

ttravelled = Ebatteries / Prequired Note that since Prequired is a vector, and we would like to divide a scalar by a vector, we need to explicitely state that we're dividing the scalar by each element of the vector, as opposed to doing matrix division. Instead of using /, we must use ./ Figure 6.4 Click image to enlarge, or click here to open
Since the resulting vector of ttravelled is in seconds, it would be a good idea to convert the values to minutes:

ttravelled in minutes = ttravelled / 60 In this case, since we are dividing a vector by a scalar, we need not specify the . in front of / Figure 6.5 Click image to enlarge, or click here to open
We can now evaluate the variable ttravelled in minutes to see all elements in the vector. Figure 6.6 Click image to enlarge, or click here to open
However, we would like to plot the results for better visual digestion. 2-dimensional plots are created using the plot function. The plot function requires as input two equally sized vectors, the first one representing values on the x-axis, and the second representing values on the y-axis:

In our case, variable mrider represents the range of weights for the rider, and variable ttravelled in minutes represents the corresponding time travelled in minutes. The plot function is thus called as follows:

plot(mrider, ttravelled minutes) Figure 6.7 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.8 Click image to enlarge, or click here to open
The plot can be refined with labels, title, and grid as follows:

plot(mrider, ttravelled minutes), xlabel('Weight of Traveller in kg'), ylabel('Time travelled in minutes'), title('Weight of rider vs. time travelled at constant velocity of 20km/h'), grid Figure 6.9 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.10 Click image to enlarge, or click here to open
The graph suggests that the lighter the rider is, the larger the additional amount of time travelled, i.e. the relationship between rider weight and time travelled is not linear. The source for this behavior is function FSA, in which we evaluate Frontal Surface Area as a function of height and weight. Since this function is used in computing Aerodynamic drag Fair, the non-linear additional time travelled for lighter riders comes solely from having a smaller frontal surface area.

However, this is only true because of the equations we used. Firstly, a rider's height changes with weight, even though the relationship is not linear, meaning that even if we varied height and weight properly, the relationship between weight and total time travelled would not be linear. Secondly, we have not taken into consideration any variation in efficiency due to mass. We have stated before that inefficiencies are due to friction and other factors within the gears, etc. Without doubt, these inefficiencies are a function of weight.

### 6.1.2 Part 2

For the second part we will simply convert the results from part 1 from timetravelled to distancetravelled.

We create a new m-file and name it exercise_plot2.m, and include exercise_plot1, which recursively includes exercise_batterypower.m Figure 6.11 Click image to enlarge, or click here to open
We merely need to multiply the vector of timetravelled (in sec) by the velocity in m/sec, and finally divide the resulting vector by 1000 for a resulting vector in km. We then plot the results. Figure 6.12 Click image to enlarge, or click here to open
We see from the graph that the range of distance is between 13 and 22. Figure 6.13 Click image to enlarge, or click here to open

### 6.1.3 Part 3

For the third part of this exercise, we will vary both weight (range of 40 through 110) and speed (range of 1 through 20), and finally plot a 3D graph.

We create a new m-file and name it exercise_mesh.mSince we will use the previous definition of weight from exercise_plot1.m, we call exercise_plot1.m on the first line of this new m-file. Figure 6.14 Click image to enlarge, or click here to open
Next, we define the range of values for velocity vvar as [1..20]:

vvar = [1:20] Figure 6.15 Click image to enlarge, or click here to open
Before we compute the 2 dimensional matrix of results, it is a good practice to initialize the matrix variable by setting it to an empty vector:

disttravelled km = [ ] Figure 6.16 Click image to enlarge, or click here to open
We have already computed vectors of results. A good way to think of a matrix of results is that the matrix is a collection of vectors. We will treat the computation in the following way:

For each scalar of velocity in the range of velocities, compute the results given the range of weights. The result is a vector for a specific value of velocity, which we place in the matrix of final results.

Once we have done this for each velocity, we have a matrix of results.

We now make use of a for loop to iterate over all velocity values. We begin by writing an empty loop:

for v = 1:length(vvar)

end Figure 6.17 Click image to enlarge, or click here to open
At this point, we can copy and paste the equations for computing the result vector for distance travelled from exercise_plot1.m and exercise_plot2.m. Note that while we are still using the variable mrider, which is the range of rider weights, we are not using the variable v_const as before. Instead, we are using each value in the vector vvar, over which we are iterating. To index into the currently iterated position of the vector, we are using the statement vvar(v), which returns a scalar for the current velocity. Figure 6.18 Click image to enlarge, or click here to open
To save the resulting vector dist_travelled in the matrix, we will need to access a particular row of the matrix, and store the result vector in that row. To do this, we make use of the statement disttravelled km(v,:), which accesses row v in the matrix. Figure 6.19 Click image to enlarge, or click here to open
We can now evaluate the resulting matrix in the Command Window. Figure 6.20 Click image to enlarge, or click here to open
To plot the results in 3D, we use a function called mesh. Instead of taking 2 parameters like plot, it takes 3: range of values on the x-axis, range of values on the y-axis, and the matrix of results, which has to match the size of the x and y range (x range as columns, y range as rows) Figure 6.21 Click image to enlarge, or click here to open
Once we evaluate the function, the 3D mesh appears in a new window. Depending on the complexity of the equations and the size of input vectors, this may take quite a while. Figure 6.22 Click image to enlarge, or click here to open
Function mesh takes the same types of feature parameters for labeling axes, adding a title and a grid. Figure 6.23 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.24 Click image to enlarge, or click here to open
Using the rotate button in the toolbar, we can interactively rotate the 3D mesh to any desirable position Figure 6.25 Click image to enlarge, or click here to open
During rotation, the graph does not update interactively. Only after rotating the bounding box, the graph will appear in its new position. Figure 6.26 Click image to enlarge, or click here to open
Besides mesh, there are 3 other derivatives of the function that present the graph in a slightly different fashion.

meshc includes contour lines. Figure 6.27 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.28 Click image to enlarge, or click here to open
meshz includes waterfall lines at the edges of the graph. Figure 6.29 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.30 Click image to enlarge, or click here to open
contour draws only the contour lines without the 3D mesh. Figure 6.31 Click image to enlarge, or click here to open
The resulting graph appears in a new window. Figure 6.32 Click image to enlarge, or click here to open