Professional Documents
Culture Documents
https://testbankpack.com/p/solution-manual-for-matlab-
programming-for-engineers-5th-edition-chapman-1111576718-
9781111576714/
2. MATLAB Basics
2.1 (a) The size of array1 is 4 5. (b) The value of array1(1,4) is -3.5. (c)
array1(:,1:2:5) is a 4 3 array consisting of the first, third, and fifth columns of array1:
>> array1(:,1:2:5)
ans =
0 2.1000 6.0000
0 -6.6000 3.4000
2.1000 0.3000 1.3000
1.1000 0 -2.0000
(d) array1([1 3],end) consists of the elements in the first and third rows on the last column
of array1:
» array1([1 3],end)
5
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
ans =
6.0000
1.3000
2.2 (a) Legal (b) Illegal—names must begin with a letter. (c) Legal (d) Illegal—names must begin
with a letter. (e) Illegal—the apostrophe and the question mark are illegal characters.
2.3 (a) This is a three element row array containing the values 2, 5, and 8:
» a = 2:3:8
a =
2 5 8
(c) This is a 2 2 element array containing the first and third rows and columns of b:
» c = b(1:2:3,1:2:3)
c =
2 2
8 8
(d) This is a 1 3 row array containing the sum of a (= [2 5 8]) plus the second row of b (= [5 5
5]):
» d = a + b(2,:)
d =
7 10 13
6
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
(e) This is a 1 9 row array containing:
Note that the expression 3:5' is the same as 3:5, because the transpose operator applies to the
single element 5 only: 5' = 5. Both expressions produce the row array [1 3 5]. To produce
a column array, we would write the expression as (3:5)', so that the transpose operator applied
to the entire vector.
(f) This statement swaps the first and third rows in the second column of array b:
(g) This statement produces nothing, because even the first element (1) is below the termination
condition (5) when counting down:
» e = 1:-1:5
e =
Empty matrix: 1-by-0
» array1(3,:)
ans =
2.1000 0.3000 0.1000 -0.4000 1.3000
» array1(:,3)
ans =
2.1000
-5.6000
0.1000
0
(c) This array consists of the first and third rows and the third and fourth columns of array1, with
the third column repeated twice:
» array1(1:2:3,[3 3 4])
ans =
-2.1000 -2.1000 -3.5000
0.1000 0.1000 -0.4000
» array1([1 1],:)
ans =
7
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.1000 0 -2.1000 -3.5000 6.0000
1.1000 0 -2.1000 -3.5000 6.0000
2.5 (a) This statement displays the number using the normal MATLAB format:
» fprintf('value = %e\n',value);
value = 3.141593e+001
» fprintf('value = %f\n',value);
value = 31.415927
(e) This statement displays the number in general format, which uses an exponential form if the
number is too large or too small.
» fprintf('value = %g\n',value);
value = 31.4159
(f) This statement displays the number in floating-point format in a 12-character field, with 4 digits
after the decimal point:
» fprintf('value = %12.4f\n',value);
value = 31.4159
10
(b) Legal: This is matrix multiplication. Since eye(2) is the 2 2 identity matrix ,
the
result of the multiplication is just the original matrix a. 01
» result = a * d
result =
2 1
-1 4
8
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
(c) Legal: This is element by element array multiplication
» result = a .* d
result =
2 0
0 4
» result = a * c
result =
5
2
(e) Illegal: This is element by element array multiplication, and the two arrays have different sizes.
» result = a \ b
result =
-0.4444 1.1111
-0.1111 0.7778
(g) Legal: This is element by element array left division: b(i) / a(i)
» result = a .\ b
result =
-0.5000 3.0000
0 0.5000
» result = a .^ b
result =
0.5000 1.0000
1.0000 16.0000
2.7 (a) 8.2 (b) 8.2 (c) 1.0 (d) 729 (e) 6561 (f) 729 (g) 4 (h) 4 (i) 3
2.9 The solution to this set of equations can be found using the left division operator:
2.10 A program to plot the height and speed of a ball thrown vertically upward is shown below:
» ball
Enter the initial velocity of the ball: 20
Enter the initial height of the ball: 10
2.11 A program to calculate the distance between two points in a Cartesian plane is shown below:
10
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Prompt the user for the input points
x1 = input('Enter x1: ');
y1 = input('Enter y1: ');
x2 = input('Enter x2: ');
y2 = input('Enter y2: ');
% Calculate distance
dist = sqrt((x1-x2)^2 + (y1-y2)^2);
% Tell user
disp (['The distance is ' num2str(dist)]);
» dist2d
Enter x1: -3
Enter y1: 2
Enter x2: 6
Enter y2: -6
The distance is 10
2.12 A program to calculate the distance between two points in a Cartesian plane is shown below:
% Calculate distance
dist = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2);
% Tell user
disp (['The distance is ' num2str(dist)]);
11
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
When this program is executed, the results are:
» dist3d
Enter x1: -3
Enter y1: 2
Enter z1: 5
Enter x2: 3
Enter y2: -6
Enter z2: -5
The distance is 14.1421
% Calculate dBm
dBm = 10 * log10( pin / 1.0e-3 );
% Tell user
disp (['Power = ' num2str(dBm) ' dBm']);
» decibel
Enter the power in watts: 10
Power = 40 dBm
» decibel
Enter the power in watts: 0.1
Power = 20 dBm
13
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
When this program is executed, the results are:
14
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
2.14 A program to calculate and plot the power consumed by a resistor as the voltage across the resistor
is varied from 1 to 200 volts shown below:
15
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The resulting plots are shown below.
16
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
2.15 (a) A program that accepts a 3D vector in rectangular coordinates and calculates the vector in
spherical coordinates is shown below:
% Tell user
disp ('The spherical coordinates are:');
disp (['r = ' num2str(r)]);
disp (['theta = ' num2str(theta)]);
disp (['phi = ' num2str(phi)]);
>> rect2spherical
Enter x: 4
Enter y: 3
Enter z: 0
The spherical coordinates are:
r = 5
theta = 36.8699
phi = 0
>> rect2spherical
Enter x: 4
Enter y: 0
Enter z: 3
The spherical coordinates are:
17
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
r = 5
theta = 0
phi = 36.8699
(b) A program that accepts a 3D vector in spherical coordinates (with the angles and ϕ
in degrees) and calculates the vector in rectangular coordinates is shown below:
% Tell user
disp ('The 3D rectangular coordinates are:');
disp (['x = ' num2str(x)]);
disp (['y = ' num2str(y)]);
disp (['z = ' num2str(z)]);
>> spherical2rect
Enter vector length r: 5
Enter plan view angle theta in degrees: 36.87
Enter elevation angle phi in degrees: 0
The rectangular coordinates are:
x = 4
y = 3
z = 0
>> spherical2rect
Enter vector length r: 5
18
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Another random document with
no related content on Scribd:
“It’s yours. My friend in the South doesn’t want it.”
“With all that cloth you won’t have to worry about breeches now for
the rest of your life, Don,” said Glen grinning.
Don did not reply; he was thinking hard.
The next morning while Glen and his uncle were with the troops he
entered the cellar and spent almost an hour making a list of the
supplies that were there. Then he hurried up-stairs and went out into
the street.
Half an hour later he was standing in front of a lieutenant in a large
hallway. “I’d like very much to see General Washington,” he said.
“Indeed,” said the lieutenant; “and what may be your business?”
“I have something to give him.”
“Indeed. You don’t look as if you had much to give.” The lieutenant
smiled good-naturedly. “I’m sorry to have to turn you away, but the
general is a busy man these days.”
Don fell back a pace and looked around him.
“I’m sorry——” the lieutenant was saying, when a door opened, and
a tall figure stepped into the hall.
Like a flash the lieutenant and several other officers who were
standing near by snapped to attention. It was Washington himself
that was walking quietly toward the entrance. Don gulped once, and
then before he knew what he was doing he had exclaimed:
“Sir—General Washington!”
The general turned, and Don pulled his slip of paper from his pocket
and handed it to him. “This is a list of goods that were in our cellar all
during the occupation,” he said. “My uncle, Capt. David Hollis, gave
them to me for keeping the Redcoats from getting them. I want to
give them to our army.”
Washington glanced at the paper—he seemed to read everything on
it in a single glance—and then turned to the boy. “The army will be
very grateful to have these supplies,” he said. “I thank you, my boy.
You are a true patriot.”
Don colored to the roots of his hair as he watched the general hand
the paper to the lieutenant and then turn and smile and pass into the
street.
“Donald!” cried Aunt Martha as Don burst noisily into the room.
“What’s the matter?”
“I gave the supplies to Washington!” cried Don. “I saw him, Aunt
Martha, and he said the army would be glad to get them. You know
they need stuff for uniforms, and especially powder.”
“Good for you, Donald! It’s the best thing you could have done with
them.”
“And, Aunt Martha, he said I was a true patriot!”
“You are, Donald; you’ve helped the cause.”
In another minute Don was, closely followed by Sailor, on his way to
Hog Alley to tell Jud the news. His eyes were bright, and his face
was flushed as he ran along the streets, which now were filled with
Continental uniforms. He had done something to help his country at
last.
TRANSCRIBER’S NOTES:
Obvious typographical errors have been corrected.
Inconsistencies in hyphenation have been
standardized.
Archaic or variant spelling has been retained.
*** END OF THE PROJECT GUTENBERG EBOOK A PATRIOT LAD
OF OLD BOSTON ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.