Lab 1. Part 4. Practice Problems

Practice in MATLAB

Practice Problem 1: Fixing bugs

Edit buggy.m so that when it runs, it produces this output in the Command Window:
>> buggy
ans =
     3
ans =
   120
ans =
    15
ans =
    57
ans =
     2
ans =
         3125 
ans =
   Inf
ans =
    3.1416
ans =
  314.1593
ans =
   3.1416e+03
ans =
     0
ans =
   71.3208
Welcome to CS112
The square root of 853 is
ans = 
   29.2062
>> 

Practice problem 2: Calculating distances on a grid

In this practice problem, we'll be working with the map shown below. You are at home. You'll use MATLAB to figure out which destination is closest, the mall or the pizza place. This problem of determining the closest place is a common operation.

Step 1. Where is home?

We will use variables, which allow us to store information over time. For example, let's use two variables to store our home location. By looking at the map, you can see that home is at row 4 and column 8. Let's type these into MATLAB:
>> homeRow = 4
>> homeCol = 8
Notes about variables:
Variable names must start with a letter and cannot have spaces. MATLAB is case-sensitive when it comes to variable names. MATLAB will reject the following variable names:
  • _myLocation (because of the leading underscore)
  •  home (because of the leading space)
  • fave place (because of the space between words)
  • 5Guys (because of the leading number)
Here are reasonable alternatives:
  • myLocation or my_location
  • home
  • fave_place or favePlace
  • guys5 or guysFive

Step 2. Check out your workspace

Now, look at your workspace in MATLAB. Note how it is keeping track of all variables that you create. Think of it as MATLAB's memory.

Step 3. Where is the mall?

Just as we did above for home, enter two more variables that will keep track of the mall's row and column location. Choose your variable names so they are clear and descriptive. Note that your workspace should also keep track of your mall location variables.

Step 4. Clear your workspace (where MATLAB remembers all your variables).

Huh? Why would we do that? Just for practice, so you can learn how MATLAB works. Type clear, as shown below:
>> clear
You can also clear your command window
clc clears your command window. Good command to know.

And now, look at your workspace. Your variables should be gone. Wiped away. Check and make sure.
>> homeRow
And MATLAB will tell you it doesn't know what homeRow is.

Step 5. Help! I want my variables back!

No problem! Use your arrow keys. The up arrow will allow you to access your previous commands and re-execute them. So go back until you find the lines the defined your variables, and re-execute them all. Keep an eye on your workspace to make sure all the relevant variables are re-established. Ok, now we can get back to our calculations.

Step 6. How far -- in rows -- is the mall from home?

First, let's calculate the distance between the homeRow and the mallRow.
>> homeRow - mallRow
Since we are calculating distance, we only care about the difference between the two numbers, and not the sign (positive or negative). So we can use MATLAB's built-in absolute value function abs. Let's save the row distance in a variable (fill in the blank below with an appropriate variable name):
>> _______ = abs(homeRow - mallRow)

Step 7. How far -- in columns -- is the mall from home?

Perform the same calculation that we did in Step 4 above, but this time for column distance instead of row distance, and save the column distance in a new variable. Look at all the variables you have created in your workspace.

Step 8. How far is the mall (in total) from home?

Now we can add the row distance to the column distance and come up with the total distance between home and the mall. Create a new variable to hold the total distance.
Challenge: can you produce this output in MATLAB (note everything is on the same line) using your variable (with MATLAB's built-in disp to produce the distance 5 shown below:
The distance between home and the mall is 5 blocks.

Step 9. How far is the pizza place from home?

You can follow the same steps for the pizza place, and calculate the total distance from home to the pizza place. Is the pizza place or the mall closer to home?

SUMMARY: What have we covered so far?

  • variable names are entered on the left, followed by the equals sign, then the value to assign the variable on the right. For example, homeRow = 4
  • if the value contains letters, enclose it in single quotation marks (sometime we refer to this as a 'string'). For example, courseName = 'cs112'
  • You can see a variable's value in the Command window by typing just its name. For example,
    >> homeRow
    homeRow = 
         4
    
  • clear removes all the variables from the Workspace.
  • clc clears the text in the Command Window
  • You can suppress the output from being shown in the Command Window by ending the command with a semicolon ;
    >> homeRow;
    
  • You can perform math using variables. For example, distRow = abs(homeRow - mallRow)