Beruflich Dokumente
Kultur Dokumente
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.
Solution: ans = 2. 3. 4. 5. 6.
Solution: ans = 12 10 8 6 4
Solution: ans = 12 5 -2
Solution: a = 1 and b = 2
Solution:
Technically, there is NO output (see the ; ).
However, 0.5 for: ans = 1. 1.5 2.0 2.5
Solution:
x is: 101 while y is 3.14
(a) (1 mark) What command would explain to you how the command nthroot works?
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.
(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’,’ ’)
c = 3
u = [ 2 4 6 ]
v = [ 1 2 2 ]
Solution: ans = 5 7 9
Solution: ans = 6 12 18
Solution: ans = 3 6 8
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’
(a) (1 mark) Determine the number of rows and columns in A and store both in ap-
propriately named variables.
(b) (1 mark) Determine the total number elements in A and store in an appropriately
named variable.
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))
Solution:
sum(sum(A))
Solution:
ans =
- 2. 1.
4. - 3.
Solution:
ans =
- 2. 4.
1. - 3.
Solution:
ans =
8 7
Solution:
ans =
- 2. 0.
0. 1.
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):
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
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])
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’)
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!
3x2y + 4z = 10
x+y =2
x + 5y 4z + 4 = 0
Solution:
result =
-0.0000
2.0000
3.5000
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:
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: