Sie sind auf Seite 1von 3

ENGR 13200 – Spring 2011 Activity 1

Class 1a
Activity 1: MATLAB Variables
(Estimated Time: 15 minutes)

This activity will familiarize you with variable assignments in MATLAB.

1. Solving engineering problems with MATLAB will require values to be assigned to variable
names. MATLAB has rules for selecting these variable names. In the Command Window,
type (only type the bold):

>> Tool = 5
>> tool = 6.2222
>> who

2. You now have two variables in a name that would be spoken as "tool". To review the values
stored in each variable name, just type the variable name in the Command Window. Type:

>> tool
>> Tool

The variables available in the workspace can also be viewed in the Workspace window. Note
that the values are different for "Tool" and "tool". MATLAB is case-sensitive, so mixing
upper and lowercase letters will result in different variable names. TOOL, tOoL, TooL, tOOl,
tooL, toOl, toOL are all different variable names. This can lead to confusion to you when
debugging a program. Make sure your variable names are distinct.

3. To clear variables from the workspace, use the clear function. To clear a single variable, you
would type clear variable_name. In the Command Window, type:

>> who
>> clear tool
>> who

How did the result of each "who" function differ?

4. Names can be any combination of letters and numbers. MATLAB uses just the first 63
characters to differentiate between variable names. At the MATLAB prompt, type:

>> variable1=1
>> 2ndvariable=2

Notice the error that results when placing a number at the beginning of a variable name.
Numbers are acceptable at the end or within a variable name (e.g., variable1 or var2nd) but
not at the beginning. Variable names must begin with a letter. It is good programming
practice to use descriptive names for variables so that you can quickly identify what the
variables mean rather than one or two letter variable names (though trying to be too
descriptive can quickly lead to long, unwieldy variable names and long formulas).
ENGR 13200 – Spring 2011 Activity 1
Class 1a
5. There are names to avoid when selecting variables. Type:

>> pi
>> who
>> pi=4
>> who
>> pi
>> sin(pi)

You have created a variable with the same name as a MATLAB function. In other words, pi
is a MATLAB function. Due to the order in which MATLAB searches for named elements,
the variable named pi is used rather than MATLAB's pi function.

6. Consider this: If you enter foo at the MATLAB prompt, MATLAB searches for foo in a very
specific order:
1. Looks for foo as a variable.
2. Checks for foo as a MATLAB function.
3. Looks in the current directory for a file named foo.m.
4. Searches the directories on the MATLAB search path, in order, for foo.m.

To avoid programming errors, you should avoid using the names of MATLAB functions
when creating your variable names. To correct the problem above, type:

>> clear pi
>> who
>> pi

As you can see, creating variables with the same name as MATLAB functions can lead to
confusion and errors in a program. Names to avoid include: ans, pi, eps, inf, NaN, i, j,
realmin, realmax, area and any other existing MATLAB function.

To find if a name is a MATLAB function, type help possible_variable_name (e.g., type help
i; the result is an explanation of the imaginary unit, indicating that MATLAB is already using
the name "i" and you should not). If MATLAB is not using that name, it will respond "not
found" and the name is safe to use. Note that your textbook offers other suggestions for
checking variable names.

Hint: To insure you avoid MATLAB names, use numbers in your names and/or underscores (
_ ) (e.g., variable_1, result2, box_width). There are no MATLAB functions that contain the
underscore symbol, though some functions do use numbers.

7. Identify why some of these variable names are NOT legitimate MATLAB variable names:

1. x 5. apple#5
2. 9lives 6. atan
3. part21 7. car_3z
4. my_favorite_starter_variable_name_is_var_1
ENGR 13200 – Spring 2011 Activity 2
Class 1a
Activity 2: MATLAB as a Calculator
(Estimated Time: 20 minutes)

This task will familiarize you with using MATLAB as a calculator.

1. Calculate:

a) X = (2 + 7)3 + (2732/3)/2 + (552)/3

b) p = 23 + 73 + (2732)/3/2 + 552/3

2. Define the variables x and z as x = 9.6 and z = 8.1, then evaluate:

3/5
 2z 
2
a)
a = xz −  
 3x 

443z e − xz
b)
b= 3
+
2x x+ z

Das könnte Ihnen auch gefallen