0% found this document useful (0 votes)
30 views

Lab 4

The document describes two functions, type2m and type3m, that perform elementary row operations of multiplying a row by a constant or adding a row multiplied by a constant to another row. It then presents a general function called myref that uses these functions and loops to reduce any n×(n+1) matrix to row echelon form. Examples are given showing the myref function successfully putting random matrices into row echelon form.

Uploaded by

smartay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lab 4

The document describes two functions, type2m and type3m, that perform elementary row operations of multiplying a row by a constant or adding a row multiplied by a constant to another row. It then presents a general function called myref that uses these functions and loops to reduce any n×(n+1) matrix to row echelon form. Examples are given showing the myref function successfully putting random matrices into row echelon form.

Uploaded by

smartay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

These two functions perform basic elementary row operations for multiplying a row by a constant which

is type2m or to multiply a row by a constant and add it to another row which is type3m.

function out = type2m(n,j,a)


out = eye(n);
out(j,j) = a;

end

function out = type3m(n,j,k,c)

out=eye(n);
out(j,k)=c;

end

If we want to create a general equation that will help us reduce any n x (n+1) matrix to row echelon
form we can take the basic functions and create a general one using loops.

function A=myref(B)
[n,m]=size(B);
for j=1:n
for k=1:n
if j<=k
B=type3m(n,k,j,-B(k,j)/B(j,j))*B;
B=type2m(n,j,1/B(j,j))*B;

end
end
end
A=B;

Thus we see from examples how this truly works:

A=rand(4,5)

A=

0.0046 0.0844 0.4314 0.1455 0.5499

0.7749 0.3998 0.9106 0.1361 0.1450

0.8173 0.2599 0.1818 0.8693 0.8530


0.8687 0.8001 0.2638 0.5797 0.6221

>> myref(A)

ans =

1.0000 18.2201 93.0930 31.4053 118.6521

0 1.0000 5.1919 1.7640 6.6914

0 0 1.0000 16.4848 29.0630

0 0 0 1.0000 1.7264

>> A=rand(6,7)

A=

0.3510 0.1839 0.4909 0.7803 0.9421 0.8212 0.6477

0.5132 0.2400 0.4893 0.3897 0.9561 0.0154 0.4509

0.4018 0.4173 0.3377 0.2417 0.5752 0.0430 0.5470

0.0760 0.0497 0.9001 0.4039 0.0598 0.1690 0.2963

0.2399 0.9027 0.3692 0.0965 0.2348 0.6491 0.7447

0.1233 0.9448 0.1112 0.1320 0.3532 0.7317 0.1890

>> myref(A)

ans =
1.0000 0.5240 1.3987 2.2232 2.6843 2.3399 1.8457

0 1.0000 7.8823 25.9056 14.5352 40.8768 17.1145

0 0 1.0000 3.2404 1.8925 5.0424 2.0135

0 0 0 1.0000 0.7017 1.7185 0.6214

0 0 0 0 1.0000 1.1432 -0.5361

0 0 0 0 0 1.0000 0.9059

You might also like