HW2 (Due: Sep-15-2014, start of class)
Exercise 1 2 3 TOTAL
Max Nr. Pts 30 30 40 100
Your Score
Submit a printout of your solutions for each problem. Make sure to include all the plots.
1. Problems
(a) Write a MATLAB script, named FromCmToFeetAndInches.m, to prompt the user to enter the height in
cm. The code will then compute the height in feet and inches. Recall that 1 foot = 12 inches = 30.48cm.
For example, if the user enters 178, the code should compute
feet = 5
inches = 10.0787
(b) Write a MATLAB script, named NrDigits.m, to prompt the user to enter an integer and then compute
the number of digits. For example, if the user enters 43521, then the code should compute 5. As another
example, if the user enters 3219, then the code should compute 4.
(c) Recall that the rand function generates a random number in the interval (0; 1). Write a MATLAB script,
named RandomPtsInsideRect.m, to prompt the user to enter the width and length of a rectangle and the
number of random points. Your code should then generate as many random points as requested by the
user, all inside the rectangle with center at (0, 0) and width and length as specified by the user. Use the
plot function to draw the generated points as red circles.
2. Matrices
(a) Write MATLAB code to create the following matrices (use the operator : whenever possible):
A =
2
664
16 10 4
1 5 9
..5 0 5
7 5 3
3
775
B =
2
4
2 6 5 7
3 1 3 2
4 2 9 4
3
5
(b) Display the element in row 3 and column 2 of A
(c) Display the second column of B
(d) Display the block of elements in A at the intersection of rows 1; 3; 4 and columns 1; 3
(e) Delete the second column of B
(f) Change the third row of B to 4; 3; 2; 1
(g) Add 2; 1; 3 as a fifth column to B
3. Plotting
(a) Write a MATLAB script, named MyPlots.m, to plot the functions
f1(x) = x2 .. 1; f2(x) = sin(x)
p
x; f3(x) = log5(abs(x))
as x ranges from ..5 to 5 in increments of 0:1. All three plots must show on the same figure. Use
different line colors and different marker types (e.g., square, circle, triangle, star) for each plot. In each
case, the line width should be 1 and the marker size should be 3. Make sure to include a printout of the
plot when you turnin the homework.
1
(b) Write MATLAB commands to plot the pairs (2; 5), (3; 8), (4; 7), (5; 5), (6; 9). Do not show lines, just
the points. Display points as blue triangles.
(c) Write a MATLAB script, named Smile.m, to draw a smiley face similar to this one:
Make sure to include a printout of your drawing when you turnin the homework.
2
PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT 🙂