Holly Moore
(Solutions Manual All
Chapters, 100% Original
Verified, A+ Grade)
All Chapters Solutions Manual
Supplement files download
link at the end of this file.
,MATLAB for Engineers, 6th Edition
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 a program using a script (M-file) or a live script (MLX-file). 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 a program file, divided into sections with section dividers.
clear, clc
Getting Started
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
Copyright © 2023 Pearson Education, Inc.
,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,
isvarname fred
The function 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 = logical
1
isvarname book_1
ans = logical
1
isvarname Second_Place
ans = logical
1
isvarname No_1
ans = logical
1
isvarname vel_5
ans = logical
1
isvarname tan %although tan is a function name it can be used as a variable
name
ans = logical
1
isvarname fred! %! is not an allowed character
ans = logical
0
Copyright © 2023 Pearson Education, Inc.
, isvarname book-1 % - is not an allowed character
ans = logical
0
isvarname 2ndplace % variable names must start with a letter
ans = logical
0
isvarname #1 % # is not an allowed character
ans = logical
0
isvarname vel.5 % . is not an allowed character
ans = logical
0
isvarname while % while is a reserved name
ans = logical
0
which tan % tan is a function name
built-in (C:\Program Files\MATLAB\R2022a\toolbox\matlab\elfun\@double\tan) % double method
which while % while is also a function name, but is reserved
built-in (C:\Program Files\MATLAB\R2022a\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
Copyright © 2023 Pearson Education, Inc.