Sie sind auf Seite 1von 11

COSC 2836 TEST #2 (MatLab)

Computer Software for Sciences - Instructor: Aaron Langille


Wednesday, March 14th , 2012. 1:00 p.m. – 2:15 p.m.

Name (please print)


Student Id Number

1. What is the expected output (i.e., what would be seen in the MatLab command window)
of the following MatLab commands:
(a) (1 mark) floor(4.59)

Solution: ans = 4.

(b) (1 mark) abs(1-2)

Solution: ans =1.

(c) (1 mark) >>2 : 6

Solution: ans = 2. 3. 4. 5. 6.

(d) (1 mark) >>x=-10.456; x - fix(x)

Solution: ans = -0.456

(e) (1 mark) >>12 : -2 : 3

Solution: ans = 12 10 8 6 4

(f) (1 mark) >>mod(10,6)

Solution: ans = 12 5 -2

(g) (1 mark) >>[a,b] = rat(0.5)

Solution: a = 1 and b = 2

(h) (1 mark) >>1 : 0.0138 : 2.7;

Solution:
Technically, there is NO output (see the ; ).
However, 0.5 for: ans = 1. 1.5 2.0 2.5

(i) (2 marks) >>fprintf(’x is: %d while y is %1.2f\n’, 101, 3.141)

Solution:
x is: 101 while y is 3.14

Test #2, Page 1 of 11 Marks earned: out of a possible 10 marks


2. Give the MatLab command(s) that will perform the following tasks (you do not need to
show what the output would be):

(a) (1 mark) What command would explain to you how the command nthroot works?

Solution: help nthroot

(b) (1 mark) Create a 3 ⇥ 6 matrix of uniform random numbers, each of which is


between 0 and 10.

Solution: rand(3,6)*10

(c) (1 mark) Write a 2-D matrix of values, named myMatrix, out to a text file called
matrix.txt using a semi-colon (;) to separate the data values.

Solution: dlmwrite(’matrix.txt’,myMatrix,’;’)

(d) (1 mark) Create a vector with 119 elements ranging from ⇡ to 2⇡.

Solution: linspace(pi,2*pi,119)

(e) (1 mark) Create a vector ranging from ⇡ to ⇡ 5 with elements evenly spaced every
1.75 units.

Solution: v2=-pi:1.75:pi^5

(f) (1 mark) Save all commands and resulting output, from this point forward, to a
text file called “output1.txt”.

Solution: diary(’output1.txt’)

(g) (1 mark) Determine both the minimum value AND the location where this value
occurs in an arbitrary vector w.

Solution: [minimum, location] = min(v)

(h) (1 mark) Read the values from text file imagedata.txt and store them in a variable
A. Assume that the data elements are separated by spaces.

Solution: A = dlmread(’imagedata.txt’,’ ’)

Test #2, Page 2 of 11 Marks earned: out of a possible 8 marks


3. Suppose that we define the coefficient c and the vectors u and v as follows:

c = 3
u = [ 2 4 6 ]
v = [ 1 2 2 ]

What would be the output of the following MatLab commands:


(a) (1 mark) >>c + u

Solution: ans = 5 7 9

(b) (1 mark) >>c * u

Solution: ans = 6 12 18

(c) (1 mark) >>u + v

Solution: ans = 3 6 8

(d) (1 mark) >>u * v

Solution: ans = error

(e) (1 mark) >>u .^ v

Solution: ans = 2 16 36

Give the MatLab command(s) that will perform the following tasks (you do not
need to show what the output would be):

(f) (1 mark) Create a vector t whose elements are the product of each element in u
multiplied by the corresponding element in v (i.e., ti = ui ⇥ vi ).

Solution: t=u .* v

(g) (1 mark) Create a variable t that stores the scalar product (dot product) between
u and v.

Solution: t=u * v’

Test #2, Page 3 of 11 Marks earned: out of a possible 7 marks


4. Suppose you have a 2-D matrix, A, of unknown dimensions and unknown values. Give
the MatLab command(s) that will perform the following tasks (you do not need to show
what the output would be):

(a) (1 mark) Determine the number of rows and columns in A and store both in ap-
propriately named variables.

Solution: [NbRows, NbCols] = size(A)

(b) (1 mark) Determine the total number elements in A and store in an appropriately
named variable.

Solution: length(A(:)) or NbRows * NbCols

(c) (1 mark) Determine the value of the smallest element in A.

Solution: min(min(A))

(d) (1 mark) Create a variable z that contains the value of the element found on the
3rd column and 25th row of A. Assume this is within the boundaries of the matrix
indices.

Solution: z=A(25,3)

(e) (1 mark) Create a variable z that contains the values of all the elements found in
the 3rd column of A.

Solution: z=A(:,3)

(f) (1 mark) Create a matrix B that is the same size as A but is completely filled with
1’s.

Solution: B=ones(size(A))

(g) (1 mark) Determine the sum of all elements in matrix A:

Solution:
sum(sum(A))

Test #2, Page 4 of 11 Marks earned: out of a possible 7 marks


5. Suppose that we define the vectors u and v and the matrices A and B as follows:

>>u = [-2 1] >>v = [4 -3]


u = -2. 1. v = 4. - 3.

>>A = [-1 -2; 3 4] >>B = [8 5; 6 7]


A = B =
-1. -2. 8. 5.
3. 4. 6. 7.

What would be the output of the following MatLab commands:


(a) (1 mark) >>[ u; v ]

Solution:
ans =
- 2. 1.
4. - 3.

(b) (1 mark) >>[ u’ v’ ]

Solution:
ans =
- 2. 4.
1. - 3.

(c) (1 mark) >>max(B)

Solution:
ans =
8 7

(d) (1 mark) >>diag(u)

Solution:
ans =
- 2. 0.
0. 1.

(e) (1 mark) >>diag(A)

Solution:
ans =
- 1.
4.

Give the MatLab command(s) that will perform the following tasks by using a
combination of u, v, A or B (you do not need to show what the output would be):

(f) (1 mark) Create a matrix, C, that is the product between A and u:

Solution:
C=A * u’

(g) (1 mark) Create the following matrix, C, using the vectors and matrices above:
C =

-2 4 8 6 -1 3
1 -3 5 7 -2 4

Test #2, Page 5 of 11 Marks earned: out of a possible 7 marks


Solution:
C=[ u’ v’ B’ A’ ]

Test #2, Page 6 of 11 Marks earned: out of a possible 0 marks


6. Consider the mathematical function: f (x) = xsin(e x )
(a) (3 marks) Without using anonymous functions, show the commands necessary
to determine the values of f at x = 9 and x = 12.5 and to plot the function in
the range 50  x  50.

Solution:
x = 9;
(x)*(sin(exp(-x)))
x = -12.5;
(x)*(sin(exp(-x)))
x = [ -50:0.1:50 ];
plot(x, (x).*(sin(exp(-x))))

(b) (3 marks) Using anonymous functions, show the commands necessary to deter-
mine the values of f at x = 9 and x = 12.5 and to plot the function in the range
50  x  50.

Solution:
f = @(x) (x)*(sin(exp(-x)));
f(9)
f(-12.5)
fplot(f, [-50 50])

Test #2, Page 7 of 11 Marks earned: out of a possible 6 marks


7. (8 marks) How would you create the following single graphic that consists of four distinct
plots. Do you best to duplicate my output completely. Assume that all plots are in colour.
Some details of each are clarified below. Assume that you have already executed the following
commands:

t = [-pi:0.1:pi]; % used for 2-D graphs; [x,y,z] = peaks(); % used for 3-D graphs

a) A single plot of sin(t), sin(2t) and sin(3t). All three lines are di↵erent colours.
b) A parametric graph of cos(t) and sin(t). Axis scaling is constrained to equal dimensions.
c) A pseudo-color graph with with interpolated shading and a contour graph overlaid. The contour
graph has lines forced to black. No axes are displayed.
d) A combined surface and contour plot.
If you can’t remember how to combine them into a single graphic, at least try to do them separately
(minus 3 marks).

Solution:

subplot(2,2,1)
plot(t, [ sin(t); sin(2*t); sin(3*t) ])
title(’a) 3 functions’)
subplot(2,2,2)
plot(sin(t),cos(t))
axis equal
grid
title(’b) parametric graph’)
subplot(2,2,3)
contour(x,y,z, ’k’);
hold on
pcolor(x,y,z);
shading interp
axis off
title(’c) pseudo-color + contour’)
subplot(2,2,4)
surfc(x,y,z)
xlabel(’x-axis’), ylabel(’y-axis’), zlabel(’z-axis’)
title(’d) surface + contour’)

Test #2, Page 8 of 11 Marks earned: out of a possible 8 marks


8. (6 marks) Consider each of the following while-loop code fragments. For each fragment
show the output that will be produced when the fragment is executed:
(a) x=5;
while (x <= 8)
y = x + (x-1);
disp(y)
x = x + 1;
end

Output produced:

Solution:
9

11

13

15

(b) a=0;
while (a > 0)
b = atan2(a);
disp(b)
a = a - 2;
end

Output produced:

Solution:
% Nothing, inititial condition is not true

(c)
(d) var1=6;
while (var1 >= 5)
var2 = var1 / 2;
disp(var2)
end

Output produced:

Solution:
3
3
3
3
...infinite loop!

Test #2, Page 9 of 11 Marks earned: out of a possible 6 marks


9. (5 marks) Show all of the steps required to find a solution to the following set of linear
equations (the ones from the Excel test) in Matlab. You don’t need to show a solution
for the set of equations, just show how you would have Matlab solve it. DO NOT write
a user-defined function for this question, just provide the Matlab statements.

3x2y + 4z = 10
x+y =2
x + 5y 4z + 4 = 0

Solution:

>> lhs = [ 3 -2 4; 1 1 0; 1 5 -4];


>> rhs = [ 10; 2; -4 ];
>> result = lhs \ rhs;
>> result

result =

-0.0000
2.0000
3.5000

>> combined = [ lhs rhs ];


>> rref(combined)

ans =

1.0000 0 0 0
0 1.0000 0 2.0000
0 0 1.0000 3.5000

10. (5 marks) Write a user defined function called func1 that takes two input arguments x
and y. func1 should produce one output result that is computed based on the function
f (x, y) below:
8
>
> x2 + y x<0 and y 10
>
< x2 + y 2 x<0 and y < 10
f (x, y) =
>
>
> x+y x 0 and y 10
:
x + y2 x 0 and y < 10
If you can’t remember the Matlab function structure, at least write the if statements
that compute the final value based on some variables x and y.

Solution:

function [ result ] = func1(x,y)

if x<0 & y >= 10


result = x^2 +y;
elseif x<0 & y< 10
result = x^2 + y^2;
elseif x>= 0 & y >= 10
result = x+y;
else
result = x + y^2;
end

Test #2, Page 10 of 11 Marks earned: out of a possible 10 marks


COSC 2836 Student Name:
Software for Sciences March 14, 2012

11. (8 marks) In this question you will write a MatLab function. You have a choice on
which of the functions below to write. However, they are worth di↵erent amounts of
marks. Only option b) will allow you to maximize the marks for this question. Select
only ONE of the options below. If you do more than 1 I will only mark the
first one on the page. Circle the one you have attempted.:
(a) Write a MatLab function that takes in one argument, a vector or 2-D matrix and
returns a single value, the highest value in the vector or matrix. Use nested for
loops to accomplish this task. Assume that all of the values in the vector or matrix
are unique. Maximum = 5 out of 8 marks.
(b) Write a MatLab function that takes in one argument, a vector or 2-D matrix and
returns a vector of three values, the highest value in the vector or matrix AND the
position (two coordinates) in the vector or matrix where it occurs. Use nested for
loops to accomplish this task. Assume that all of the values in the vector or matrix
are unique. Maximum = 8 out of 8 marks.

Solution:

Test #2, Page 11 of 11 Marks earned: out of a possible 8 marks

Das könnte Ihnen auch gefallen