LAB 2 - Emiko Groves - MAT 275
Exercise 1
Part (a)
A=[9,2,-7;9,8,-4;5,-1,-4];
B=[7,-5,12;-8,19,20;-8,15,20];
b=[40;-11;38];
c=[1,-3,-1];
d=[5;4;-3];
A*B
ans = 3×3
103 -112 8
31 47 188
75 -104 -40
B*A
ans = 3×3
78 -38 -77
199 116 -100
163 84 -84
c*A
ans = 1×3
-23 -21 9
B*d
ans = 3×1
-21
-24
-40
Part (b)
C=[A;B]
C = 6×3
9 2 -7
9 8 -4
5 -1 -4
7 -5 12
-8 19 20
-8 15 20
D=[B d]
D = 3×4
7 -5 12 5
-8 19 20 4
-8 15 20 -3
1
Part (c)
x=A\b
Part (d)
A(3,1)=0
A = 3×3
9 2 -7
9 8 -4
0 -1 -4
Part (e)
a=A(:,2)
a = 3×1
2
8
-1
Part (f)
B(:,1)=[]
B = 3×2
-5 12
19 20
15 20
Exercise 2
Part (a)
Display contents of geomsum1 M-file
type 'geomsum1.m'
function total_sum = geomsum1(r, a, n)
total_sum = 0;
for k = 0:n-1
term = a * r^k;
total_sum = total_sum + term;
end
end
Assign values to input variables
r = -3/7;
a = 5;
n=7;
Compute geometric sum for specified values of r,a, and n.
2
geomsum1(r, a, n)
ans =
3.5093
Part (b)
type 'geomsum2.m'
function S = goemsum2(r, a, n)
e = [0 : n-1];
R = r.^e;
S = sum(a * R);
end
r = -3/7;
a = 5;
n=7;
geomsum2(r, a, n)
ans =
3.5093
Exercise 3
Part (a)
Initiate product P.
P = 1;
Define starting iteration index.
m = 1;
Define stepsize of iteration.
k = 2;
Define ending iteration index.
n = 17;
Compute product.
for i = m:k:n
P = P * i; % muliply P by next element at each iteration (suppress
output)
end
3
Display product.
disp(P)
34459425
NOTE: i = m:k:n is also a vector! A for-loop essentially iterates through each element of a vector. Keep this in
mind for part (b).
Part (b)
P = prod(1:2:17)
P =
34459425
Exercise 4
Initiate variables.
power = 4;
k = 1; % initiate counter
Initialize the vector v to the empty vector
v = [];
Compute powers and store in v.
while power < 10^7 % specify condition of while-loop: stop iterating once
% condition is no longer satisfied
v(k) = power; % evaluate kth entry of the vector v
k = k + 1; % increment counter k
power = 4^k; % compute next value of power at each iteration
end
Display vector v.
disp(v)
4 16 64 256 1024 4096 16384 65536 262144 1048576 4194304
Exercise 5
NOTE: Here, you must create a separate function M-file f that accepts one input argument, a value of the
variable x. You may want to start creating a new folder for each lab as we will be using f multiple times to define
different functions. Please refer to the example on the last page of the protocol before attempting this problem.
Delete this note before submission.
Display contents of function f M-file.
4
type 'f.m'
function y = f(x)
if x <= 3
y = exp(x - 12);
elseif x > 3 && x <= 6
y = 3 * x + 2;
elseif x > 6 && x ~= 12
y = x / (x - 12);
elseif x == 12
disp("The function is undefines at x = 12");
y = NaN;
else
y = NaN;
end
end
Evaluate f at the given vaue of x.
f(2)
ans =
4.5400e-05
Evaluate f at the given value of x.
f(3)
ans =
1.2341e-04
% Evaluate f at the given value of x.
f(3.5)
ans =
12.5000
Evaluate f at the given value of x.
f(6)
ans =
20
Evaluate f at the given value of x.
f(12)
The function is undefines at x = 12
ans =
NaN
Evaluate f at the given value of x.
5
f(13)
ans =
13
6