If X 10 Then If X 5 Then X 5 Else Print X End If Else DO If X 50 Exit X X - 5 End Do End If
If X 10 Then If X 5 Then X 5 Else Print X End If Else DO If X 50 Exit X X - 5 End Do End If
2.1
IF x < 10 THEN
IF x < 5 THEN
x = 5
ELSE
PRINT x
END IF
ELSE
DO
IF x < 50 EXIT
x = x - 5
END DO
END IF
2.2
Step 1: Start
Step 2: Initialize sum and count to zero
Step 3: Examine top card.
Step 4: If it says end of data proceed to step 9; otherwise, proceed to next step.
Step 5: Add value from top card to sum.
Step 6: Increase count by 1.
Step 7: Discard top card
Step 8: Return to Step 3.
Step 9: Is the count greater than zero?
If yes, proceed to step 10.
If no, proceed to step 11.
Step 10: Calculate average = sum/count
Step 11: End
2.3
start
sum = 0
count = 0
INPUT
value
value =
end of data
value =
end of data
sum = sum + value
count = count + 1
T
F
count > 0
average = sum/count
end
T
F
2.4
Students could implement the subprogram in any number of languages. The following
Fortran 90 program is one example. It should be noted that the availability of complex
variables in Fortran 90, would allow this subroutine to be made even more concise.
However, we did not exploit this feature, in order to make the code more compatible with
Visual BASIC, MATLAB, etc.
PROGRAM Rootfind
IMPLICIT NONE
INTEGER::ier
REAL::a, b, c, r1, i1, r2, i2
DATA a,b,c/1.,5.,2./
CALL Roots(a, b, c, ier, r1, i1, r2, i2)
IF (ier .EQ. 0) THEN
PRINT *, r1,i1," i"
PRINT *, r2,i2," i"
ELSE
PRINT *, "No roots"
END IF
END
SUBROUTINE Roots(a, b, c, ier, r1, i1, r2, i2)
IMPLICIT NONE
INTEGER::ier
REAL::a, b, c, d, r1, i1, r2, i2
r1=0.
r2=0.
i1=0.
i2=0.
IF (a .EQ. 0.) THEN
IF (b <> 0) THEN
r1 = -c/b
ELSE
ier = 1
END IF
ELSE
d = b**2 - 4.*a*c
IF (d >= 0) THEN
r1 = (-b + SQRT(d))/(2*a)
r2 = (-b - SQRT(d))/(2*a)
ELSE
r1 = -b/(2*a)
r2 = r1
i1 = SQRT(ABS(d))/(2*a)
i2 = -i1
END IF
END IF
END
The answers for the 3 test cases are: (a) 0.438, -4.56; (b) 0.5; (c) 1.25 + 2.33i; 1.25
2.33i.
Several features of this subroutine bear mention:
The subroutine does not involve input or output. Rather, information is passed in and out
via the arguments. This is often the preferred style, because the I/O is left to the
discretion of the programmer within the calling program.
Note that an error code is passed (IER = 1) for the case where no roots are possible.
2.5 The development of the algorithm hinges on recognizing that the series approximation of the
sine can be represented concisely by the summation,
x
i
i
i
n 2 1
1
2 1
( )!
where i = the order of the approximation. The following algorithm implements this
summation:
Step 1: Start
Step 2: Input value to be evaluated x and maximum order n
Step 3: Set order (i) equal to one
Step 4: Set accumulator for approximation (approx) to zero
Step 5: Set accumulator for factorial product (fact) equal to one
Step 6: Calculate true value of sin(x)
Step 7: If order is greater than n then proceed to step 13
Otherwise, proceed to next step
Step 8: Calculate the approximation with the formula
approx approx ( 1)
x
factor
i-1
2i-1
+
Step 9: Determine the error
100%
true
approx true
%error
x
y
1
tan
,
_
x
y
1
tan
,
_
x
y
1
tan 0
2
y < 0
2
y < 0
180
Polar
180
Polar
End Polar
= 3.141593
T
T
T
T
T
F
F
F
F
And here is a VBA function procedure to implement it:
Option Explicit
Function Polar(x, y)
Dim th As Single, r As Single
Const pi As Single = 3.141593
r = Sqr(x ^ 2 + y ^ 2)
If x < 0 Then
If y > 0 Then
th = Atn(y / x) + pi
ElseIf y < 0 Then
th = Atn(y / x) - pi
Else
th = pi
End If
Else
If y > 0 Then
th = pi / 2
ElseIf y < 0 Then
th = -pi / 2
Else
th = 0
End If
End If
Polar = th * 180 / pi
End Function
The results are:
x y
1 1 90
1 -1 -90
1 0 0
-1 1 135
-1 -1 -135
-1 0 180
0 1 90
0 -1 -90
0 0 0
4.18 f(x) = x-1-1/2*sin(x)
f '(x) = 1-1/2*cos(x)
f ''(x) = 1/2*sin(x)
f '''(x) = 1/2*cos(x)
f
IV
(x) = -1/2*sin(x)
Using the Taylor Series Expansion (Equation 4.5 in the book), we obtain the following
1
st
, 2
nd
, 3
rd
, and 4
th
Order Taylor Series functions shown below in the Matlab program-
f1, f2, f4. Note the 2
nd
and 3
rd
Order Taylor Series functions are the same.
From the plots below, we see that the answer is the 4
th
Order Taylor Series expansion .
x=0:0.001:3.2;
f=x-1-0.5*sin(x);
subplot(2,2,1);
plot(x,f);grid;title('f(x)=x-1-0.5*sin(x)');hold on
f1=x-1.5;
e1=abs(f-f1); %Calculates the absolute value of the
difference/error
subplot(2,2,2);
plot(x,e1);grid;title('1st Order Taylor Series Error');
f2=x-1.5+0.25.*((x-0.5*pi).^2);
e2=abs(f-f2);
subplot(2,2,3);
plot(x,e2);grid;title('2nd/3rd Order Taylor Series Error');
f4=x-1.5+0.25.*((x-0.5*pi).^2)-(1/48)*((x-0.5*pi).^4);
e4=abs(f4-f);
subplot(2,2,4);
plot(x,e4);grid;title('4th Order Taylor Series Error');hold off
0 1 2 3 4
-1
0
1
2
3
f(x)=x-1-0.5*sin(x)
0 1 2 3 4
0
0.2
0.4
0.6
0.8
1s t Order Taylor Series Error
0 1 2 3 4
0
0. 05
0. 1
0. 15
0. 2
2nd/ 3rd Order Taylor Series Error
0 1 2 3 4
0
0. 005
0.01
0. 015
4t h Order Taylor Series Error
4.19 EXCEL WORKSHEET AND PLOTS
First Derivative Approximations Compared to Theoretical
-4.0
-2.0
0.0
2.0
4.0
6.0
8.0
10.0
12.0
14.0
-2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5
x-values
f
'
(
x
)
Theoretical
Backward
Centered
Forward
Approximations of the 2nd Derivative
-15.0
-10.0
-5.0
0.0
5.0
10.0
15.0
-2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5
x-values
f
'
'
(
x
)
f''(x)-Theory
f''(x)-Backward
f''(x)-Centered
f''(x)-Forward
x f(x) f(x-1) f(x+1) f(x-2) f(x+2) f''(x)-
Theory
f''(x)-
Back
f''(x)-Cent f''(x)-
Forw
-2.000 0.000 -2.891 2.141 3.625 3.625 -12.000 150.500 -12.000 -10.500
-1.750 2.141 0.000 3.625 -2.891 4.547 -10.500 -12.000 -10.500 -9.000
-1.500 3.625 2.141 4.547 0.000 5.000 -9.000 -10.500 -9.000 -7.500
-1.250 4.547 3.625 5.000 2.141 5.078 -7.500 -9.000 -7.500 -6.000
-1.000 5.000 4.547 5.078 3.625 4.875 -6.000 -7.500 -6.000 -4.500
-0.750 5.078 5.000 4.875 4.547 4.484 -4.500 -6.000 -4.500 -3.000
-0.500 4.875 5.078 4.484 5.000 4.000 -3.000 -4.500 -3.000 -1.500
-0.250 4.484 4.875 4.000 5.078 3.516 -1.500 -3.000 -1.500 0.000
0.000 4.000 4.484 3.516 4.875 3.125 0.000 -1.500 0.000 1.500
0.250 3.516 4.000 3.125 4.484 2.922 1.500 0.000 1.500 3.000
0.500 3.125 3.516 2.922 4.000 3.000 3.000 1.500 3.000 4.500
0.750 2.922 3.125 3.000 3.516 3.453 4.500 3.000 4.500 6.000
1.000 3.000 2.922 3.453 3.125 4.375 6.000 4.500 6.000 7.500
1.250 3.453 3.000 4.375 2.922 5.859 7.500 6.000 7.500 9.000
1.500 4.375 3.453 5.859 3.000 8.000 9.000 7.500 9.000 10.500
1.750 5.859 4.375 8.000 3.453 10.891 10.500 9.000 10.500 12.000
2.000 8.000 5.859 10.891 4.375 14.625 12.000 10.500 12.000 13.500
x f(x) f(x-1) f(x+1) f'(x)-Theory f'(x)-Back f'(x)-Cent f'(x)-Forw
-2.000 0.000 -2.891 2.141 10.000 11.563 10.063 8.563
-1.750 2.141 0.000 3.625 7.188 8.563 7.250 5.938
-1.500 3.625 2.141 4.547 4.750 5.938 4.813 3.688
-1.250 4.547 3.625 5.000 2.688 3.688 2.750 1.813
-1.000 5.000 4.547 5.078 1.000 1.813 1.063 0.313
-0.750 5.078 5.000 4.875 -0.313 0.313 -0.250 -0.813
-0.500 4.875 5.078 4.484 -1.250 -0.813 -1.188 -1.563
-0.250 4.484 4.875 4.000 -1.813 -1.563 -1.750 -1.938
0.000 4.000 4.484 3.516 -2.000 -1.938 -1.938 -1.938
0.250 3.516 4.000 3.125 -1.813 -1.938 -1.750 -1.563
0.500 3.125 3.516 2.922 -1.250 -1.563 -1.188 -0.813
0.750 2.922 3.125 3.000 -0.313 -0.813 -0.250 0.313
1.000 3.000 2.922 3.453 1.000 0.313 1.063 1.813
1.250 3.453 3.000 4.375 2.688 1.813 2.750 3.688
1.500 4.375 3.453 5.859 4.750 3.688 4.813 5.938
1.750 5.859 4.375 8.000 7.188 5.938 7.250 8.563
2.000 8.000 5.859 10.891 10.000 8.563 10.063 11.563
8.11 Substituting the parameter values yields
75 . 1
1000
1
150
1
10
3
+
f
A plot of the function suggests a root at about 0.45.
-3
-2
-1
0
1
2
3
0 0.2 0.4 0.6
But suppose that we do not have a plot. How do we come up with a good initial guess. The void
fraction (the fraction of the volume that is not solid; i.e. consists of voids) varies between 0 and 1. As
can be seen, a value of 1 (which is physically unrealistic) causes a division by zero. Therefore, two
physically-based initial guesses can be chosen as 0 and 0.99. Note that the zero is not physically
realistic either, but since it does not cause any mathematical difficulties, it is OK. Applying bisection
yields a result of = 0.461857 in 15 iterations with an absolute approximate relative error of 6.510
3
%
8.12
The total pressure is equal to the partial pressures of the components:
t b
P P P +
According to Antoines equation
b
b
b
C T
B
A
b
e P
+
t
t
t
C T
B
A
t
e P
+
P e e T f
t
t
t
b
b
b
C T
B
A
C T
B
A
The root of this equation can be evaluated to yield T = 350.5.
8.13 There are a variety of ways to solve this system of 5 equations
] [CO
] ][HCO [H
2
3
1
+
K (1)
] [HCO
] ][CO [H
3
2
3
2
+
K (2)
] ][OH [H =
+
w
K (3)
] [CO ] [HCO ] [CO =
2
3 3 2
+ +
T
c (4)
] [H ] [OH + ] [CO 2 ] [HCO = Alk
+ 2
3 3
+
(5)
One way is to combine the equations to produce a single polynomial. Equations 1 and 2 can be solved
for
1
3
3 2
] ][HCO [H
] CO [H
*
K
+
] [HCO
] [H
] [CO
3
2 2
3
+
K
These results can be substituted into Eq. 4, which can be solved for
T
c F
0 3 2
] CO [H
*
T
c F
1 3
] [HCO
T
c F
2
2
3
] [CO
where F
0
, F
1
, and F
2
are the fractions of the total inorganic carbon in carbon dioxide, bicarbonate and
carbonate, respectively, where
2 1 1
2
2
0
] [H ] [H
] [H
=
K K K
F
+ +
+ +
+
2 1 1
2
1
1
] [H ] [H
] [H
=
K K K
K
F
+ +
+ +
+
2 1 1
2
2 1
2
] [H ] [H
=
K K K
K K
F
+ +
+ +
Now these equations, along with the Eq. 3 can be substituted into Eq. 5 to give
Alk ] [H ] [H + 2 = 0
+ +
2 1
+
w T T
K c F c F
Although it might not be apparent, this result is a fourth-order polynomial in [H
+
].
( ) ( )
2 +
1 1 2 1
3 +
1
4 +
] H [ Alk ] H [ Alk ] H [
T w
c K K K K K K + + + +
( ) 0 ] H [ 2 Alk
2 1
+
2 1 1 2 1
+
w T w
K K K c K K K K K K
Substituting parameter values gives
0 10 512 . 2 ] H [ 10 055 . 1 ] H [ 10 012 . 5 ] H [ 10 001 . 2 ] H [
31 + 19 2 + 10 3 + 3 4 +
+
This equation can be solved for [H
+
] = 2.51x10
-7
(pH = 6.6). This value can then be used to compute
8
7
14
10 98 . 3
10 51 . 2
10
] [OH
( )
( ) ( )
( ) 001 . 0 10 3 33304 . 0 10 3
10 10 10 2.51 10 10 2.51
10 2.51
= ] CO H [
3 3
3 . 10 3 . 6 7 - 3 . 6
2
7 -
2
7 -
3 2
*
+ +
( )
( ) ( )
( ) 002 . 0 10 3 666562 . 0 10 3
10 10 10 2.51 10 10 2.51
10 2.51 10
= ] HCO [
3 3
3 . 10 3 . 6 7 - 3 . 6
2
7 -
-7 3 . 6
3
+ +
( ) ( )
( ) M
4 3 3
3 . 10 3 . 6 7 - 3 . 6
2
7 -
3 . 10 3 . 6
2
3
10 33 . 1 10 3 000133 . 0 10 3
10 10 10 2.51 10 10 2.51
10 10
= ] CO [
+ +
8.14 The integral can be evaluated as
1
1
]
1
,
_
in out
in
out
max max max
ln
1 1 out
in
C C
C
C
K
k
dc
k C k
K
C
C
Therefore, the problem amounts to finding the root of
1
1
]
1
,
_
+
in out
in
out
max
out
ln
1
) ( C C
C
C
K
k F
V
C f
Excel solver can be used to find the root:
8.24
%Region from x=8 to x=10
x1=[8:.1:10];
y1=20*(x1-(x1-5))-15-57;
figure (1)
plot(x1,y1)
grid
%Region from x=7 to x=8
x2=[7:.1:8];
y2=20*(x2-(x2-5))-57;
figure (2)
plot(x2,y2)
grid
%Region from x=5 to x=7
x3=[5:.1:7];
y3=20*(x3-(x3-5))-57;
figure (3)
plot(x3,y3)
grid
%Region from x=0 to x=5
x4=[0:.1:5];
y4=20*x4-57;
figure (4)
plot(x4,y4)
grid
%Region from x=0 to x=10
figure (5)
plot(x1,y1,x2,y2,x3,y3,x4,y4)
grid
title('shear diagram')
a=[20 -57]
roots(a)
a =
20 -57
ans =
2.8500
0 1 2 3 4 5 6 7 8 9 10
-60
-40
-20
0
20
40
60
s hear diagram
8.25
%Region from x=7 to x=8
x2=[7:.1:8];
y2=-10*(x2.^2-(x2-5).^2)+150+57*x2;
figure (2)
plot(x2,y2)
grid
%Region from x=5 to x=7
x3=[5:.1:7];
y3=-10*(x3.^2-(x3-5).^2)+57*x3;
figure (3)
plot(x3,y3)
grid
%Region from x=0 to x=5
x4=[0:.1:5];
y4=-10*(x4.^2)+57*x4;
figure (4)
plot(x4,y4)
grid
%Region from x=0 to x=10
figure (5)
plot(x1,y1,x2,y2,x3,y3,x4,y4)
grid
title('moment diagram')
a=[-43 250]
roots(a)
a =
-43 250
ans =
5.8140
0 1 2 3 4 5 6 7 8 9 10
-60
-40
-20
0
20
40
60
80
100
moment diagram
8.26 A Matlab script can be used to determine that the slope equals zero at x = 3.94 m.
%Region from x=8 to x=10
x1=[8:.1:10];
y1=((-10/3)*(x1.^3-(x1-5).^3))+7.5*(x1-8).^2+150*(x1-7)+(57/2)*x1.^2-
238.25;
figure (1)
plot(x1,y1)
grid
%Region from x=7 to x=8
x2=[7:.1:8];
y2=((-10/3)*(x2.^3-(x2-5).^3))+150*(x2-7)+(57/2)*x2.^2-238.25;
figure (2)
plot(x2,y2)
grid
%Region from x=5 to x=7
x3=[5:.1:7];
y3=((-10/3)*(x3.^3-(x3-5).^3))+(57/2)*x3.^2-238.25;
figure (3)
plot(x3,y3)
grid
%Region from x=0 to x=5
x4=[0:.1:5];
y4=((-10/3)*(x4.^3))+(57/2)*x4.^2-238.25;
figure (4)
plot(x4,y4)
grid
%Region from x=0 to x=10
figure (5)
plot(x1,y1,x2,y2,x3,y3,x4,y4)
grid
title('slope diagram')
a=[-10/3 57/2 0 -238.25]
roots(a)
a =
-3.3333 28.5000 0 -238.2500
ans =
7.1531
3.9357
-2.5388
0 1 2 3 4 5 6 7 8 9 10
-250
-200
-150
-100
-50
0
50
100
150
200
slope diagram
8.27
%Region from x=8 to x=10
x1=[8:.1:10];
y1=(-5/6)*(x1.^4-(x1-5).^4)+(15/6)*(x1-8).^3+75*(x1-7).^2+(57/6)*x1.^3-
238.25*x1;
figure (1)
plot(x1,y1)
grid
%Region from x=7 to x=8
x2=[7:.1:8];
y2=(-5/6)*(x2.^4-(x2-5).^4)+75*(x2-7).^2+(57/6)*x2.^3-238.25*x2;
figure (2)
plot(x2,y2)
grid
%Region from x=5 to x=7
x3=[5:.1:7];
y3=(-5/6)*(x3.^4-(x3-5).^4)+(57/6)*x3.^3-238.25*x3;
figure (3)
plot(x3,y3)
grid
%Region from x=0 to x=5
x4=[0:.1:5];
y4=(-5/6)*(x4.^4)+(57/6)*x4.^3-238.25*x4;
figure (4)
plot(x4,y4)
grid
%Region from x=0 to x=10
figure (5)
plot(x1,y1,x2,y2,x3,y3,x4,y4)
grid
title('displacement curve')
a =
-3.3333 28.5000 0 -238.2500
ans =
7.1531
3.9357
-2.5388
Therefore, other than the end supports, there are no points of zero displacement along the beam.
0 1 2 3 4 5 6 7 8 9 10
-600
-500
-400
-300
-200
-100
0
dis plac ement c urve
8.39 Excel Solver solution:
8.40 The problem reduces to finding the value of n that drives the second part of the equation to
1. In other words, finding the root of
( ) 0 1 1
1
) (
/ ) 1 (
n n
c
R
n
n
n f
Inspection of the equation indicates that singularities occur at x = 0 and 1. A plot indicates that
otherwise, the function is smooth.
-1
-0.5
0
0.5
0 0.5 1 1.5
A tool such as the Excel Solver can be used to locate the root at n = 0.8518.
8.41 The sequence of calculation need to compute the pressure drop in each pipe is
2
) 2 / (D A
A
Q
v
v D
Re
( )
1
1
]
1
f
f f
1
4 . 0 Re log 0 . 4 root
D
v
f P
2
2
The six balance equations can then be solved for the 6 unknowns.
The root location can be solved with a technique like the modified false position method. A bracketing
method is advisable since initial guesses that bound the normal range of friction factors can be readily
determined. The following VBA function procedure is designed to do this
Option Explicit
Function FalsePos(Re)
Dim iter As Integer, imax As Integer
Dim il As Integer, iu As Integer
Dim xrold As Single, fl As Single, fu As Single, fr As Single
Dim xl As Single, xu As Single, es As Single
Dim xr As Single, ea As Single
xl = 0.00001
xu = 1
es = 0.01
imax = 40
iter = 0
fl = f(xl, Re)
fu = f(xu, Re)
Do
xrold = xr
xr = xu - fu * (xl - xu) / (fl - fu)
fr = f(xr, Re)
iter = iter + 1
If xr <> 0 Then
ea = Abs((xr - xrold) / xr) * 100
End If
If fl * fr < 0 Then
xu = xr
fu = f(xu, Re)
iu = 0
il = il + 1
If il >= 2 Then fl = fl / 2
ElseIf fl * fr > 0 Then
xl = xr
fl = f(xl, Re)
il = 0
iu = iu + 1
If iu >= 2 Then fu = fu / 2
Else
ea = 0#
End If
If ea < es Or iter >= imax Then Exit Do
Loop
FalsePos = xr
End Function
Function f(x, Re)
f = 4 * Log(Re * Sqr(x)) / Log(10) - 0.4 - 1 / Sqr(x)
End Function
The following Excel spreadsheet can be set up to solve the problem. Note that the function call,
=falsepos(F8), is entered into cell G8 and then copied down to G9:G14. This invokes the
function procedure so that the friction factor is determined at each iteration.
The resulting final solution is
8.42 The following application of Excel Solver can be set up:
The solution is:
8.43 The results are
0
20
40
60
80
100
120
1 2 3
8.44
% Shuttle Liftoff Engine Angle
% Newton-Raphson Method of iteratively finding a single root
format long
% Constants
LGB = 4.0; LGS = 24.0; LTS = 38.0;
WS = 0.230E6; WB = 1.663E6;
TB = 5.3E6; TS = 1.125E6;
es = 0.5E-7; nmax = 200;
% Initial estimate in radians
x = 0.25
%Calculation loop
for i=1:nmax
fx = LGB*WB-LGB*TB-LGS*WS+LGS*TS*cos(x)-LTS*TS*sin(x);
dfx = -LGS*TS*sin(x)-LTS*TS*cos(x);
xn=x-fx/dfx;
%convergence check
ea=abs((xn-x)/xn);
if (ea<=es)
fprintf('convergence: Root = %f radians \n',xn)
theta = (180/pi)*x;
fprintf('Engine Angle = %f degrees \n',theta)
break
end
x=xn;
x
end
% Shuttle Liftoff Engine Angle
% Newton-Raphson Method of iteratively finding a single root
% Plot of Resultant Moment vs Engine Anale
format long
% Constants
LGB = 4.0; LGS = 24.0; LTS = 38.0;
WS = 0.195E6; WB = 1.663E6;
TB = 5.3E6; TS = 1.125E6;
x=-5:0.1:5;
fx = LGB*WB-LGB*TB-LGS*WS+LGS*TS*cos(x)-LTS*TS*sin(x);
plot(x,fx)
grid
axis([-6 6 -8e7 4e7])
title('Space Shuttle Resultant Moment vs Engine Angle')
xlabel('Engine angle ~ radians')
ylabel('Resultant Moment ~ lb-ft')
x =
0.25000000000000
x =
0.15678173034564
x =
0.15518504730788
x =
0.15518449747125
convergence: Root = 0.155184 radians
Engine Angle = 8.891417 degrees
-6 -4 -2 0 2 4 6
-8
-6
-4
-2
0
2
4
x 10
7
Space Shutt le Result ant Moment vs Engine Angle
Engine angle ~ radians
R
e
s
u
l
t
a
n
t
M
o
m
e
n
t
~
l
b
-
f
t
8.45 This problem was solved using the roots command in Matlab.
c =
1 -33 -704 -1859
roots(c)
ans =
48.3543
-12.2041
-3.1502
Therefore,
1
= 48.4 Mpa
2
= -3.15 MPa
3
= -12.20 MPa
T t
1 100 20
2 88.31493 30.1157
3 80.9082 36.53126