Sei sulla pagina 1di 18

2. (1 point) Page 215. Problem 8.3. The gauss_elim.

m file is in the Sept21


CourseMeetings Folder

function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end

A= [0,2,1;1,1,2;2,1,1]

B=[4;6;7]

>> gauss_elim(A,B)

ans =

2.2000

1.4000

1.2000

>> inv(A)*B

ans =

2.2000

1.4000

1.2000

>> A\B

ans =

2.2000

1.4000

1.2000
3. (1 points) Write one paragraph explaining why partial pivoting in used in lines 8-
18 gauss_elim.m
Gaussian elimination is a systematic application of elementary row operations to a system of linear
equations in order to convert the system to upper triangular form. So now, partial pivoting is when use
elementary row operations on the argument matrix [Alb] to transform A into upper triangular form if a
zero is located on the diagonal, switch the rows until a nonzero is in that place. In partial pivoting at kth
elimination step, one determines the entry of greatest magnitude among the bottom n-k+1 entry in the
kth matrix columns the entry is brought to the pivot position by swapping rows as necessary.

4. (1 point) Page 215. Problem 8.5

function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end
>> A=[1,2,-1;0,-1,-1;1,1,2]

A=

1 2 -1

0 -1 -1

1 1 2

>> B=[-8;-1;-1]

B=

-8

-1

-1
>> gauss_elim(A,B)

ans =

-4

-1

>> inv(A)*B

ans =

-4

-1

>> A\B

ans =

-4

-1

5. (1 point) Page 215. Problem 8.7

function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end

>> A=[1,2,3;0,-1,-1;1,1,2]

A=

1 2 3

0 -1 -1

1 1 2

>> B=[-8;-1;-1]

B=
-8

-1

-1

>> gauss_elim(A,B)

Error using gauss_elim (line 12)

Singular matrix and No unique solution

>> inv(A)*B

Warning: Matrix is singular to working precision.

ans =

-Inf

-Inf

-Inf

>> A\B

Warning: Matrix is singular to working precision.

ans =

NaN

-Inf

Inf

6. (1 point) Page 215. Problem 8.9

function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end

>> A=[2,0,0;1,-3,0;0,2,-1]

A=

2 0 0

1 -3 0

0 2 -1

>> B=[100;0;0]
B=

100

>> gauss_elim(A,B)

ans =

50.0000

16.6667

33.3333

>> inv(A)*B

ans =

50.0000

16.6667

33.3333

>> A\B

ans =

50.0000

16.6667

33.3333

7. (1 point) Page 215. Problem 8.10

function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end
>> A=[1, 0, 0;0.5, -2.5, 0;0, 1/3, -10/3]

A=

1.0000 0 0

0.5000 -2.5000 0

0 0.3333 -3.3333

>> B=[50;0;0]

B=

50

>> gauss_elim(A,B)

ans =

50.0000

10.0000

1.0000

>> inv(A)*B

ans =

50

10

>> A\B

ans =

50.0000

10.0000

1.0000
8. (2 points) For the following process flow diagram, write the mass balances (overall and
component) for the entire system to generate three equations and three unknowns. Solve the
resulting set of equations using Matlab (gauss_elim.m , the inverse or the backslash operation).
Submit your written work as well as the Matlab input and output.
function x = gauss_elim(A,B)
%The sizes of matrices A,B are supposed to be NA x
NA and NA x NB.
%This function solves Ax = B by Gauss elimination
algorithm.
NA = size(A,2); [NB1,NB] = size(B);
if NB1 ~= NA, error('A and B must have compatible
dimensions'); end
N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; %
Augmented matrix
epss = eps*ones(NA,1);
for k = 1:NA
%Scaled Partial Pivoting at AB(k,k)
[akx,kx] = max(abs(AB(k:NA,k))./ ...
max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k +
1)]'))');
if akx < eps, error('Singular matrix and No unique
solution'); end
mx = k + kx - 1;
if kx > 1 % Row change if necessary
tmp_row = AB(k,k:N);
AB(k,k:N) = AB(mx,k:N);
AB(mx,k:N) = tmp_row;
end
% Gauss forward elimination
AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
AB(k,k) = 1; %make each diagonal element one
for m = k + 1: NA
AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N);
AB(m,k) = 0;
end
end
%backward substitution for a upper-triangular
matrix eqation
% having all the diagonal elements equal to one
x(NA,:) = AB(NA,NA+1:N);
for m = NA-1: -1:1
x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m +
1:NA,:);
end

>> A=[.04,.54,.26;.93,.24,0;.03,.22,.74]

A=

0.0400 0.5400 0.2600

0.9300 0.2400 0

0.0300 0.2200 0.7400

>>B=[2;6;2]

B=

>> gauss_elim(A,B)

ans =

5.8238

2.4330

1.7433

>> inv(A)*B

ans =
5.8238

2.4330

1.7433

>> A\B

ans =

5.8238

2.4330

1.7433

9. (1 point) Using the matrix rank, determine how many of the following chemical reactions are
linearly independent:
C + O2 --> CO2
N2 + 3H2 --> 2NH3
3CH4 + 2N2 + 3O2 --> 4NH3 + 3CO2
C + 2H2 --> CH4
N2 + O2 --> 2NO
2NO + 2H2 --> N2 + 2H2O
CO2 + H2 --> H2O + CO
2NO + 2CO --> CO2 + N2
CO2 + H2 --> CO + H2O

>> A = [-1 -1 1 0 0 0 0 0 0 0; 0 0 0 -1 -3 2 0 0 0 0; 0 -3 3 -2 0 4 -3 0 0 0; -1 0 0 0 -2 0 1 0 0 0;

0 -1 0 0 0 0 0 2 0 0; 0 0 0 1 -2 0 0 -2 2 0; 0 0 -1 0 -1 0 0 0 1 1; 0 0 1 1 0 0 0 -2 0 -2; 0 0 -1 0 -1 0 0 0 1 1];

>> rank(A)

ans =

Potrebbero piacerti anche