Lab 2 - Nick Mohrbacher - MAT 275 Lab
Table of Contents
Exercise 1 .......................................................................................................................... 1
Exercise 2 .......................................................................................................................... 3
Exercise 3 .......................................................................................................................... 4
Exercise 4 .......................................................................................................................... 5
Exercise 5 .......................................................................................................................... 6
Exercise 1
Part (a)
A=[5,-1,3;2,4,-7;6,1,8];
B=[12,0,7;3,-2,5;-1,9,10];
b=[16;36;17];
c=[1,2,3];
d=[4;3;2];
A*B
B*A
c*A
B*d
ans =
54 29 60
43 -71 -36
67 70 127
ans =
102 -5 92
41 -6 63
73 47 14
ans =
27 10 13
ans =
62
16
43
1
Lab 2 - Nick Mohrbach-
er - MAT 275 Lab
Part (b)
C=[B;A]
D=[B,d]
C =
12 0 7
3 -2 5
-1 9 10
5 -1 3
2 4 -7
618
D =
12 0 7 4
3 -2 5 3
-1 9 10 2
Part (c)
x=A\b
x =
5
3
-2
Part (d)
A(3,2)=0
A =
5 -1 3
2 4 -7
608
Part (e)
a=A(2,:)
a =
2
Lab 2 - Nick Mohrbach-
er - MAT 275 Lab
2 4 -7
Part (f)
B(:,3)=[]
B =
12 0
3 -2
-1 9
Exercise 2
Part (a)
% Display contents of geom_sum M-file.
type 'geom_sum.m'
% Assign values to input variables.
r = (1/3);
a = (5);
n = 8;
% Compute geometric sum for specified values of r,a, and n.
geom_sum(5,1/3,8)
function [S] = geom_sum(a,r,n)
%function to solve geometric sum a+ar+ar^n+...+ar^n+1
S=0; %initialize function
for k=0:n-1 %initialize counter from zero to n-1 by 1 incriment
S=S+a*r^k %function for geom_sum
end
end
S =
5
S =
6.6667
3
Lab 2 - Nick Mohrbach-
er - MAT 275 Lab
S =
7.2222
S =
7.4074
S =
7.4691
S =
7.4897
S =
7.4966
S =
7.4989
ans =
7.4989
Part (b)
n=[0 1 2 3 4 5 6 7]; %define n array
a=5; %define a
r=1/3;%define r
sum(a*r.^n) %output sum of function, should match part 2a
ans =
7.4989
Exercise 3
Part (a)
% Initiate product P.
4
Lab 2 - Nick Mohrbach-
er - MAT 275 Lab
P = 1;
% Define starting iteration index.
m = 1;
% Define stepsize of iteration.
k = 2;
% Define ending iteration index.
n = 15;
%initialize values for vector storage
value = 1;
%initialize vector
y = [];
% Compute product.
for i = m:k:n
P= P*(i); % muliply P by next element at each iteration (suppress
output)
value = i; %define value
y=[y,value]; %define vector
end
% Display product.
P
P =
2027025
Part (b)
S=prod(y) %S is the product of all of the stored values in vector y
S =
2027025
Exercise 4
% Initiate variables.
power = 3;
k = 1; % initiate counter
% Initiate vector V of powers of 3.
V = [];
% Compute powers of 3 and store in V.
5
Lab 2 - Nick Mohrbach-
er - MAT 275 Lab
while power < 3000 % specify condition of while-loop: stop iterating
once
% condition is no longer satisfied
power = 3^k; % compute next value of power at each iteration
V = [V,power]; % concatenate vector V with new element at each
iteration
k = k+1; % increment counter k
end
% Display vector V.
V %V will display its last value as higher than 3000 since a while
loop stop
%checks for validity prior to running loop (after running the
previous loop).
V =
Columns 1 through 6
3 9 27 81 243
729
Columns 7 through 8
2187 6561
Exercise 5
% Display contents of function f M-file.
type 'f2.m'
% Evaluate f at x = 1.
f2(1)
% Evaluate f at x = 2.
f2(2)
% Evaluate f at x = 3.
f2(3)
% Evaluate f at x = 4.
f2(4)
% Evaluate f at x = 7.
f2(7)
% Evaluate f at x = 10.
f2(10)
function [y] = f(x) %define function y=f(x)
if x==7 %initiate if staement for undefined values
disp ('The function y is undefined at x=7')
elseif x<=2 % first function in peacewise function parameters
y=exp(x-1);
elseif x>2 & x<=4 %second function parameters
y=x^2+x;
6