Lab 2 - Argon Patino -
MAT 275 Section:12068
Table of Contents
Exercise 1 .......................................................................................................................... 1
Exercise 2 .......................................................................................................................... 4
Exercise 3 .......................................................................................................................... 5
Exercise 4 .......................................................................................................................... 6
Exercise 5 .......................................................................................................................... 7
The End!!! ......................................................................................................................... 8
Exercise 1
Enter the following matrices and vectors in MATLAB
% Set Matrices for A,B,b,c,d
A = [8,4,-8;8,5,-3;6,8,-9] % 3 x 3 Matrix A
B = [15,-3,-8;5,6,-8;1,-2,-8] % 3 x 3 Matrix B
b = [4;-13;7] % 3 x 1 Matrix b
c = [5,0,-2] % 1 x 3 Matrix c
d = [2;4;0] % 3 X 1 Matrix d
% Answer (a)
AB = A.*B % Matrix A*B
BA = B.*A % Matrix B*A
cA = c.*A % Matrix c*A
Bd = B.*d % Matrix B*d
% Answer (b)
C = [A;B] % 6 x 3 Matrix C
D = [B,d] % 3 x 4 Matrix D
% Answer (c)
x = A\b % Solve System Ax = b with backslash command.
% Answer (d)
A(1,2) = 0 % Replace 1 row 2 column. A(1,2) with 0.
% Answer (e)
a = A(2,:) % Extract 2nd row of the matrix A.
Lab 2 - Argon Patino -
MAT 275 Section:12068
% Answer (f)
B(:,2) = [] % Delete 2nd column of matrix B.
A =
8 4 -8
8 5 -3
6 8 -9
B =
15 -3 -8
5 6 -8
1 -2 -8
b =
4
-13
7
c =
5 0 -2
d =
2
4
0
AB =
120 -12 64
40 30 24
6 -16 72
BA =
120 -12 64
40 30 24
6 -16 72
cA =
Lab 2 - Argon Patino -
MAT 275 Section:12068
40 0 16
40 0 6
30 0 18
Bd =
30 -6 -16
20 24 -32
0 0 0
C =
8 4 -8
8 5 -3
6 8 -9
15 -3 -8
5 6 -8
1 -2 -8
D =
15 -3 -8 2
5 6 -8 4
1 -2 -8 0
x =
-2.1250
-1.1071
-3.1786
A =
8 0 -8
8 5 -3
6 8 -9
a =
8 5 -3
B =
15 -8
5 -8
1 -8
Lab 2 - Argon Patino -
MAT 275 Section:12068
Exercise 2
%Write a function file called geomsum1 which accepts the values of r,
a and n as input arguments and uses a for loop to return the sum of
the first n terms of the geometric series.
% Part (a)
%Display contents of geomsum1 M-file.
type 'geomsum1.m'
% Assign values to input variables.
r = 1/3;
a = 7;
n = 9;
% Compute geometric sum for specified values of r,a, and n.
geomsum1(r,a,n)
% Function to compute the geometric series
function SE= geomsum1(r,a,n)
SE = 0; % Initialize the sum value
for k=0:1:n ; % Use for loop starts at 0 compute the geometric series
by 1 up to n
SE=SE+a*r^k;
end
ans =
10.4998
% Part (b)
%Write a function file called geomsum2 which accepts the values of r,
a and n as input arguments and uses the built in command sum to find
the sum of the first n terms of the geometric series.
% Display contents of geomsum1 M-file.
type 'geomsum2.m'
% Assigned values (r,a,n) are same as previous input variables.
r = 1/3;
a = 7;
n = 9;
Lab 2 - Argon Patino -
MAT 275 Section:12068
% Compute geometric sum for specified values of r,a, and n.
geomsum2(r,a,n)
% Function to compute the geometric series
function [Y] = geomsum2(r,a, n)
e=0:1:n-1 % Define the vector
R=r.^e % Evaluate the vector
Y=sum(a.*R); % Compute the geometric series
end
e =
0 1 2 3 4 5 6 7 8
R =
Columns 1 through 7
1.0000 0.3333 0.1111 0.0370 0.0123 0.0041 0.0014
Columns 8 through 9
0.0005 0.0002
ans =
10.4995
Exercise 3
%Write a script file that evaluates the product of the first 7 even
numbers using a "for" loop.
% Part (a)
% Initiate product P.
P = 1;
% Define starting iteration index.
m = 2;
% Define stepsize of iteration.
k = 2;
Lab 2 - Argon Patino -
MAT 275 Section:12068
% Define ending iteration index.
n = 14;
% Compute product.
for i = m:k:n % Use for loop starts at m compute the geometric
series by k up to n
P = P * i; % muliply P by next element at each iteration.
end
% Display product of first 7 even numbers.
P
P =
645120
% Part (b)
% Evaluate the product of the first 7 even numbers using a single
MATLAB command
% Display product of first 7 even numbers using single command.
P = prod(m:k:n)
P =
645120
Exercise 4
Script file that creates a row vector v containing all the powers of 4 that are (strictly) less than 10^6
% Initiate variables.
power = 4;
k = 4^power; % initiate counter
% Initialize the vector v to the empty vector
v = [];
% Compute powers and store in v.
while power < 1000000 % specify condition of while-loop
v= [v,power]; % evaluate entry of the vector v
k = 4^power; % Increment counter k
power = power*4; % compute next value of power at each
iteration
end
% Display vector v.
v
Lab 2 - Argon Patino -
MAT 275 Section:12068
v =
Columns 1 through 6
4 16 64 256 1024
4096
Columns 7 through 9
16384 65536 262144
Exercise 5
Write a function file that creates the following piecewise function as M-file.
% Display contents of function f M-file.
type 'f.m'
% Evaluate f at the given vaue of 1.
f(1)
% Evaluate f at the given value of 2.
f(2)
% Evaluate f at the given value of 2.5.
f(2.5)
% Evaluate f at the given value of 4.
f(4)
% Evaluate f at the given value of 6.
f(6)
% Evaluate f at the given value of 7.
f(7)
%% Write a function file that creates the following piecewise function
function y = f ( x )
if x==6
disp ('the function is undefined at x = 6')
elseif x <= 2
y = exp(x-6);
else if (x<2 & x<=4)
y = 3*x-2 ;
else if (x<4 & x~=6)
y = x/(x-6);
end
end
end
end
ans =
0.0067