{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# HW2 - Part1 functionx=mygehw2_soln(a,b%...

This preview shows pages 1–3. Sign up to view the full content.

Part 1 function x = mygehw2_soln(a,b) % Made by the ChBE 2120 Fall 2010 class n = length(b); % Forward elimination step of Gauss elimination % Eliminate each variable from x_1 to x_n 1 for k = 1:n 1 if (abs(a(k,k)) < 1e 12) error('Divide by zero error in forward elimination.') end % Eliminate our variable in each row below our current variable number for i = k+1:n % Find our pivot factor = a(i,k)/a(k,k); % For each column in the row being eliminated, subtract the initial % value times the pivot for j = k+1:n a(i,j) = a(i,j) factor*a(k,j); end % Don't forget to do that for the b vector as well b(i) = b(i) factor*b(k);

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

View Full Document
end end % Backward substitution step if (abs(a(n,n)) < 1e 12) error('Divide by zero error in back substitution.') end % Do first back substitution to get x_n x(n) = b(n)/a(n,n); % Go back up each row for i = n 1: 1:1 sum = b(i); % Use all previously found x's to subtract constants from the b value for j = i+1:n sum = sum a(i,j)*x(j); end if (abs(a(i,i)) < 1e 12) error('Divide by zero error in back substitution.') end % Divide the remaining b by the main diagonal coefficient x(i) = sum/a(i,i); end
This is the end of the preview. Sign up to access the rest of the document.
• Spring '07
• Gallivan
• Harshad number, Forward Elimination step, Backward Substitution step, current variable number, Gaussian Elimination  x=mygehw2_soln, inverse function  x=inv

{[ snackBarMessage ]}