Chapter 2 Homework Solutions
clear,clc, format shortg
You can either solve these problems in the command window, using MATLAB® as an electronic
calculator, or you can create an M-file of the solutions. If you are solving these problems as a homework
assignment or if you want to keep a record of your work, the best strategy is to use an M-file, divided
into cells with the cell divider %%.
Problem 2.1
Predict the outcome of the following MATLAB® calculations. Check your results by entering the
calculations into the command window.
1 + 3/4
ans = 1.75
5*6*4/2
ans = 60
5/2*6*4
ans = 60
5^2*3
ans = 75
5^(2*3)
ans = 15625
1 + 3 + 5/5 + 3 + 1
ans = 9
(1 + 3 + 5)/(5 + 3 + 1)
ans = 1
Using Variables
Problem 2.2
Identify which name in each of the following pairs is a legitimate MATLAB® variable name. Test your
answers by using isvarname—for example,
1
,isvarname fred
Remember, isvarname returns a 1 if the name is valid and a 0 if it is not. Although it is possible
to reassign a function name as a variable name, doing so is not a good idea. Use which to check
whether the preceding names are function names—for example,
which sin
In what case would MATLAB®tell you that sin is a variable name, not a function name?
The legitimate Matlab names are: fred book_1 Second_Place No_1 vel_5 tan
isvarname fred
ans = 1
isvarname book_1
ans = 1
isvarname Second_Place
ans = 1
isvarname No_1
ans = 1
isvarname vel_5
ans = 1
isvarname tan %although tan is a function name it can be used as a variable name
ans = 1
isvarname fred! %! is not an allowed character
ans = 0
isvarname book-1 % - is not an allowed character
ans = 0
isvarname 2ndplace %variable names must start with a letter
ans = 0
isvarname #1 %# is not an allowed character
ans = 0
isvarname vel.5 % . is not an allowed character
ans = 0
2
, isvarname while %while is a reserved name
ans = 0
which tan % tan is a function name
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elfun\@double\tan) % double method
which while %while is also a function name, but is reserved
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\lang\while)
%You can reassign a function name as a variable name
%For example
sin=3
sin = 3
%The which function now tells us sin is a variable
which sin
sin is a variable.
% Use the clear function to return sin to its function definition
clear sin
which sin
built-in (C:\Program Files\MATLAB\R2016b\toolbox\matlab\elfun\@double\sin) % double method
Scalar Operations and Order of Operations
Problem 2.3
Create MATLAB® code to perform the following calculations. Check your code by entering it into
MATLAB® and performing the calculations on your scientific calculator.
5^2
ans = 25
(5 + 3)/(5*6)
ans = 0.26667
sqrt(4+6^3) % or...
ans = 14.832
(4+6^3)^(1/2)
ans = 14.832
3
, 9*6/12 + 7*5^(3+2)
ans = 21880
1 + 5*3/6^2 + 2^(2-4) *1/5.5
ans = 1.4621
Problem 2.4
(a) The area of a circle is Define r as 5, then find the area of a circle, using MATLAB®.
r=5
r = 5
area=pi*r^2
area = 78.54
(b) The surface area of a sphere is . Find the surface area of a sphere with a radius of 10 ft.
r=10
r = 10
surface_area=4*pi*r^2
surface_area = 1256.6
(c) The volume of a sphere is 4/3pr2. Find the volume of a sphere with a radius of 2 ft.
r=2
r = 2
volume=4/3*pi*r^3
volume = 33.51
Problem 2.5
(a) The area of a square is the edge length squared (A = edge2). Define the edge length as 5, then find
the area of a square, using MATLAB®.
edge=5
edge = 5
4