• 0

# MAT 275 lab 2 Exercise 1.pdf

Is there anything else you׳d like to ask?
Our top-rated tutors can help you.

Click here to post a question
Related Documents
1 / 5100%
LAB 2 - Your Name - MAT 275
Exercise 1
A=[-7,-7,-4;-7,-6,-4;-8,-6,-6];
B=[-3,7,-8;17,-5,18;11,-4,11];
b=[14;-1;-10];
c=[2,5,-2];
d=[-1;0;-3];
Part (a)
A*B
ans = 3×3
-142 2 -114
-125 -3 -96
-144 -2 -110
B*A
ans = 3×3
36 27 32
-228 -197 -156
-137 -119 -94
c*A
ans = 1×3
-33 -32 -16
B*d
ans = 3×1
27
-71
-44
Part (b)
C=[A;B]
C = 6×3
-7 -7 -4
-7 -6 -4
-8 -6 -6
-3 7 -8
17 -5 18
11 -4 11
D=[B,d]
D = 3×4
-3 7 -8 -1
17 -5 18 0
11 -4 11 -3
1
Part (c)
NOTE: Use the 'backslash' command, NOT the division operator '/' .Delete these notes before submission.
x=A\b
x = 3×1
14.6000
-15.0000
-2.8000
Part (d)
A(2,3)=0
A = 3×3
-7 -7 -4
-7 -6 0
-8 -6 -6
Part (e)
a=A(2,:)
a = 1×3
-7 -6 0
Part (f)
A(:,3)=[]
A = 3×2
-7 -7
-7 -6
-8 -6
Exercise 2
Part (a)
Display contents of geomsum1 M-file
type 'geomsum1.m'
function y = geomsum1(r,a,n)
y=0;
for i = 0:n-1
y = y + a*(r^i);
end
end
Assign values to input variables
r = 5/8;
a = 4;
n = 8;
2
Compute geometric sum for specified values of r,a, and n.
out = geomsum1(r,a,n)
out = 10.4183
Part (b)
type 'geomsum2.m'
function y = geomsum2(r,a,n)
e=[0:n-1];
R=r.^e;
y=sum(a.*R);
end
geomsum2(r,a,n)
ans = 10.4183
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
Display product.
P
P = 34459425
Part (b)
3
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.
v
v = 1×11
4 16 64 256 1024 4096
Exercise 5
Display contents of function f M-file.
type 'f.m'
function y = f(x)
if x <= 3
y =exp(x-7);
elseif x <= 6
y = 3-2*x;
elseif x == 7
disp('The function is undefined at x = 7')
else
y = x/(x-7);
end
end
Evaluate f at the given vaue of x.
f(2)
4
ans = 0.0067
Evaluate f at the given value of x.
f(3)
ans = 0.0183
% Evaluate f at the given value of x.
f(3.5)
ans = -4
Evaluate f at the given value of x.
f(6)
ans = -9
Evaluate f at the given value of x.
f(7)
The function is undefined at x = 7
Evaluate f at the given value of x.
f(8)
ans = 8
5