Sei sulla pagina 1di 9

Ejercicios de Numerical Methods for Chemical Engineers with MATLAB

Applications
Ejercicio 2.1
clc
clear
%input data
Ts=input(' Temperature of steam (deg C) = ');
Ta=input(' Temperature of air (deg C) = ');
D1=1e-3*input(' Pipe ID (mm) = ');
D2=1e-3*input(' Pipe OD (mm) = ');
Ith=1e-3*input(' Insulation thickness (mm) = ');
D3=(D2+2*Ith);
hi=input(' Inside heat transfer coefficient(W/m2.K) = ');
ho=input(' Outside heat transfer coefficient(W/m2.K) = ');
Ks=input(' Heat conductivity of steel (W/m.K) = ');
Ki=input(' Heat conductivity of insulation (W/m.K) = ');
%Matrix of coefficients
A=[2*Ks/log(D2/D1)+hi*D1 , -2*Ks/log(D2/D1) ,0
Ks/log(D2/D1) , -(Ks/log(D2/D1)+Ki/log(D3/D2)) , Ki/log(D3/D2)
0 , 2*Ki/log(D3/D2) , -(2*Ki/log(D3/D2)+ho*D3)];
%matrix of constants
C= [hi*D1*Ts ; 0 ; -ho*D3*Ta];
%solving the set of equations by Gauss elimination method
T= Gauss(A , C);
%show the results
disp(' '),disp(' Results : ')
fprintf(' T1= %4.2f\n T2=%4.2f\n T3=%4.2f\n',T)

function x=Gauss(A,c)
%GAUSS solves a set of linear algebraic equations by the Gauss
elimination
%method.
%GAUSS(A,C) finds unknowns of a set of linear algebraic equations.A is
the
%matrix of coefficients and C is the vector of constants.
%See also JORDAN,JACOBI
c=(c(:).')'; %make sure it's a column vector
n=length(c);
[nr nc]=size(A);
%check coefficient matrix and vector of constants
if nr~=nc
error('coefficient matrix is not square,')
end
if nr~=n
error('coefficient matrix and vector of constants do not have the
same length')
end
%check if the coefficient matrix is singular
if det(A)==0
fprintf('\nRank=%7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end

unit=eye(n);
%unit matrix
order=(1:n);
%order of unknowns
aug=[A c];
%augmented matrix
%Gauss elimination
for k=1:n-1
pivot=abs(aug(k,k));
prow=k;
pcol=k;
%locating the maximun pivot element
for row=k:n
for col=k:n
if abs(aug(row,col))>pivot
pivot=abs(aug(row,col));
prow=row;
pcol=col;
end
end
end
%interchanging the rows
pr=unit;
tmp=pr(k , :);
pr(k,:)=pr(prow,:);
pr(prow , : ) = tmp;
aug = pr*aug;
%interchanging the columns
pc=unit;
tmp=pc(k , : );
pc(k , : )=pc(pcol , : );
pc(pcol , : )=tmp;
aug(1 : n , 1 : n)= aug(1 : n , 1 : n)*pc;
order=order*pc; %keep track of the column interchanges
%reducing the elements below diagonal to zero in the column k
lk=unit;
for m=k+1:n
lk(m,k)=-aug(m,k)/aug(k,k);
end
aug=lk*aug;
end
x=zeros(n,1);
%back substitution
t(n)=aug(n,n+1)/aug(n,n);
x(order(n))=t(n);
for k=n-1:-1:1
t(k)=(aug(k,n+1)-sum(aug(k,k+1:n).*t(k+1:n)))/aug(k,k);
x(order(k))=t(k);
end

Resultados
Temperature of steam (deg C) = 130
Temperature of air (deg C) = 25

Pipe ID (mm) = 20
Pipe OD (mm) = 25
Insulation thickness (mm) = 40
Inside heat transfer coefficient(W/m2.K) = 1700
Outside heat transfer coefficient(W/m2.K) = 3
Heat conductivity of steel (W/m.K) = 45
Heat conductivity of insulation (W/m.K) = 0.064
Results :
T1= 129.79
T2=129.77
T3=48.12

Ejercicio 2.2
%example2_2
%solution to example 2.2.this program solves the material
%and energy balances equations of a steam distribution
%system using the function JORDAN.M.
clc
clear
%matrix of coefficients
A=[1,1,1,0*(4:14)
1.17,0,0,-1,0*(5:14)
0*(1:4),1,0*(6:14)
0,0,1,0,1,-1,-1,-1,0*(9:12),1 , 0
0*(1:5),1,1,1,1,-1,-1,0*(12:14)
0*(1:3),1,0*(5:12),-1,0
1,0*(2:3),-1,0*(5:9),1,0*(11:13),1
0*(1:9),4.594,0*(11:13),0.11
0*(1:8),1,0*(10:14)
0,1,0*(3:14)
0*(1:5),1,0*(7:13),-0.0147
0,0,1,0*(4:11),-0.07,0,0
0*(1:6),1,0*(8:14)
0*(1:9),1,0,-1,0,1];
%vector of constants
c=[43.93,0,95.798,99.1,-8.4,24.2,189.14,146.55,10.56,2.9056,0,0,14.6188,97.9];
%solution
X=Jordan(A,c)
function x=Jordan(A,c)
c=(c(:).')';
n=length(c);
[nr nc]=size(A);
if nr~=nc
error('coefficient matrix is not square,')
end
if nr~=n
error('coefficient matrix and vector of constants do not have the
same length')
end
%check if the coefficient matrix is singular

if det(A)==0
fprintf('\nRank=%7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end
unit=eye(n);
%unit matrix
order=(1:n);
%order of unknowns
aug=[A c];
%augmented matrix
%Gauss_jordan algorithm
for k=1:n
pivot=abs(aug(k,k));
prow=k;
pcol=k;
%locating the maximun pivot element
for row=k:n
for col=k:n
if abs(aug(row,col))>pivot
pivot=abs(aug(row,col));
prow=row;
pcol=col;
end
end
end
%interchanging the rows
pr=unit;
tmp=pr(k , :);
pr(k,:)=pr(prow,:);
pr(prow , : ) = tmp;
aug = pr*aug;
%interchanging the columns
pc=unit;
tmp=pc(k , : );
pc(k , : )=pc(pcol , : );
pc(pcol , : )=tmp;
aug(1 : n , 1 : n)= aug(1 : n , 1 : n)*pc;
order=order*pc; %keep track of the column interchanges
%reducing the elements below diagonal to zero in the column k
lk=unit;
for m=1:n
if m==k
lk(m,k)=1/aug(k,k);
else
lk(m,k)=-aug(m,k)/aug(k,k);
end
end
aug=lk*aug;
end
x=zeros(n,1);
%solution
for k=1:n
x(order(k))=aug(k,n+1);
end

Resultados
X =

20.6854
2.9056
20.3390
24.2020
95.7980
2.4211
14.6188
-0.0010
10.5600
27.9567
8.0422
290.5565
0.0020
164.6998

Ejercicio 2.3
%example2_3
clc
clear
%input data
fprintf('solution of set of linear algebraic equations by the Jacobi
method\n\n')
n=input('Number of equations = ');
for k=1:n
fprintf('\n Coefficients of eq. %2d= ',k)
A(k,1:n)=input(' ');
fprintf(' Constant of eq. %2d= ',k)
c(k)=input(' ');
end
disp(' ')
tol=input('Convergence criterion= ');
trace=input(' Show step-bye-step path to results(0/1)? ');
redo=1;
while redo
disp(' ')
guess=input('Vector of initial guess= ');
%solution
ca=Jacobi(A,c,guess,tol,trace);
fprintf('\n\n Results:\n')
for k=1:n
fprintf(' CA(%2d)=%6.4g\n',k,ca(k))
end
disp(' ')
redo=input('Repeat the calculations with another guess (0/1)? ');
disp(' ')
end
function x=Jacobi(A,c,x0,tol,trace)
%initialization
if nargin<4 || isempty(tol)
tol=1e-6;
end
if nargin>=4 && tol==0

tol=1e-6;
end
if nargin<5 || isempty(trace)
trace=0;
end
if trace
fprintf('\n Initial guess:\n')
fprintf('%8.6g ',x0)
end
c=(c(:).')';
%Make sure it's a column vector
x0=(x0(:).')'; %Make sure it's a column vector
n=length(c);
[nr nc]=size(A);
%check coefficient matrix,vector of constants and
%vector of unknowns
if nr~=nc
error('Coefficient matrix is not square.')
end
if nr~=n
error('Coefficient matrix and vector of constants do not have the
same length.')
end
if length(x0)~=n
error('Vector of unknowns and vector of constants do not have the
same length.')
end
%check if the coefficient matrix is singular
if det(A)==0
fprintf('\n Rank=%7.3g\n',rank(A))
error('The coefficient matrix is singular.')
end
%Building modified coefficient matrix and modified
%vector of coefficients
D=diag(diag(A)); %The diagonal matrix
a0=inv(D)*A-eye(n); %Modified matrix of coefficients
c0=inv(D)*c;
%Modified vector of constans
x=x0;
x0=x+2*tol;
iter=0;
%substitution procedure
while max(abs(x-x0))>=tol
x0=x;
x=c0-a0*x0;
if trace
iter=iter+1;
fprintf('\n Iteration no. %3d\n',iter)
fprintf('%8.6g ',x)
end
end

Resultados
solution of set of linear algebraic equations by the Jacobi method

Number of equations = 4
Coefficients of eq.
Constant of eq. 1=

1= [1100,0,0,0]
1000

Coefficients of eq.
Constant of eq. 2=

2=
0

[1000,-1400,100,0]

Coefficients of eq.
Constant of eq. 3=

3=
0

[0,1100,-1240,100]

Coefficients of eq.
Constant of eq. 4=

4=
0

[0,0,1100,-1250]

Convergence criterion= 1e-5


Show step-bye-step path to results(0/1)? 1
Vector of initial guess= 0.06*ones(1,4)
Initial guess:
0.06
0.06
0.06
0.06
Iteration no.
1
0.909091 0.0471429 0.0580645
0.0528
Iteration no.
2
0.909091 0.653498 0.0460783 0.0510968
Iteration no.
3
0.909091 0.652642 0.583837 0.0405489
Iteration no.
4
0.909091 0.691053 0.582227 0.513776
Iteration no.
5
0.909091 0.690938 0.654465 0.512359
Iteration no.
6
0.909091 0.696098 0.654248 0.575929
Iteration no.
7
0.909091 0.696083 0.663952 0.575739
Iteration no.
8
0.909091 0.696776 0.663923 0.584278
Iteration no.
9
0.909091 0.696774 0.665227 0.584252
Iteration no. 10
0.909091 0.696867 0.665223
0.5854
Iteration no. 11
0.909091 0.696867 0.665398 0.585396
Iteration no. 12
0.909091 0.696879 0.665397 0.58555
Iteration no. 13
0.909091 0.696879 0.665421 0.58555
Iteration no. 14
0.909091 0.696881 0.665421 0.58557
Iteration no. 15

0.909091 0.696881 0.665424

0.58557

Results:
CA( 1)=0.9091
CA( 2)=0.6969
CA( 3)=0.6654
CA( 4)=0.5856
Repeat the calculations with another guess (0/1)? 1

Vector of initial guess= 100*ones(1,4)


Initial guess:
100
100
100
Iteration no.
1
0.909091 78.5714 96.7742
Iteration no.
2
0.909091 7.56179 76.7972
Iteration no.
3
0.909091 6.13487 13.5759
Iteration no.
4
0.909091 1.61906 10.8923
Iteration no.
5
0.909091 1.42738 2.39971
Iteration no.
6
0.909091 0.820759 2.03923
Iteration no.
7
0.909091 0.79501 0.898394
Iteration no.
8
0.909091 0.713522 0.84997
Iteration no.
9
0.909091 0.710063 0.69672
Iteration no. 10
0.909091 0.699116 0.690215
Iteration no. 11
0.909091 0.698652 0.669628
Iteration no. 12
0.909091 0.697181 0.668755
Iteration no. 13
0.909091 0.697119 0.665989
Iteration no. 14
0.909091 0.696921 0.665872
Iteration no. 15
0.909091 0.696913
0.6655
Iteration no. 16
0.909091 0.696886 0.665485
Iteration no. 17
0.909091 0.696885 0.665435
Iteration no. 18

100
88
85.1613
67.5816
11.9468
9.58527
2.11175
1.79452
0.790587
0.747973
0.613113
0.607389
0.589273
0.588504
0.586071
0.585967
0.58564
0.585626

0.909091 0.696882 0.665433 0.585583


Iteration no. 19
0.909091 0.696882 0.665426 0.585581
Results:
CA( 1)=0.9091
CA( 2)=0.6969
CA( 3)=0.6654
CA( 4)=0.5856
Repeat the calculations with another guess (0/1)? 0

Potrebbero piacerti anche