clear all,clc
echo on
% Quiz 2 solutions:
% Problem 1:
X = [3 2 6 8]'
Y = [4 1 3 5]'
X.^Y
Z=X.*Y
X'*Y-Z
% Problem 2:
10/2\5-3+2*4
3^2/4
2+floor(6/9+3*2)/2-3
% Problem 3:
% Part a:
% To calculate the sum of the elements in a vector.
x = [1 8 3 9 0 1]
add=0;
for i = 1:length(x);
add=add+x(i);
end;
add
y=sum(x)
% Part b:
% To calculate the running sum and check with cumsum(x)
x = [1 8 3 9 0 1]
add=0;
for i = 1:length(x);
add=add+x(i);
z(i)=add;
end;
z
cumsum(x)
% Part c:
% To calculate sine values of elements in the vector
sin(x)
% Quiz 2 solutions:
% Problem 1:
X = [3 2 6 8]'
X =
3
2
6
8
Y = [4 1 3 5]'
Y =
4
1
3
5
X.^Y
ans =
81
2
216
32768
Z=X.*Y
Z =
12
2
18
40
X'*Y-Z
ans =
60
70
54
32
% Problem 2:
10/2\5-3+2*4
ans =
6
3^2/4
ans =
2.2500
2+floor(6/9+3*2)/2-3
ans =
2
% Problem 3:
% Part a:
% To calculate the sum of the elements in a vector.
x = [1 8 3 9 0 1]
x =
1 8 3 9 0 1
add=0;
for i = 1:length(x);
add=add+x(i);
add=add+x(i);
add=add+x(i);
add=add+x(i);
add=add+x(i);
add=add+x(i);
end;
add
add =
22
y=sum(x)
y =
22
% Part b:
% To calculate the running sum and check with cumsum(x)
x = [1 8 3 9 0 1]
x =
1 8 3 9 0 1
add=0;
for i = 1:length(x);
add=add+x(i);
z(i)=add;
add=add+x(i);
z(i)=add;
add=add+x(i);
z(i)=add;
add=add+x(i);
z(i)=add;
add=add+x(i);
z(i)=add;
add=add+x(i);
z(i)=add;
end;
z
z =
1 9 12 21 21 22
cumsum(x)
ans =
1 9 12 21 21 22
% Part c:
% To calculate sine values of elements in the vector
sin(x)
ans =
0.8415 0.9894 0.1411 0.4121 0 0.8415