CHAPTER 2 2.1 IF x < 10 THENIF x < 5 THENx = 5ELSEPRINT xEND IFELSEDOIF x < 50 EXITx = x - 5END DOEND IF2.2 Step 1: Start Step 2: Initialize sum and countto 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 countby 1. Step 7: Discard top card Step 8: Return to Step 3. Step 9: Is the countgreater than zero? If yes, proceed to step 10. If no, proceed to step 11. Step 10: Calculate average = sum/countStep 11: End 2.3 start sum = 0 count = 0 INPUT value value = “end of data” value = “end of da ta” sum = sum + value count = count + 1 T F count > 0 average = sum/count end T F
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
2.2Step 1: StartStep 2: Initialize sum and count to zeroStep 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 cardStep 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.
2.4Students could implement the subprogram in any number of languages. The followingFortran 90 program is one example. It should be noted that the availability of complexvariables 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 withVisual BASIC, MATLAB, etc.
2.5 The development of the algorithm hinges on recognizing that the series approximation of thesine can be represented concisely by the summation,
xi
i
i
n 2 1
1 2 1
−
= −∑( )!
where i = the order of the approximation. The following algorithm implements thissummation:
Step 1: StartStep 2: Input value to be evaluated x and maximum order nStep 3: Set order (i) equal to oneStep 4: Set accumulator for approximation (approx) to zeroStep 5: Set accumulator for factorial product (fact) equal to oneStep 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 formulaapprox approx ( 1)
xfactor
i-12i-1
= + −Step 9: Determine the error
100%true
approxtrue%error −=
Step 10: Increment the order by oneStep 11: Determine the factorial for the next iteration
factor factor (2 i 2) (2 i 1)= • • − • • −Step 12: Return to step 7Step 13: End
Interpretation: The absolute percent relative error drops until at n = 6, it actually yields aperfect result (pure luck!). Beyond, n = 8, the errors starts to grow. This occurs because of round-off error, which will be discussed in Chap. 3.
2.8 AQ = 442/5 = 88.4
AH = 548/6 = 91.33
without final
AG =30(88.4) + 30(91.33)
30 + 30= 89 8667.
with final
AG =30(88.4) + 30(91.33) + 40(91)
30 + 30= 9032.
The following pseudocode provides an algorithm to program this problem. Notice that theinput of the quizzes and homeworks is done with logical loops that terminate when the user enters a negative grade:
Using the Taylor Series Expansion (Equation 4.5 in the book), we obtain the following1st, 2 nd, 3 rd, and 4 th Order Taylor Series functions shown below in the Matlab program-
f1, f2, f4. Note the 2nd
and 3rd
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 thedifference/errorsubplot(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
This can be rearranged and expressed as a roots problem
01
1075.1)1(15.0)(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 voidfraction (the fraction of the volume that is not solid; i.e. consists of voids) varies between 0 and 1. Ascan be seen, a value of 1 (which is physically unrealistic) causes a division by zero. Therefore, twophysically-based initial guesses can be chosen as 0 and 0.99. Note that the zero is not physicallyrealistic either, but since it does not cause any mathematical difficulties, it is OK. Applying bisectionyields a result of ε = 0.461857 in 15 iterations with an absolute approximate relative error of 6.5 ×10−3
%
8.12The total pressure is equal to the partial pressures of the components:
t b P P P +=
According to Antoine’s equation
b
bb C T
BA
b eP +−
= t
t t C T
BA
t eP +−
=
Combining the equations yields
0)( =−+= +−+−P eeT f t
t t
b
bb C T
BA
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
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=10x1=[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=8x2=[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=7x3=[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=5x4=[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=10figure (5)plot(x1,y1,x2,y2,x3,y3,x4,y4)gridtitle( 'slope diagram' )a=[-10/3 57/2 0 -238.25]roots(a)
%Region from x=8 to x=10x1=[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=8x2=[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=7x3=[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=5x4=[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=10figure (5)plot(x1,y1,x2,y2,x3,y3,x4,y4)gridtitle( 'displacement curve' )
a =-3.3333 28.5000 0 -238.2500
ans =7.15313.9357
-2.5388
Therefore, other than the end supports, there are no points of zero displacement along the beam.
8.40 The problem reduces to finding the value of n that drives the second part of the equation to1. In other words, finding the root of
( ) 0111
)( /)1( =−−−
= − nncR
nn
nf
Inspection of the equation indicates that singularities occur at x = 0 and 1. A plot indicates thatotherwise, 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/(DA π=
A
Qv =
µρvD=Re
( ) −−=f
f f 1
4.0Relog0.4root
Dv
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 bracketingmethod 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 thisOption Explicit
Function FalsePos(Re)Dim iter As Integer, imax As IntegerDim il As Integer, iu As IntegerDim xrold As Single, fl As Single, fu As Single, fr As SingleDim xl As Single, xu As Single, es As SingleDim xr As Single, ea As Single