Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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]
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
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