1

Ex#1:

  1. As shown on the white board in the last two lectures, use Gaussian elimination with backward substitution and two-digit rounding arithmetic to solve the following three linear systems, A, B and C. (Do not reorder the equations). (Hand in your written work by showing all the steps).

4x1 + x2 +2x3 = 9

2x1 + 4x2 - x3= -5 (A)

x1 + x2 - 3x3 = -9

-x1 + 4x2 + x3 =8

5/3x1 + 2/3x2 + 2/3x3=1 (B)

2x1 + x2 + 4x3=11

x1 - x2 + 3x3 = 2

3x1 - 3x2 + x3= -1 (C)

x1 + x2= 3

Only run the Matlab program on (D) and explain the results you got.

x1+ x2 +x4= 2

2x1 + x2 – x3 + x4=1 (D)

4x1 – x2 – 2x3 + 2x4=0

3x1 –x2 –x3+ 2x4=-3

  1. The following Matlab program finds the roots of a system of equation based on the naïve (simple/regular) Gaussian elimination. Run it and comment on the conformity of your solutions found in question a. You may need to add some lines to the code to show the output as follows:

function x = GaussNaive(A,b)

% GaussNaive: naive Gauss elimination

% x = GaussNaive(A,b): Gauss elimination without pivoting.

% input:

% A = coefficient matrix

% b = right hand side vector

% output:

% x = solution vector

[m,n] = size(A);

if m~=n, error('Matrix A must be square'); end

nb = n+1;

Aug = [A b]; % Augmented matrix

% forward elimination

for k = 1:n-1

for i = k+1:n

factor = Aug(i,k)/Aug(k,k);

Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);

end

end

% back substitution

x = zeros(n,1);

x(n) = Aug(n,nb)/Aug(n,n);

for i = n-1:-1:1

x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);

end

A sample of output

The Matrix A is:

4 -1 1

2 5 2

1 2 4

The vector b is:

8

3

11

The Augmented matrix is:

4 -1 1 8

2 5 2 3

1 2 4 11

The roots are:

1 -1 3

Ex#2:

  1. Use LU factorization as it has been shown through few examples in the lecture to generate both of L and U matrices (by hand). (Hand in your written work by showing all the steps).

Use the following system of equations:

A =

1 1 0 3

2 1 -1 1

3 -1 -1 2

-1 2 3 -1

  1. Use and understand the following Matlab program to compute L and U matrices. Run it and comment on the conformity of your solutions found in question a. Display both L and U.

function [L,U]=LU_factorization(A,n)

% LU factorization of an n by n matrix A. A = LU

% using Gauss elimination NO PIVOTING

L=eye(n);

for k=1:n

if (A(k,k) == 0) Error(' Dividing by zero!'); end

L(k+1:n,k)=A(k+1:n,k)/A(k,k);

for j=k+1:n

A(j,:)=A(j,:)-L(j,k)*A(k,:);

end

end

U = triu(A); % Since U is stored in the original matrix A, we need to triangular it.

end

A sample of input /output of your Matlab is:

Test your program with 2 different inputs: Input#1 (see below) and generate a 6x6 matrix of floating point numbers between 0 and 1.

Input #1: (same example that we solved on Thursday in the lecture)

A =

1 1 0 3

2 1 -1 1

3 -1 -1 2

-1 2 3 -1

L =

1 0 0 0

2 1 0 0

3 4 1 0

-1 -3 0 1

U =

1 1 0 3

0 -1 -1 -5

0 0 3 13

0 0 0 -13

Input#2: Generate a 6X6 random matrix of floating point numbers between 0 and 1.

  1. Now, you need to add the vector [b] to this exercise such that [A][x] = [b] and solve for x. Use the same vector [b] that we used in the lecture, that is [b] = {4 1 -3 4} and perform the following tasks:
  1. Run theMatlab GaussNaive on input#1 and [b]. Register the roots.
  2. Now, using the two equations (again covered in the lecture on Thursday), that is:

[L] [d] = [b] (solving for d) (1)

[u] [x] = [d] (solving for x) (2)

Check the conformity of the roots obtained in Ex#2 question c1and compare them with those using equations (1) and (2).

Note. This is the same example we covered in class.

Ex#3:

Given the following system of equations:

x1 - x2 + Ax3 = -2

-x1 + 2x2 – Ax3 = 3

Ax1 + X2+ x3 =2

  1. For which value(s) of A, the above system of equations has no solutions?
  2. For which value(s) of A, the above system of equations has an infinite number of solutions?
  3. For which value(s) of A, the above system of equations has a unique solution?