Implement procedure Naive_Gauss (on page 77 in text) in Mathematica. Include an
ID: 3670002 • Letter: I
Question
Implement procedure Naive_Gauss (on page 77 in text) in Mathematica. Include an error message if there is an attempt to divide by 0. Test it with the following matrix from class (to test your error message, just replace a11 with a 0). 6 2 2 4 16 (Alb)12 -8 6 10 26 3 -13 9 3 19 -6 4 1 -18 -34 Hints. Here is an example of one way to create a two-dimensional list of values (a 4x4 matrix) in Mathematica.... And a 4x1 vector A-((1,2,3,4)15,3,2,-2),14,4,7,-9),14,2,1,9)) To display A nicely as in a homework assignment, you may use the following statement: A//MatrixForm Or MatrixForm(A) Make sure to display the original coefficient matrix A, and the row-reduced coefficient matrix, as well as the solution vector X. In Mathematica, you may reference, for example, as1 and ba, as: BI13]]Explanation / Answer
The sample you attached on Naive Gaussian elimination for Forward elimination and Back substitution looks fine.
function x = naiv_gauss(A,b);
n = length(b); x = zeros(n,1);
Now as per matrix we proceed:
function [x] = gausselimm(A,B)
Now A is nXn matrix and B is a nxm matrix. Solution is nxn matrix X.
[nA,mA] = size (A)
[nB,mB]=size (B)
if nA<>mA then
error('gausselim - Matrix A must be square');
abort;
elseif mA<>nB then
error('gausselim- imcompatible dimensions b/w A and b');
abort;
end;
a=[A,B]; //Matrix
n=nA; // No of rows and columns in A, rows in B
m=mB; // No of columns in B
// Forward elimination with pivoting in partial
for k=1:n-1
kpivot =k; amax = abs(a(k,k));
for i=k+1:n
if abs(a(i,k))>amax then
kpivot = i; amax= a(k,i);
end;
end;
temp = a(kpivot,:); a(kpivot,:) = a(k,:); a(k,:)=temp;
// Forward Elimination
for i=k+1:n
for j=k+1:n+m
a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end;
end;
end;
// Backward Substitution
for j=1:m
x(n,j)=a(n,n+j)/a(n,n);
for i=n-1:-1:1
sumk=0
for k=i+1:n
sumk=sumk+a(i,k)*x(k,j);
end;
x(i,j)=(a(i,n+j)-sumk)/a(i,i);
end;
end;
//End Function