You are on page 1of 10

EXAMPLE - 1

Let us try and solve a simple set of non-linear equations, which are tedious to solve by
hand.

Enter the two equations given above and then see them in formatted equations as well.

Solve them and see how many blocks were created. You know the answer already if you
have understood blocking.

Viola! Solution would be something like this:


EXAMPLE - 2

Let us try and solve a complex equation using Functions. You must remember this one, as
we have practiced this in our last class

Firstly, we define a function f_Darcy as

Function f_Darcy(Re,RR)
f=8*((8/Re)^12+((2.457*ln(1/((7/Re)^0.9+0.27*(RR))))^16+(37530/Re)
^16)^(-1.5))^(1/12)
f_Darcy=f
end

Then we provide input after the above definition

Re=5000 [-] "Reynolds number"


RR = 0.001 [-] "relative roughness"
f=f_Darcy(Re,RR) "Darcy friction factor"

If you have done everything right, the output should look like this:
EXAMPLE - 3

Another simple equation using Functions. This time, try and solve the equation by yourself
first.

We define the function PR as,

Function PR(v,T)
R = 188.9 [J/kg-K]
a = 70.89 [N-m^4/kg^2]
b = 0.0006059 [m^3/kg]
PR = R*T/(v-b)-a/(v*(v+b)+b*(v-b))
End

And then provide the input as,

v=0.1 [m^3/kg] "specific volume"


T=325 [K] "temperature"
P=PR(v,T) "pressure, estimated from the Peng-
Robinson equation of state"

If you have everything right, the final output should be,


EXAMPLE - 4

Another simple conditioning using Procedure. Try and practice the formatting without seeing
as well.

A simple example that finds the product, ratio, sum


and difference (M, D, A, S) of two values (X and Y):

Procedure Test(X,Y: M,D,A,S)


M:=X*Y "multiply"
D:=X/Y "divide"
A:=X+Y "add"
S:=X-Y "subtract"
end

A valid Call statement for Procedure Test shown above is:

Call Test(33,44: Product, Ratio, Sum, Difference) "Call to the Test


Procedure"

Another way to call the procedure:

Call Test(X, Y: Product, Ratio, 88, 32) "Alternative call to the Test
Procedure"
EXAMPLE - 5

Let us try and practice conditioning.

The following example develops a function named Nusselt which uses logic to provide the
appropriate value of the nondimensional Nusselt number for fully-developed flow in a
circular pipe subjected to a constant heat flux given the Reynolds number (Re), Prandtl
number (Pr), and the relative roughness (RR). The function uses correlations presented in
Nellis and Klein (2009). For laminar flow (Re < 2300), the Nusselt number is a constant,
Nu = 4.36.

The function begins by assigning Nusselt a value consistent with laminar flow and the using
the Return statement to return to the main program if Re < 2300.

Function Nusselt(Re, Pr, RR)


"assume flow is laminar"
Nusselt = 4.36 [-] "laminar flow Nusselt number"
if (Re<2300) then Return "return if flow is laminar"

For turbulent flow (Re > 2300), the friction factor is computed according to:

and used to compute the Nusselt number according to the Gnielinski correlation:

Note that the Gnielinski correlation is only valid for 2300 < Re < 5x106. The function
continues by using the above equations to assign Nusselt a value that is consistent with
turbulent flow. The Return statement is used to return to the calling program if the
Reynolds number is less than 5x106.

"assume Reynolds is turbulent"


f = (-2*log10(2*RR/7.54-5.02*log10(2*RR/7.54+13/Re)/Re))^(-2)
"friction factor"
Nusselt = f/8*(Re-1000)*Pr/(1+12.7*(Pr^(2/3)-1)*sqrt(f/8)) "Gnielinski
correlation"
if (Re<=5e6) then Return "return if Re is not out of range"

Finally, the function returns a value of -9 when the Reynolds number provided is greater
than 5x106 in order to indicate that the correlation is not valid.
"Reynolds number must be out of range"
Nusselt = -9 [-] "value if Re is out of range"
end
The function is tested at various Reynolds numbers:

"test the function"


Nu#_laminar=Nusselt(1000,0.7,0.001) "test for laminar flow"
Nu#_turbulent=Nusselt(5000,0.7,0.001) "test for turbulent flow"
Nu#_outofRange=Nusselt(6E6,0.7,0.001) "test for out of range"

After solution:
EXAMPLE - 6

A simple example of using GoTo option of EES.

Function Fact(N)
F:=1 "factorial"
i:=0 "counter"
10: i:=i+1 "increment counter"
F:=F*i "multiply factorial by counter"
If (i<N) Then GoTo 10 "see if counter has reached N"
Fact:=F "assign factorial"
End

Y= Fact(7) "Fact(7)=5040"
Y2=Factorial(7) "EES built-in factorial function"

Solving leads to Y = 5040 and Y2 = 5040.

Another way to build the function

Function Fact(N)
Fact:=1 "factorial"
If (N=0) then Return "check for N=0"
Repeat
Fact:=Fact*N "start with argument"
N:=N-1; "decrement argument by one"
Until (N<=1)
End
EXAMPLE - 7

An example to pass arrays as arguments in Functions. We will calculate sum of squares of


N numbers.

Function SumSquares(N, A[1..N])


S:=0 "initialize sum"
i:=1 "initialize index"
Repeat
S:=S+A[i]^2 "sum the square of each element"
i:=i+1 "increment the element"
Until (i>=N) "stopping condition"
SumSquares:=S "set the Function name to the calculated value"
end

N=5 "number of array elements"


duplicate i=1,N
X[i]=i "set the value of the array elements"
end
SS=SumSquares(N,X[1..N]) "call the sum of squares function"

Output should be SS = 30.

For extending the limit on N from above 100, we can use the following example.

$Constant N# = 150 "size of array"


Function SumSquares(A[1..N#])
S:=0 "initialize sum"
i:=1 "initialize index"
Repeat
S:=S+A[i]^2 "sum the square of each element"
i:=i+1 "increment the element"
Until (i>=N#) "stopping condition"
SumSquares:=S "set the function name to the calculated
value"
end
duplicate i=1,N#
X[i]=i "set the value of the array elements"
end
SS=SumSquares(X[1..N#]) "Call the sum of squares function"
EXAMPLE - 8

Furthermore, we can see how to define array table window for Functions. We can use this
for numerical differentiation.

The following example computes the temperature of an object that is initially at T ini and is
subjected to convection and radiation from an environment at T∞. The surface area of the
object is As and the total heat capacity is C. The convective heat transfer coefficient is h
and the object is assumed to have an emissivity of ε = 1. Assuming that the object is at a
uniform temperature, the time rate of change of its temperature is given by:

If we have to discretise the above equation, we can do it as,

Function Temp(duration, h_bar, A_s, T_ini, T_infinity, C, N)


Dtime:= duration/N
T[1]:= T_ini "initial temperature"
time[1]:=0
i:=1
Repeat
time[i+1]=time[i]+Dtime
T[i+1]=T[i]+(A_s*h_bar*(T_infinity-T[i])+A_s*sigma#*(T_infinity^4-
T[i]^4))*Dtime/C
i:=i+1
Until(i>N)
emp=T[N+1]
End
N=5 [-]
duration=100 [s] "duration of process"
h_bar=100 [W/m^2-K] "heat transfer coefficient"
A_s=0.01 [m^2] "surface area"
T_ini=300 [K] "initial temperature"
T_infinity=500 [K] "ambient temperature"
C=100 [J/K] "total heat capacity"
T_final=Temp(duration, h_bar, A_s, T_ini, T_infinity, C, N) "final
temperature"
EXAMPLE - 9

Solve this equation using numerical technique:

x3.5 – 4.5x2 + 3x = 25

First, solve by hand using these relations, basically the Newton’s method:

Again, solve by hand first, and then solve that logic by using EES.

x[1]=10 "guess value"


duplicate i=1,5 "carry out iterations"
f[i]=x[i]^3.5-4.5*x[i]^2+3*x[i]-25 "residual"
J[i]=3.5*x[i]^2.5-4.5*2*x[i]+3 "derivative"
x[i+1]=x[i]-f[i]/J[i] "improved estimate"
end

Now, try two equations, using the same Newton’s method, but by building a Jacobian
matrix.

The final solution should look like this. Do this one on your own. You can solve them
directly, but try and solve by hand first. Then code it in EES as shown in the class.

You might also like