Lab 2 - Jeanie Kim - MAT 275 Lab
Table of Contents
Exercise 1 .......................................................................................................................... 1
Exercise 2 .......................................................................................................................... 2
Exercise 3 .......................................................................................................................... 3
Exercise 4 .......................................................................................................................... 4
Exercise 5 .......................................................................................................................... 4
Exercise 1
%Given Matrices
A= [-8,-5,-1;1,-6,-8;8,2,5];
B = [-1,12,8;18,14,-4;-6,2,-8];
b = [20;3;35];
c = [2,5,1];
d = [5,;-1;-1];
% Part (a)
A.*B
B.*A
c.*A
B.*d
ans =
8 -60 -8
18 -84 32
-48 4 -40
ans =
8 -60 -8
18 -84 32
-48 4 -40
ans =
-16 -25 -1
2 -30 -8
16 10 5
ans =
1
Lab 2 - Jeanie Kim - MAT 275 Lab
-5 60 40
-18 -14 4
6 -2 8
Part(b)
C = [A;B];
D = [B,d];
Part (c) NOTE: Use the "backslash" command. NOT the division operator "/".
x = A\b %Solves system Ax = b
x =
2.4054
-9.2162
6.8378
Part (d)
A(2,3)=0;%Replaces (2, 3)-entry of the matrix A by 0
Part (e)
a=A(2,:); %Extract the 2nd row of the matrix A and store it as a
vector a.
Part (f)
B(3,:)=[];
Exercise 2
Part (a)
type 'geomsum1.m' %displays contents of geosum1.m
r = -2/3; %Displays value of r
a = 4; %Displays value of a
n = 12; %Displays value of n
% Compute geometric sum for specified values of r,a, and n.
geomsum1(r,a,n) %Computes geometric sum of defined values of r,a, and
n using loop
function S = geomsum1(r,a,n) %Creates function S and defines it as the
geometric sum with values r,a,n
geo = []; %Sets geo as an empty vector
for i = 1;n %Sets beginning and ending values for n
2
Lab 2 - Jeanie Kim - MAT 275 Lab
g= a*(r^n); %Gives equation for it to follow
geo = [geo,g];%
end
S = sum(geo) %takes the sum of geo
n =
12
S =
0.0308
ans =
0.0308
Part (b)
geomsum2(r,a,n) %Computes geometric sum of the defined values of r,a,
and n using sum command
S =
0.9884
ans =
0.9884
Exercise 3
Part (a)
P = 1; %Initiates product P
m = 1; %Defines the starting iteration index
k = 2; %Defines stepsize of iteration.
n = 19; %Define ending iteration index.
for i = m:k:n %Sets explicit increment for loop
P = P*i; %Multiplies P by next element at each iteration
end
disp(P) % Display product.
654729075
3
Lab 2 - Jeanie Kim - MAT 275 Lab
Part (b)
p = prod(i) %Evalutes product of the first 10 odd numbers
p =
19
Exercise 4
power = 3; %Gives starting value and will be done to some power of k
defined below
k = 3; % Power that starting value is subject to
v = [];%Sets vector v to empty vector
while power < 10^6 %Specifies condition where it stops
v(k) = power; %Evaluate kth entry of the vector v
k = k+1; %Adds one to the increment
power = 3^k; %Computes next value of power at each iteration
end
disp(v) %Displaies vector v.
Columns 1 through 6
0 0 3 81 243
729
Columns 7 through 12
2187 6561 19683 59049 177147
531441
Exercise 5
type 'f.m' % Displays contents of function f M-file
f(0) %Evaluates f at 0
f(1) %Evaluates f at 1
f(1.5) %Evaluates f at 1.5
f(2) %Evaluates f at 2
f(3) %Evaluates f at 3
f(4) %Evaluates f at 4
4
Lab 2 - Jeanie Kim - MAT 275 Lab
function y=f(x) %Sets function to follow
if x<=1 %If x is less than or equal to 1
y=3-3*x; %The function to be evaluated in that interval
elseif x<=2 %if x is less than or equal to 2
y=exp(x-3) %The function to be evalued in that interval
elseif x>2 %If x is greater than 2
y= (x)/(x-3) %The function to be evalued in that interval
elseif x==3 %If x is greater that 3
disp('the function is undefined at x=3'); %Displays the function
is undefined at x=3 only if x = 3
end %ends function
ans =
3
ans =
0
y =
0.2231
ans =
0.2231
y =
0.3679
ans =
0.3679
y =
Inf
ans =
Inf
5