CS 111 Introduction to Programming for Engineering and Science

advertisement
CS 111 Introduction to Programming for Engineering
and Science
Midterm No. 1: Matlab Solution
Date: 25/03/2006
Time: 10:45-12:40
Open-book, open notes exam
Rules:
1. Sharing a book, notes, pencils, erasers, dictionary, etc is not allowed.
2. Asking questions to the proctors about the exam or talking with other students is not
allowed.
3. Copying will be punished.
4. You must leave at least one chair before the next student in the same row.
5. You cannot enter the exam after 10 minutes the exam has started. No extra time will
be given to latecomers.
6. Make sure that you have 3 questions in 6 pages. You can answer the questions at the
empty space provided. Please write a comment if you type the answers at the other side
of the sheets.
7. When your proctor announces that the exam is over, put your pens down and pass the
exam to the right student in the row. The last person in the row has to pass the answers
to the front, and so the proctor will get all the exams from the first row, rightmost
student.
8. It is your responsibility to fill in your information to the following table and sign. You
must also sign the sitting plan of your exam place correctly. Please write your name and
surname on every page.
Name:
...........................
Surname: ...............................................................
Section:
...........................
Exam Place:..............................................................
Student ID: ..........................
Signature: ........................................................
Grade
Question 1
25
Question 2
25
Question 3
50
Total
100
Page 1
Name and surname: ______________________________________________________________________
Question 1. (25 points) Arrays and related operations
Let a denote the following matrix:
 1
2
a  
3

4
1 0
1 1
3 3
0
0





 4
2
0
3
Find the resulting arrays b, c, d, e, f, mask1 provided that the assignment statements are given in the order
from top to bottom:
b=a(3:-2:1, 2:end);
b=[b; ones(1,3)*-1]
3
3 2

b   1 0  2
 1  1  1
d=[ones(4,1) zeros(4,1)]
1
1
d
1

1
0
0
0

0
1
1

1

1
2

2
2

2
c
3

3

3
3

4
4

4
4

1
2
3

4
1

2
3

4
1

2

3
4

1
2

3
4
c=[ones(4,1) (1:4)'];
c=[c;c+d;c+d+d;c+3*d]
Page 2
Name and surname: ______________________________________________________________________
mask1= (c(:,1)==c(:,2))’
mask1  1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
e = [ eye(4,3) c(mask1)’; fix(rand(4)+2)]
1
0

0

0
e
2

2
2

2
0 0
1 0
0 1
0 0
2 2
2 2
2 2
2 2
1
2
3

4
2

2
2

2
f=a(mask1);
f(3,[3 5])= 2;
f(:,2)=[];
f=f(:)
  1
 0
 
 0
 
 3
 0
 
 2
f   4
 
 0
 0
 
 0
 
 0
 2
 
Page 3
Name and surname: ______________________________________________________________________
Question 2. (25 points) Functions
Part a) Function definition
Write a Matlab function, say count_range that calculates the number of values in a given double array (first
input argument) with a value between a double, say A (second input argument) and another double, say B
(third input argument) – in the range [A,B], inclusive- and returns this number as the output argument.
Notes:
1. If A or (and) B is an empty matrix, then set the corresponding value(s) to 0.
count_range([0 -1 0.5 2 -1.6;1 0 0 3 -2.9],1,[])
ans =
5
2. If A or (and) B is not a scalar, give a proper error message and hence abort.
count_range([0 -1 0.5 2 -1.6;1 0 0 3 -2.9],1,[2 3])
??? Error using ==> count_range
B (3rd argument) must be a scalar
3. B must be equal to or greater than A. Otherwise exchange the values of A and B.
count_range([0 -1 0.5 2 -1.6;1 0 0 3 -2.9],1,-1)
result =
6
4. If the given array is an empty matrix, the result must be 0.
count_range([],1,3)
result =
0
function cnt = count_range (ARR, A, B)
% count_range.m : This function counts the number of values in an array ARR
% with a value in the range [A,B]
% usage: count_range(ARR,A,B)
if isempty(A)
A=0;
end
if isempty(B)
B=0;
end
if (length(A)>1)
error('A (2nd argument) must be a scalar');
end
if (length(B)>1)
error('B (3rd argument) must be a scalar');
end
if (B<A)
T=B;
B=A;
A=T;
end
cnt=0;
[rn cn] = size(ARR);
for r=1:rn
for c=1:cn
num=ARR(r,c);
if num>=A && num<=B
cnt=cnt+1;
end
end
end
Page 4
Name and surname: ______________________________________________________________________
Part b) Function usage
Write Matlab program, say test_count_range that calls the function of part a) for any input matrix, ARR, any
input values of A and B correctly and displays the result with a proper explanation.
Consider the following sample run from the command window:
>> test_count_range
Please type a numeric array:[0 -1 0.5 2 -1.6;1 0 0 3 -2.9]
Please type a value for A:-1
Please type a value for B:1
The number of values in the given array in the given range is 6
% test_count_range.m: This program tests the count_range function
% for any input data (all arguments)
ARR=input('Please type a numeric array:');
A=input('Please type a value for A:');
B=input('Please type a value for B:');
RESULT=count_range(ARR,A,B);
disp(['The number of values in the given array in the given range is ' num2str(RESULT)]);
Page 5
Name and surname: ______________________________________________________________________
Question 3. (50 points) Problem solving and graphs
Let E denote illumination in lux, I denote intensity in candela, d denote distance in meters and α denote the
angle between the light beam and the normal of the plane, then the following equation is true:
E
I .Cos
d2
Your Matlab program should do the following:









Get a positive value for I (scalar) from the user (continue to ask until the value is positive)
Let α be a vector of angles between 0 and pi in increments of 0.01
Get a positive value for d (scalar) from the user (continue to ask until the value is positive)
Compute E.
Plot E versus α in degree (upper plot). Do not forget to put appropriate titles. Note the extra line
representing the x-axis in the coordinate system.
Now, Get a value between 30 and 60 for α in degrees (scalar) from the user (continue to ask until the
value is valid)
Obtain the distance vector, d. This vector should contain the same number of elements as α vector with
equal increments from 1 to 5.
Compute E.
Plot E versus d as the lower plot in the same figure. Do not forget to put appropriate titles.
Page 6
Name and surname: ______________________________________________________________________
teta=0:0.001:pi; %teta is in radians
alfa=180*teta/pi; % alfa is in degrees
tt=length(teta); %tt shows number of elements of the degree vector
dd=(5-1)/(tt-1); %dd shows the increment
w=1:dd:5; %w is the distance vector for second plot
%w=linspace(1,5,tt);
%input intensity (I) and distance (d1)
I=input('please type a positive value (I):');
while (I<=0)
I=input('please type a positive value (I):');
end
d1=input('please type a positive value (d):');
while (d1<=0)
d1=input('please type a positive value (d):');
end
% compute E1 for first plot
E1=I*cos(teta)/d1^2;
z=zeros(size(alfa)); %to plot the y=0 (x-axis) line
%input degree for second plot
tet=input('please type a value between 30-60 (teta in degrees):');
while (tet<30|tet>60)
tet=input('please type a value between 30-60 (teta in degrees):');
end
ang=tet*pi/180; %ang is in radians
%compute E for second plot
E=I*cos(ang)/w.^2;
%Graphs
subplot(2,1,1)
plot(alfa,E1,alfa,z,'k');
xlabel('teta (degrees)');
ylabel('illumination');
title('E vs teta');
subplot(2,1,2)
plot(w,E)
xlabel('distance (m)');
ylabel('illumination');
title('E vs d');
Page 7
Download