SOLUTIONS MANUAL
,Chapter 1: Introduction to MATLAB
Exercises
1) Create a variable myage and store your age in it. Subtract 2 from
the value of the variable. Add 1 to the value of the variable. Observe
the Workspace Window and Command History Window as you do this.
>> myage = 20;
>> myage = myage - 2;
>> myage = myage + 1;
2) Explain the difference between these two statements:
result = 9*2
result = 9*2;
Both will store 18 in the variable result. In the first, MATLAB will
display this in the Command Window; in the second, it will not.
3) Use the built-in function namelengthmax to find out the maximum
number of characters that you can have in an identifier name under
your version of MATLAB.
>> namelengthmax
ans =
63
4) Create two variables to store a weight in pounds and ounces. Use
who and whos to see the variables. Use class to see the types of the
variables. Clear one of them and then use who and whos again.
>> pounds = 4;
>> ounces = 3.3;
>> who
Your variables are:
ounces pounds
>> whos
Name Size Bytes Class
Attributes
ounces 1x1 8 double
pounds 1x1 8 double
, >> clear pounds
>> who
Your variables are:
ounces
5) Explore the format command in more detail. Use help format to
find options. Experiment with format bank to display dollar values.
>> format +
>> 12.34
ans =
+
>> -123
ans =
-
>> format bank
>> 33.4
ans =
33.40
>> 52.435
ans =
52.44
6) Find a format option that would result in the following output
format:
>> 5/16 + 2/7
ans =
67/112
>> format rat
>> 5/16 + 2/7
ans =
67/112
7) Think about what the results would be for the following expressions,
and then type them in to verify your answers.
* 5
4 + 3 ^ 2
(4 + 3) ^ 2
3 \ 12 + 5
4 – 2 * 3
>> 25/5*5
ans =
, 25
>> 4 + 3 ^ 2
ans =
13
>> (4 + 3) ^ 2
ans =
49
>> 3 \ 12 + 5
ans =
9
>> 4 - 2 * 3
ans =
-2
As the world becomes more “flat”, it is increasingly important for
engineers and scientists to be able to work with colleagues in other
parts of the world. Correct conversion of data from one system of
units to another (for example, from the metric system to the
American system or vice versa) is critically important.
8) Create a variable pounds to store a weight in pounds. Convert this
to kilograms and assign the result to a variable kilos. The conversion
factor is 1 kilogram = 2.2 lb.
>> pounds = 30;
>> kilos = pounds / 2.2
kilos =
13.6364
9) Create a variable ftemp to store a temperature in degrees
Fahrenheit (F). Convert this to degrees Celsius (C) and store the result
in a variable ctemp. The conversion factor is C = (F – 32) * 5/9.
>> ftemp = 75;
>> ctemp = (ftemp - 32) * 5/9
ctemp =
23.8889
10) The following assignment statements either contain at least one
error, or could be improved in some way. Assume that radius is a
variable that has been initialized. First, identify the problem, and then
fix and/or improve them:
33 = number
The variable is always on the left
number = 33