14:440:127 Introduction to Computers for Engineers
Recitation 12
- Solutions
1.
Plotting
Over the past 5 years, the number of students in some Computer Vision class has been
15, 25, 55, 115, 144. Class size seems like it
ʼ
s growing exponentially. To verify this, plot
these values on a plot with a log y scale and label it (
semilogy, xlabel, ylabel, title
).
Use magenta square symbols of
marker size
10 and
line width
4, and no line
connecting them. (marker size and line width are both properties that you can change
direction in a line of code - type in
help plot
to see examples.) You may have to change
the x limits to see all 5 symbols (
xlim
). If the relationship really is exponential, it will look
linear on a log plot.
The script that generates the desired Fgure is pasted below:
% plotClassSize
% plots the number of students in 6.094 over 5 years on a
semilog plot
% open a figure and plot the data
figure
semilogy([1 2 3 4 5],[15 25 55 115 144],'ms','markersize',
10,'linewidth',4);
% change x limits and label it
xlim([0.5 5.5]);
xlabel('Year');
ylabel('Number of Students')
title('Number of students per year in 6.094');
2.±unction
Make function M-Fle called
myLoopTest
(N)
that loops through the values 1 through N
and for each number n it should display
ʻ
n is divisible by 2
ʼ
,
ʻ
n is divisible by 3
ʼ
,
ʻ
n is
divisible by 2 AND 3
ʼ
or
ʻ
n is NOT divisible by 2 or 3
ʼ
. Use a for loop, the function mod or
rem to Fgure out if a number is divisible by 2 or 3, and num2str to convert each number
to a string for displaying. You can use any combination of if, else, and elseif.
% myLoopTest(N)
%
% loops through the values 1 to N and for each number, displays
whether the
% number is divisible by 2, 3, both 2 and 3, or neither
function myLoopTest(N)
% loop through the numbers
for n=1:N
if ~rem(n,2) % if divisible by 2