Escolar Documentos
Profissional Documentos
Cultura Documentos
Midterm Exam I
Feb 27, 2014
Spring 2014
I pledge that I have neither given nor received any unauthorized assistance on this exam.
________________________
____________________________
Student Name
1. MATLAB
(Student Signature)
25 points
or
_Single_________________________
The Colon operator: What will the following commands produce as output?
>>i=21:-1:15
_21 20 19 18 17 16 15 ______________________
>>length(i) ___7________________________________________________
>>j=[0.3:0.2:1.2] _____________________________________________
j =
0.3000
0.5000
0.7000
0.9000
1.1000
>>k=i-j
______________________________________________________
Error using Matrix dimensions must
agree
>>k=-(12:-1:8) -12 -11 -10 -9 -8
>>k=-15:-1:20 Empty matrix: 1-by-0
Calculations past maximum and minimum ranges, NaN, Inf and others
>>realmax(single)*4 Inf
>>realmin('single')-4 -4
Precedence ordering
>>8+4^2--1/3*3
25
>>8+4^2---1/3*3
23
diag function and its use. Let A=[1 2 3;4 5 6;7 8 9];b=[1;1;1] or = [ ] , = []
>>diag(A) ___________________________________________________
1
5
9
>>diag(diag(A)) _____________________________________________
1
0
0
0
5
0
0
0
9
Array addressing/manipulation. Let A=[7 8 9;4 5 6;1 2 3;] or = [ ]
______________________________________
>> A(2:3,3:-1:2)
ans =
6
3
5
2
>> A(3:2,3:-1:2)
______________________________________
Empty matrix: 0-by-2
>>fliplr(A)
_____________________________________________
ans =
9
6
3
8
5
2
7
4
1
>>sum(A) _________________________________________________
12
15
18
Logical:
2|Page
Let the following variables be the only variables defined in the workspace.
A=[1 2;3 4 ]
B=[2 4; 3 5 ]
What will the following expressions produce in MATLAB?
A==B
ans =
0
1
0
0
________________________________________________
A~=B
ans =
1
0
1
1
________________________________________________
~(A>=B)
ans =
1
0
1
1
_________________________________________________
0
1
_________________________________________________
(A>2 | B<5)
ans =
1
1
1
1
_________________________________________________
3|Page
if (all(B>0))
display(All elements are positive)
end
All elements are positive
________________________________________________
4|Page
Consider turbulent flow through the network shown above. The governing equations are the
pressure drop equations for each pipe element i j and the mass balance equation at each
node. They are given for laminar flow by,
2
=
Where = .02 .
5|Page
function PipeTurb
%x=[p_2 p_3 p_4 v_12 v_23 v_24 v_34 v_35 v_46]
%Provide the guess vector
options=optimset('Display','off','TolFun',1.e-12);
x=[25000 20000 20000 ones(1,7)]
x=fsolve(@PipeF,x,options)
res=PipeF(x);
fprintf(1,'Solution for turbulent flow\n\n')
for i=1:9
fprintf(1,'x(%2i)= %16.6e; res(%2i)= %16.6e\n', i,x(i),i,res(i) )
end
end
%==============================
function g f =PipeF(x)
l12=1000; l23=800; l24=800; l34=900; l35=1000; l46=1000; %Lengths
d12=0.10; d23=0.08; d24=0.08; d34=0.10; d35=0.09; d46=0.09; %Diameters
%part
p1=30000; p5=10000; p6=100000; %Pressures
% Alphas
Areas
a12=.02*l12/d12; aa12=pi*d12^2/4;
a23=.02*l23/d23; aa23=pi*d23^2/4;
a24=.02*l24/d24 3; aa24=pi*d24^2/4;
a34=.02*l34/d34; aa34=pi*d34^2/4;
a35=.02*l35/d35; aa35=pi*d35^2/4;
a46=.02*l46/d46; aa46=pi*d346^2/4;
%Load the guess vector into appropriate variables.
p2=x(1); p3=x(2);p4=x(3);
v12=x(4); v23=x(5); v24=x(6);v34=x(7);v35=x(8);v46=x(9);
%Assemble the equations
f(1) = p1 - p2 -a12*v12^2;
f(2) = p2 - p3 -a23*v23^2;
f(3) = p2 - p4 -a24*v24^2;
f(4) = p3 - p4 -a34*v234^2;
f(5) = p3 - p5 -a35*v35^2;
f(6) = p4 - p6 -a46*v46^2;
f(7)=d12^2*v12 - d23^2*v23 - d24^2*v24;
F f(8)=d23^2*v23 a d34^2*v34 - d35^2*v35;
f(9)=d24^2*v24 + d34^23*v34 - d46^2*v46;
end
6|Page
A pipe carrying water splits into two segments at node P and joins back at node Q. Properties
of water are: = 1000 kg/m3 , = 0.001 Pa. s. Here is the density of the water and is
the viscosity of water. The lengths ( ), diameters ( ), roughness ( ) for the two pipe
segments between P and Q, as well as the total flow rate are as shown in figure. The
problem is to determine the individual velocities (1 , 2 ) in each of the pipe segments 1 and
2. The equation to be satisfied is obtained based on the fact that the pressure drop between
points P and Q is the same no matter which path the fluid takes. This principle leads to the
following equation.
12
1 (1 ) 1
22
= 2 (2 ) 2
(1)
Where (1 , 2 ) are the velocities in the two pipes and (1 , 2 ) are called the friction factors
given by the following Churchill equation.
1/12
8 12
1
( ) = 8 [( ) +
]
( + )1.5
where
= [2.457 ln ((7/(
1
0.9
)) +0.27( /
16
)] , = [
)
37530 16
and =
Finally the mass balance equation at the nodes (P or Q) provides another equation as,
(12 1 + 22 2 ) =
4
(2)
7|Page
(c) For the case = [1000 900], = [0.1 0.09], it is required to find that value of
which makes 1 = 2 using fsolve The following MATLAB functions are claimed to
solve this problem. Will it solve the problem? Explain? There are several errors in the
functions find at least 5 errors and fix them.
It is posed as three unknowns in three equations. So it will work. The three unknowns are
= [1 2 ]
The three equations are:
1 12
2 22
(
)
(
)
1 = 1 1
2 2
=0
1 2
2 2
2 = (12 1 + 22 2 ) = 0
4
3 = 1 2 = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function MidP1
%% Problem - parallel pipe using "fsolve"
%unknown vector x=[v1 v2 qt]
clear;clc
options=optimset('TolFun',1.e-4);
sol=fsolve(@ParrPipe,[1 1 0.002],Options);
fprintf('Velocity (1) %g \nVelocity(2) %g \nQt
%g \n',sol);
end
function f=ParPipe(x)
%unknown vector x=[v1 v2 qt]
v(1)=x(1);
v(2)=x(2);
qt=x(3);
L=[1000 900]; %m
D=[0.1 0.09]; %m
e=[0.000046 0]; %Roughness in meters
rho=1000;
%density kg/m^3
mu =0.001;
%viscosity Pa.s
%Note that the following are all vectors: area, Re, A, B, ff,
area=(pi/4)*D.^2; %area
Re = (D .*v)*rho/mu;
8|Page
29
30
31
32
33
34
35
9|Page
(2+3+2+15+3=25 points)
2
+
7
+5
+ 10 = 5 sin()
3
2
( = 0) = 0;
|
= 1;
=0
One is interested in finding the solutions ()
2
|
=0
2 =0
1 = 2 = 1
2 = 3 = 2
3 = 5 sin() 10 1 5 1 2 712 3 = 3
1 (0) = 0
2 (0) = 1
3 (0) = 0
(e) What MATLAB tool will you use to solve the above problem?
Ode45
10 | P a g e