Tutorial 5 Solution
Tutorial 5 Solution
1. For the truss shown in the figure, determine the force in members IC and CG. Using that
the members AB, BC CD DE HI and GI are zero-force members the truss is described by
the following set of equations.
where F1 = 6 kN and F2 =6 kN are the external vertical forces applied to node G and
node F, a = 1.5 m and b = 2 m. Express this set of linear algebraic equations in matrix
form and use Gauss Elimination with Partial Pivoting to solve for the unknowns Ay,
FIC, FCJ, and FCG.
1
Solution:
clear,clc
a=1.5; %m
b=2; %m
F1=6; %kN
A=[-4*a 0 0 0; -2*a -2*a*b/sqrt(a^2+b^2) 0 0; 0 -a/sqrt(a^2+b^2) 0
a/sqrt(a^2+b^2); 0 -b/sqrt(a^2+b^2) -1 -b/sqrt(a^2+b^2) ];
b=[-3*a*F1;0;0;0];
x=GaussPivot(A,b)
%Ans
%x =
%
% 4.5000kN
% -5.6250kN
% 9.0000kN
% -5.6250kN
where
function x=GaussPivot(A,b)
%implements Gauss elimination with partial pivoting
%forward elimination
[m,n]=size(A);
for k=1:m-1
[big,i]=max(abs(A(k:m,k)));
ipr=k+i-1;
if ipr~=k
A([k,ipr],:)=A([ipr,k],:);
b([k,ipr])=b([ipr,k]);
end
for i=k+1:n
fac=-A(i,k)/A(k,k);
A(i,k:m)=A(i,k:m)+fac*A(k,k:m);
b(i)=b(i)+fac*b(k);
end
end
% back substitution
x=zeros(n,1);
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
end
2
2. Consider a laminar flow through a pipeline shown below. The governing equations are
the pressure drop equations for each pipe element i – j and the mass balance equation at
each node.
1 2
4
The pressure drop between nodes i and j is given by,
32 Lij
pi p j ij U ij , where ij
d ij2
Here, Uij is the velocity in the pipe segment i-j. The mass balance in node 2 is given for
example by
d122 U 12 j d 232 U 23 d 242 U 24
Use
a) LU factorization:
3
b) matlab function linsolve
to solve this problem for pressures of p1 = 250 kPa and p3 = p4= 80 kPa.
Hint: Use format long to get more accurate values for the unknowns.
Solution:
Mass balance equation in node 2: d122 U 12 j d 232 U 23 d 242 U 24
32 L12
Pipeline 12: p1 p 2 U 12
d122
32 L23
Pipeline 23: p 2 p3 U 23
d 232
32 L24
Pipeline 24 p2 p4 U 24
d 242
a)
d=[0.1 0.09 0.09] %m
L=[1200 900 900] %m
p1=250e3 %Pa
p3=80e3 %Pa
p4=80e3 %Pa
mu=0.1 %Pa.s
a=32*mu.*L./d.^2
A=[0 d(1)^2 -d(2)^2 -d(3)^2;-1 -a(1) 0 0;1 0 -a(2) 0;1 0 0 -a(3)]
b=[0; -p1;p3;p4]
[L U]=lu(A);
d=L\b;
x=U\d
ans =
1.0e+005 *
1.418271748617981 Pa
0.000002817000655 m/s
0.000001738889293 m/s
0.000001738889293 m/s
b) linsolve(A,b)
ans =
1.0e+005 *
1.418271748617981 Pa
0.000002817000655 m/s
0.000001738889293 m/s
0.000001738889293 m/s
4
3. The following system of equations is designed to determine concentrations (the c’s in
g/m3) in a series of coupled reactors as a function of the amount of mass input to each
reactor (the right-hand sides in g/day):
15c1 − 3c2 − c3 = 3800
−3c1 + 18c2 − 6c3 = 1200
−4c1 − c2 + 12c3 = 2350
Solve this problem with the Gauss-Seidel method.
Solution:
clear, clc
A=[15 -3 -1; -3 18 -6;-4 -1 12 ]
b=[3800; 1200; 2350]
x1=GaussSeidel(A,b,[0 0 0],1e-6)
% x1 = 320.2071 227.2019 321.5024
with function
function x=GaussSeidel(A,b,guess,eps)
[m,n]=size(A);
C=A;
x=guess;
for i=1:n
C(i,1:n)=C(i,1:n)/A(i,i);
d(i)=b(i)/A(i,i);
C(i,i)=0.0;
end
while (1)
for i=1:m
xnew(i)=d(i)-C(i,:)*x';
if(xnew(i)~=0)
err(i)=abs((xnew(i)-x(i))/xnew(i));
end
end
if(max(err)<eps)
break;
end
x=xnew;
end
5
Sec 52
85-220 Numerical Analysis of Engineering Systems
Winter 2014
Tutorial 5
1. For the truss shown in the figure, determine the force in members IC and CG. Using that
the members AB, BC CD DE HI and GI are zero-force members the truss is described by
the following set of equations.
where F1 = 6 kN and F2 =6 kN are the external vertical forces applied to node G and
node F, a = 1.5 m and b = 2 m. Express this set of linear algebraic equations in matrix
form and use Gauss Elimination with Partial Pivoting to solve for the unknowns Ay,
FIC, FCJ, and FCG.
6
Solution:
clear,clc
a=1.5; %m
b=2; %m
F1=6; %kN
A=[-4*a 0 0 0; -2*a -2*a*b/sqrt(a^2+b^2) 0 0; 0 -a/sqrt(a^2+b^2) 0
a/sqrt(a^2+b^2); 0 -b/sqrt(a^2+b^2) -1 -b/sqrt(a^2+b^2) ];
b=[-3*a*F1;0;0;0];
x=GaussPivot(A,b)
%Ans
%x =
%
% 4.5000kN
% -5.6250kN
% 9.0000kN
% -5.6250kN
where
function x=GaussPivot(A,b)
%implements Gauss elimination with partial pivoting
%forward elimination
[m,n]=size(A);
for k=1:m-1
[big,i]=max(abs(A(k:m,k)));
ipr=k+i-1;
if ipr~=k
A([k,ipr],:)=A([ipr,k],:);
b([k,ipr])=b([ipr,k]);
end
for i=k+1:n
fac=-A(i,k)/A(k,k);
A(i,k:m)=A(i,k:m)+fac*A(k,k:m);
b(i)=b(i)+fac*b(k);
end
end
% back substitution
x=zeros(n,1);
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
end
7
2. Consider a laminar flow through a pipeline shown below. The governing equations are
the pressure drop equations for each pipe element i – j and the mass balance equation at
each node.
1 2
4
The pressure drop between nodes i and j is given by,
32 Lij
pi p j ij U ij , where ij
d ij2
Here, Uij is the velocity in the pipe segment i-j. The mass balance in node 2 is given for
example by
d122 U 12 j d 232 U 23 d 242 U 24
Use
a) LU factorization:
8
b) matlab function linsolve
to solve this problem for pressures of p1 = 250 kPa and p3 = p4= 80 kPa.
Hint: Use format long to get more accurate values for the unknowns.
Solution:
Mass balance equation in node 2: d122 U 12 j d 232 U 23 d 242 U 24
32 L12
Pipeline 12: p1 p 2 U 12
d122
32 L23
Pipeline 23: p 2 p3 U 23
d 232
32 L24
Pipeline 24 p2 p4 U 24
d 242
a)
d=[0.1 0.09 0.09] %m
L=[1200 900 900] %m
p1=250e3 %Pa
p3=80e3 %Pa
p4=80e3 %Pa
mu=0.1 %Pa.s
a=32*mu.*L./d.^2
A=[0 d(1)^2 -d(2)^2 -d(3)^2;-1 -a(1) 0 0;1 0 -a(2) 0;1 0 0 -a(3)]
b=[0; -p1;p3;p4]
[L U]=lu(A);
d=L\b;
x=U\d
ans =
1.0e+005 *
1.418271748617981 Pa
0.000002817000655 m/s
0.000001738889293 m/s
0.000001738889293 m/s
b) linsolve(A,b)
ans =
1.0e+005 *
1.418271748617981 Pa
0.000002817000655 m/s
0.000001738889293 m/s
0.000001738889293 m/s
9
3. The following system of equations is designed to determine concentrations (the c’s in
g/m3) in a series of coupled reactors as a function of the amount of mass input to each
reactor (the right-hand sides in g/day):
15c1 − 3c2 − c3 = 3800
−3c1 + 18c2 − 6c3 = 1200
−4c1 − c2 + 12c3 = 2350
Solve this problem with the Gauss-Seidel method.
Solution:
clear, clc
A=[15 -3 -1; -3 18 -6;-4 -1 12 ]
b=[3800; 1200; 2350]
x1=GaussSeidel(A,b,[0 0 0],1e-6)
% x1 = 320.2071 227.2019 321.5024
with function
function x=GaussSeidel(A,b,guess,eps)
[m,n]=size(A);
C=A;
x=guess;
for i=1:n
C(i,1:n)=C(i,1:n)/A(i,i);
d(i)=b(i)/A(i,i);
C(i,i)=0.0;
end
while (1)
for i=1:m
xnew(i)=d(i)-C(i,:)*x';
if(xnew(i)~=0)
err(i)=abs((xnew(i)-x(i))/xnew(i));
end
end
if(max(err)<eps)
break;
end
x=xnew;
end
10
Sec 53
85-220 Analysis of Mechanical Systems
Winter 2014
Tutorial 5
1. Five reactors linked by pipes are shown in Figure. The rate of mass flow through each
pipe is computed as the product of flow (Q) and concentration (c). At steady state, the
mass flow into and out of each reactor must be equal. For example, for the first reactor, a
mass balance can be written as
Write mass balances for the remaining reactors in the Figure and express the equations in
matrix form. Use
a) LU factorization:
b) matlab function linsolve
to solve for the concentrations in each reactor.
Solution:
Reactor 1
Reactor 2
Reactor 3
Reactor 4
11
Reactor 5
a)
B=[6 0 -1 0 0;1 -1 0 0 0;0 -1 9 0 0;0 1 8 -11 2;3 1 0 0 -4];
b=[50;0;160;0;0];
[L U]=lu(B,b);
d=L\b
x=U\d
x = 11.5094
11.5094
19.0566
16.9983
11.5094
b)linsolve(B,b)
x = 11.5094
11.5094
19.0566
16.9983
11.5094
2. For the truss shown in the figure, the sum of the forces in both horizontal and vertical
directions must be zero at each node(the system is at equilibrium). Therefore, for node 1:
12
for node 2:
for node 3:
Express this set of linear algebraic equations in matrix form and then use Gauss
Elimination with Partial Pivoting to solve for the unknowns.
Note: the angles in MATLAB must be entered in radians
Solution:
node 1
node 2:
node 3:
13
A=[-cos(30/180*pi) 0 cos(60/180*pi) 0 0 0;-sin(30/180*pi) 0 -sin(60/180*pi) 0
0 0;cos(30/180*pi) 1 0 1 0 0;sin(30/180*pi) 0 0 0 1 0;0 -1 -cos(60/180*pi) 0
0 0;0 0 sin(60/180*pi) 0 0 1];
b=[0;1000;0;0;0;0];
x=Gausspivot(A,b)
%Ans
%
% -500.00
% 433.01
% -866.03
% 0
% 250.00
% 750.00
With function
function x=Gausspivot(A,b)
[m,n]=size(A);
for k=1:m-1
[big,i]=max(abs(A(k:m,k)))
ipr=k+i-1;
if ipr~=k
A([k,ipr],:)=A([ipr,k],:)
b([k,ipr])=b([ipr,k])
end
for i=k+1:n
fac=-A(i,k)/A(k,k);
A(i,k:m)=A(i,k:m)+fac*A(k,k:m)
b(i)=b(i)+fac*b(k)
end
end
% back substitution
x=zeros(n,1)
x(n)=b(n)/A(n,n)
for i=n-1:-1:1
x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i)
end
14
clear, clc
A=[10 -2 -1; -1 15 -3;-2 -5 16 ]
b=[1800; 1000; 3350]
x1=GaussSeidel(A,b,[0 0 0],1e-6)
% x1 = 235.9971 138.8530 282.2661
with function
function x=GaussSeidel(A,b,guess,eps)
[m,n]=size(A);
C=A;
x=guess;
for i=1:n
C(i,1:n)=C(i,1:n)/A(i,i);
d(i)=b(i)/A(i,i);
C(i,i)=0.0;
end
while (1)
for i=1:m
xnew(i)=d(i)-C(i,:)*x';
if(xnew(i)~=0)
err(i)=abs((xnew(i)-x(i))/xnew(i));
end
end
if(max(err)<eps)
break;
end
x=xnew;
end
15
Sec 54
85-220 Analysis of Mechanical Systems
Winter 2014
Tutorial 5
1. Five reactors linked by pipes are shown in Figure. The rate of mass flow through each
pipe is computed as the product of flow (Q) and concentration (c). At steady state, the
mass flow into and out of each reactor must be equal. For example, for the first reactor, a
mass balance can be written as
Write mass balances for the remaining reactors in the Figure and express the equations in
matrix form. Use
a) LU factorization:
b) matlab function linsolve
to solve for the concentrations in each reactor.
Solution:
Reactor 1
Reactor 2
Reactor 3
Reactor 4
16
Reactor 5
a)
B=[6 0 -1 0 0;1 -1 0 0 0;0 -1 9 0 0;0 1 8 -11 2;3 1 0 0 -4];
b=[50;0;160;0;0];
[L U]=lu(B,b);
d=L\b
x=U\d
x = 11.5094
11.5094
19.0566
16.9983
11.5094
b)linsolve(B,b)
x = 11.5094
11.5094
19.0566
16.9983
11.5094
2. For the truss shown in the figure, the sum of the forces in both horizontal and vertical
directions must be zero at each node(the system is at equilibrium). Therefore, for node 1:
17
for node 2:
for node 3:
Express this set of linear algebraic equations in matrix form and then use Gauss
Elimination with Partial Pivoting to solve for the unknowns.
Note: the angles in MATLAB must be entered in radians
Solution:
node 1
node 2:
node 3:
18
A=[-cos(30/180*pi) 0 cos(60/180*pi) 0 0 0;-sin(30/180*pi) 0 -sin(60/180*pi) 0
0 0;cos(30/180*pi) 1 0 1 0 0;sin(30/180*pi) 0 0 0 1 0;0 -1 -cos(60/180*pi) 0
0 0;0 0 sin(60/180*pi) 0 0 1];
b=[0;1000;0;0;0;0];
x=Gausspivot(A,b)
%Ans
%
% -500.00
% 433.01
% -866.03
% 0
% 250.00
% 750.00
With function
function x=Gausspivot(A,b)
[m,n]=size(A);
for k=1:m-1
[big,i]=max(abs(A(k:m,k)))
ipr=k+i-1;
if ipr~=k
A([k,ipr],:)=A([ipr,k],:)
b([k,ipr])=b([ipr,k])
end
for i=k+1:n
fac=-A(i,k)/A(k,k);
A(i,k:m)=A(i,k:m)+fac*A(k,k:m)
b(i)=b(i)+fac*b(k)
end
end
% back substitution
x=zeros(n,1)
x(n)=b(n)/A(n,n)
for i=n-1:-1:1
x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i)
end
19
clear, clc
A=[10 -2 -1; -1 15 -3;-2 -5 16 ]
b=[1800; 1000; 3350]
x1=GaussSeidel(A,b,[0 0 0],1e-6)
% x1 = 235.9971 138.8530 282.2661
with function
function x=GaussSeidel(A,b,guess,eps)
[m,n]=size(A);
C=A;
x=guess;
for i=1:n
C(i,1:n)=C(i,1:n)/A(i,i);
d(i)=b(i)/A(i,i);
C(i,i)=0.0;
end
while (1)
for i=1:m
xnew(i)=d(i)-C(i,:)*x';
if(xnew(i)~=0)
err(i)=abs((xnew(i)-x(i))/xnew(i));
end
end
if(max(err)<eps)
break;
end
x=xnew;
end
20