CSCI 2033 P4.pdf - Matlab script gauss.m a few explanations...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Matlab script gauss.m: a few explanations function [x] = gauss (A, b) % function [x] = gauss (A, b) % solves A x = b by Gaussian elimination n = size(A,1) ; A = [A,b]; for k=1:n-1 for i=k+1:n piv = A(i,k) / A(k,k) ; A(i,k+1:n+1)=A(i,k+1:n+1)-piv*A(k,k+1:n+1); end end x = backsolv(A,A(:,n+1)); Function function [x] = gauss (A, b) % function [x] = gauss (A, b) % solves A x = b by Gaussian elimination ... 4-1 Text: 1.1 – MLgauss 4-1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The file containing the above script should be called gauss.m . The syntax for function is simple: function [Output-args] = func-name(Input-args) % lines of comments Takes input arguments. Computes some values and returns them in the output arguments. The gauss.m script has 2 input arguments ( A and b ) and one output argument ( x ) % indicates a commented line. First few lines of comments after function header are echoed when you type >> help func-name For example >> help gauss 4-2 Text: 1.1 – MLgauss 4-2
Image of page 2
n = size(A,1) ; % <-- n=Number of rows in matrix A = [A,b]; % <-- Adds b as last column of A % Now A contains augmented system. % It has size n x (n+1) for k=1:n-1 % Main loop in GE -- for each k for i=k+1:n % sweep rows i=k+1 to i=n ...commands % these commands will each combine end % row i with a mulitple of row k end Example: Step k = 3 ( n = 6 ) for i=4:6 piv=a(i,3)/a(3,3); row_i=row_i-piv*row_3; end * * * * * * * 0 * * * * * * 0 0 * * * * * 0 0 * * * * * 0 0 * * * * * 0 0 * * * * * 4-3 Text: 1.1 – MLgauss 4-3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
piv = A(i,k) / A(k,k) ; A(i,k+1:n+1)=A(i,k+1:n+1)-piv*A(k,k+1:n+1); The above: 1) computes the multiplier (pivot) to use in the elimination; 2) combines rows. Result = a zero in position ( i, k ) . When combining row i with row k no need to deal with zeros in columns 1 to k - 1 . Result will be zero. Also we know A ( i, k ) will be zero – can be skipped. Result: need to combine rows from positions k + 1 to n + 1 . x = backsolv(A,A(:,n+1)); The above invokes the back-solve script to solve the final system 4-4 Text: 1.1 – MLgauss 4-4
Image of page 4
THE ECHELON FORM [1.2] 4-5
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The standard echelon form A rectangular matrix is in echelon form (or row echelon form) if it has the following three properties: 1. All nonzero rows are above any rows of all zeros. 2. Each leading entry of a row is in a column to the right of the leading entry of the row above it. 3. All entries in a column below a leading entry are zeros. Each is a nonzero (leading) entry. A * can be a non-zero or a zero entry. * * * * * * * * * * 0 0 * * * * * * * * 0 0 0 0 0 0 * * * * 0 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4-6 Text: 1.2 – Echln 4-6
Image of page 6
- Which of these matrices is in Row Echelon Form? [ * = nonzero] * * * * * * * * * * * 0 0 * * * * * * * * * 0 0 * * * * * * * * * 0 0 0 0 0 0 0 0 0 * *
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern