MATLAB: A Practical Introduction to
Programming and Problem Solving
Second Edition
SOLUTION MANUAL
Stormy Attaway
College of Engineering
Boston University
,I. Introduction to Programming Using MATLAB
Chapter 1: Introduction to MATLAB
Exercises
1) Create a variable to store the atomic weight of silicon (28.085).
>> siliconAtWt = 28.085
siliconAtWt =
28.0850
2) Create a variable myage and store your age in it. Subtract one from the value of
the variable. Add two to the value of the variable.
>> myage = 35;
>> myage = myage - 1;
>> myage = myage + 2;
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) 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
5) 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
6) Think about what the results would be for the following expressions, and then
type them in to verify your answers.
* 4
3 + 4 ^ 2
4 \ 12 + 4
3 ^ 2
(5 – 2) * 3
>> * 4
ans =
25
>> 3 + 4 ^ 2
ans =
19
>> 4 \ 12 + 4
ans =
7
>> 3 ^ 2
ans =
9
>> (5 - 2) * 3
ans =
9
7) The combined resistance RT of three resistors R1, R2, and R3 in parallel is given
by
1
RT = 1 1 1
R1 R2 R3
, Create variables for the three resistors and store values in each, and then
calculate the combined resistance.
>> r1 = 3;
>> r2 = 2.2;
>> r3 = 1.5;
>> rt = 1/(1/r1 + 1/r2 + 1/r3)
rt =
0.6875
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
pounds.
>> 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) Find another quantity to convert from one system of units to another.
>> kmPerMile = 1.6093;
>> miles = 6;
>> km = miles * kmPerMile
km =
9.6558
11) The function sin calculates and returns the sine of an angle in radians. Use help
elfun to find the name of the function that returns the sine of an angle in degrees.
Verify that calling this function and passing 90 degrees to it results in 1.
>> sind(90)
Programming and Problem Solving
Second Edition
SOLUTION MANUAL
Stormy Attaway
College of Engineering
Boston University
,I. Introduction to Programming Using MATLAB
Chapter 1: Introduction to MATLAB
Exercises
1) Create a variable to store the atomic weight of silicon (28.085).
>> siliconAtWt = 28.085
siliconAtWt =
28.0850
2) Create a variable myage and store your age in it. Subtract one from the value of
the variable. Add two to the value of the variable.
>> myage = 35;
>> myage = myage - 1;
>> myage = myage + 2;
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) 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
5) 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
6) Think about what the results would be for the following expressions, and then
type them in to verify your answers.
* 4
3 + 4 ^ 2
4 \ 12 + 4
3 ^ 2
(5 – 2) * 3
>> * 4
ans =
25
>> 3 + 4 ^ 2
ans =
19
>> 4 \ 12 + 4
ans =
7
>> 3 ^ 2
ans =
9
>> (5 - 2) * 3
ans =
9
7) The combined resistance RT of three resistors R1, R2, and R3 in parallel is given
by
1
RT = 1 1 1
R1 R2 R3
, Create variables for the three resistors and store values in each, and then
calculate the combined resistance.
>> r1 = 3;
>> r2 = 2.2;
>> r3 = 1.5;
>> rt = 1/(1/r1 + 1/r2 + 1/r3)
rt =
0.6875
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
pounds.
>> 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) Find another quantity to convert from one system of units to another.
>> kmPerMile = 1.6093;
>> miles = 6;
>> km = miles * kmPerMile
km =
9.6558
11) The function sin calculates and returns the sine of an angle in radians. Use help
elfun to find the name of the function that returns the sine of an angle in degrees.
Verify that calling this function and passing 90 degrees to it results in 1.
>> sind(90)