Write a Matlab function x myG E A b which uses the Gaussian
Write a Matlab function \"x = my_G E (A, b)\" which uses the Gaussian Elimination method described in class to solve Ax = b. The code should prompt appropriate error message if either A is not a square matrix, or the sizes of the input are not compatible for solving the above equation, or A is singular.
Solution
function [x,U] = myGE(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
p=length(A);
if size(A)~=[p,p]
disp(The Matrix is not square.);
else
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute An=Mn*An-1, bn=Mn*bn-1
for i=k+1:n;
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end;
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
end
end
Check it. As I don\'t have MATLAB in my system so I can\'t check it. But I think this is correct.
