Part 1: Gaussian elimination Gaussian Elimination is an orderly process of trans
ID: 3568401 • Letter: P
Question
Part 1: Gaussian elimination
Gaussian Elimination is an orderly process of transforming an augmented matrix into an equivalent
upper triangular form. Write a Matlab function mygauss which accepts a square matrix A (dimensions
nxn) and a column vector b (dimensions nx1) and implements the Gaussian elimination algorithm:
form G = [A b]
for i = 1. . . n - 1
for k = i + 1. . . n
for j = i . . . n + 1
gkj = gkj - (gki/gii)gij
end
end
end
G is the matrix of the augmented system [A b]. The function should return the upper triangular matrix U
and the new column vector c with the constant terms of the resulting system of linear equations in
triangular form.
For example, if you call mygauss with input arguments:
A = -3 2 -1 b = -1
6 -6 7 -7
3 -4 4 -6
the function should return:
U = -3 2 -1 c = -1
0 -2 5 -9
0 0 -2 2
Part 2: Solving triangular system of linear equations
Write a function mytriang which solves a system of linear equations Ux = c , where matrix U is in upper
triangular form, vector x is a column vector of unknowns, and c is a column vector of constant terms. The
function should implement backward substitution algorithm:
EE 221 - Numerical Computing for Engineersgiven U, b
xn = bn/unn
for i = n - 1 . . . 1
s = bi
for j = i + 1. . . n
s = s-ui,j xj
end
xi = s/ui,i
end
The function should return the vector x with the values of the unknowns. For example, if you call
mytriang with input arguments:
U = -3 2 -1 c = -1
0 -2 5 -9
0 0 -2 2
the function should return:
x = 2
2
-1
Part 3: Solving a system of linear equations
Write a function mylinear which solves a system of linear equations Ax = b . The function mylinear
should use the functions mygauss and mytriang which you implemented in Part 1 and Part 2,
respectively.
Function mylinear has two input arguments, a square matrix A and a column vector b. The function
returns the vector x with the values of the unknowns.
Your function has to check the input arguments and provide the user with the error report if the function
is not called according to the following rules:
- the function is called with two arguments,
- the input argument A is a square matrix,
- the input argument b is a column vector,
- the dimensions of A and b agree.
Explanation / Answer
part1
function [ U,c ] = myguass( A,b)
A=[-3 2 -1;6 -6 7;3 -4 4];
b=[-1,-7, -6];
n=length(b);
for i = 1:n
A(i,n+1)= b(i);
end
% calculating upper triangual matrix;
for i=1:n-1
pivot = A(i,i);
for k =i+1:n
w=A(k,i);
for j=i:n+1
A(k,j)=A(k,j)-(w/pivot)*A(i,j);
end
end
end
%extracting upper triangular matrix U;
for i=1:n
for j=1:n
U(i,j)=A(i,j);
end
end
disp(U);
% extracting the new vactor bnew;
for i=1:n
c(i)=A(i,n+1);
end
c=c';
disp(c);
end
part2
function [x] = mytriang(U, c)
U = [-3 2 -1;0 -2 5;0 0 -2];
c = [-1;-9;2];
x = c;
n = length(c);
x(n) = c(n)/U(n,n);
for i=n-1:-1:1
s = c(i);
for j=i+1:n
s = s - U(i,j)*x(j);
end
x(i) = s/U(i,i);
end
disp (x)
end
part3 i dont know
plz rate