Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Math270

Worksheet #2

Read through and copy and paste onto the Octave/FreeMat script.
% BEGIN
% Row Reducing Matrices in MatLab
% Consider the System:
% x+4y+3z=10
% 2x+y-z=-1
% 3x-y-4z=11
% Matrix of coefficients:
C=[1, 4, 3; 2, 1, -1; 3, -1, -4]
% Constant vector:
b=[10;-1;11]
% You can create the augmented matrix:
A=[C,b]
% You could have also created the augmented matrix by
M=[1, 4, 3, 10; 2, 1, -1, -1; 3, -1, -4, 11]
% To “access” specific element in the matrix,
% you can find the row and column.
% Example, the element in the 2nd row and 3rd column:
M(2,3)
% To “access” a column,
M(:,2)
% To “access a row,
M(3,:);
% From here, you can perform the elementary row operations
% Switch the 2nd and 3rd rows:
M([2,3],:)=M([3,2],:)
% Multiply the 3rd row by -2:
M(3,:)=-2*M(3,:)
% Multiply the first row by -3, add to row 2
% then replace row 2
M(2,:)=-3*M(1,:)+M(2,:)
% Be more general:
% Use element in row 3 to eliminate the leading coeff
M(3,:)=-M(3,1)*M(1,:)+M(3,:)
% Do the same to clear the next column
% But if the pivot is not 1, you need to divide
M(3,:)=-M(3,2)/M(2,2)*M(2,:)+M(3,:)
% Now compare by using "upper triangular"
triu(A)
% for the reduced row echelon form, use rref
rref(A)
% END

Assignment:
Use the row reduction steps in MatLab to solve the system:
2𝑥2 + 3𝑥3 = 8
2𝑥1 + 3𝑥2 + 𝑥3 = 5
𝑥1 − 𝑥2 − 2𝑥3 = −5
Save all your commands in a ‘script’ file named “LastNameFirstInitial_ws2.m”

Extra:
Some of you are computer majors, and for those who are not, you still could use some experience in
writing computer programs. Challenge yourself by finding out how to write “functions” in MatLab and
write three functions that (1) swap rows, (2) scale rows, and (3) replace a row by adding a multiple of
another row. Challenge, part 2: write a program (function) that uses these elementary row operations
that row reduces a matrix showing the steps (matrices) to get there.

You might also like