tarea 9 metodos
TRANSCRIPT
-
8/4/2019 tarea 9 metodos
1/9
Ejercicios de Numerical Methods for Chemical Engineers with MATLABApplications
Ejercicio 2.1clcclear%input dataTs=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 coefficientsA=[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 constantsC= [hi*D1*Ts ; 0 ; -ho*D3*Ta];%solving the set of equations by Gauss elimination methodT= Gauss(A , C);%show the resultsdisp(' '),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 Gausselimination%method.%GAUSS(A,C) finds unknowns of a set of linear algebraic equations.A isthe%matrix of coefficients and C is the vector of constants.%See also JORDAN,JACOBIc=(c(:).')'; %make sure it's a column vectorn=length(c);[nr nc]=size(A);%check coefficient matrix and vector of constantsif nr~=nc
error('coefficient matrix is not square,')
endif nr~=nerror('coefficient matrix and vector of constants do not have the
same length')end%check if the coefficient matrix is singularif det(A)==0
fprintf('\nRank=%7.3g\n',rank(A))error('The coefficient matrix is singular.')
end
-
8/4/2019 tarea 9 metodos
2/9
unit=eye(n); %unit matrixorder=(1:n); %order of unknownsaug=[A c]; %augmented matrix%Gauss eliminationfor k=1:n-1
pivot=abs(aug(k,k));prow=k;pcol=k;%locating the maximun pivot elementfor row=k:n
for col=k:nif abs(aug(row,col))>pivot
pivot=abs(aug(row,col));prow=row;pcol=col;
endend
end%interchanging the rowspr=unit;
tmp=pr(k , :);pr(k,:)=pr(prow,:);pr(prow , : ) = tmp;aug = pr*aug;%interchanging the columnspc=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 klk=unit;for m=k+1:n
lk(m,k)=-aug(m,k)/aug(k,k);endaug=lk*aug;
endx=zeros(n,1);%back substitutiont(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
ResultadosTemperature of steam (deg C) = 130
Temperature of air (deg C) = 25
-
8/4/2019 tarea 9 metodos
3/9
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.clcclear%matrix of coefficientsA=[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 , 00*(1:5),1,1,1,1,-1,-1,0*(12:14)0*(1:3),1,0*(5:12),-1,01,0*(2:3),-1,0*(5:9),1,0*(11:13),10*(1:9),4.594,0*(11:13),0.110*(1:8),1,0*(10:14)
0,1,0*(3:14)0*(1:5),1,0*(7:13),-0.01470,0,1,0*(4:11),-0.07,0,00*(1:6),1,0*(8:14)0*(1:9),1,0,-1,0,1];
%vector of constantsc=[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];%solutionX=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,')endif nr~=n
error('coefficient matrix and vector of constants do not have thesame length')end%check if the coefficient matrix is singular
-
8/4/2019 tarea 9 metodos
4/9
if det(A)==0fprintf('\nRank=%7.3g\n',rank(A))error('The coefficient matrix is singular.')
endunit=eye(n); %unit matrixorder=(1:n); %order of unknownsaug=[A c]; %augmented matrix%Gauss_jordan algorithmfor k=1:n
pivot=abs(aug(k,k));prow=k;pcol=k;%locating the maximun pivot elementfor row=k:n
for col=k:nif abs(aug(row,col))>pivot
pivot=abs(aug(row,col));prow=row;pcol=col;
end
endend%interchanging the rowspr=unit;tmp=pr(k , :);pr(k,:)=pr(prow,:);pr(prow , : ) = tmp;aug = pr*aug;%interchanging the columnspc=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 klk=unit;for m=1:n
if m==klk(m,k)=1/aug(k,k);
elselk(m,k)=-aug(m,k)/aug(k,k);
endendaug=lk*aug;
endx=zeros(n,1);
%solutionfor k=1:nx(order(k))=aug(k,n+1);
end
ResultadosX =
-
8/4/2019 tarea 9 metodos
5/9
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_3clcclear%input datafprintf('solution of set of linear algebraic equations by the Jacobimethod\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(' ');
enddisp(' ')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= ');%solutionca=Jacobi(A,c,guess,tol,trace);fprintf('\n\n Results:\n')for k=1:n
fprintf(' CA(%2d)=%6.4g\n',k,ca(k))enddisp(' ')
redo=input('Repeat the calculations with another guess (0/1)? ');disp(' ')end
function x=Jacobi(A,c,x0,tol,trace)%initializationif nargin=4 && tol==0
-
8/4/2019 tarea 9 metodos
6/9
tol=1e-6;endif nargin=tol
x0=x;x=c0-a0*x0;if trace
iter=iter+1;fprintf('\n Iteration no. %3d\n',iter)fprintf('%8.6g ',x)
endend
Resultadossolution of set of linear algebraic equations by the Jacobi method
-
8/4/2019 tarea 9 metodos
7/9
Number of equations = 4
Coefficients of eq. 1= [1100,0,0,0]
Constant of eq. 1= 1000
Coefficients of eq. 2= [1000,-1400,100,0]
Constant of eq. 2= 0
Coefficients of eq. 3= [0,1100,-1240,100]
Constant of eq. 3= 0
Coefficients of eq. 4= [0,0,1100,-1250]
Constant of eq. 4= 0
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. 50.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. 110.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
-
8/4/2019 tarea 9 metodos
8/9
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 100
Iteration no. 1
0.909091 78.5714 96.7742 88
Iteration no. 2
0.909091 7.56179 76.7972 85.1613
Iteration no. 3
0.909091 6.13487 13.5759 67.5816
Iteration no. 4
0.909091 1.61906 10.8923 11.9468
Iteration no. 5
0.909091 1.42738 2.39971 9.58527
Iteration no. 6
0.909091 0.820759 2.03923 2.11175
Iteration no. 7
0.909091 0.79501 0.898394 1.79452
Iteration no. 80.909091 0.713522 0.84997 0.790587
Iteration no. 9
0.909091 0.710063 0.69672 0.747973
Iteration no. 10
0.909091 0.699116 0.690215 0.613113
Iteration no. 11
0.909091 0.698652 0.669628 0.607389
Iteration no. 12
0.909091 0.697181 0.668755 0.589273
Iteration no. 13
0.909091 0.697119 0.665989 0.588504
Iteration no. 140.909091 0.696921 0.665872 0.586071
Iteration no. 15
0.909091 0.696913 0.6655 0.585967
Iteration no. 16
0.909091 0.696886 0.665485 0.58564
Iteration no. 17
0.909091 0.696885 0.665435 0.585626
Iteration no. 18
-
8/4/2019 tarea 9 metodos
9/9
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.6969CA( 3)=0.6654
CA( 4)=0.5856
Repeat the calculations with another guess (0/1)? 0