You are on page 1of 2

Module2 - 1

Sub ModifiedFP()

'setting all values used as double etc


Dim Q As Double
Dim w As Double
Dim theta As Double
Dim xl As Double
Dim xh As Double
Dim tol As Double
Dim iter As Integer
Dim g As Double
Dim maxiter As Double
Dim xr As Double

'calling in everything used


Q = Worksheets("sheet1").Range("Q")
w = Worksheets("sheet1").Range("w")
theta = Worksheets("sheet1").Range("theta")
xl = Worksheets("sheet1").Range("Hl")
xh = Worksheets("sheet1").Range("Hh")
tol = Worksheets("sheet1").Range("tol")
g = Worksheets("sheet1").Range("g")
maxiter = Worksheets("sheet1").Range("maxiter")

'setting some initial conditions


iter = 0
ea = 1
il = 0
iu = 0

'need to be found once to determine xr before it loops


fl = f(Q, w, xl, theta, g)
fh = f(Q, w, xh, theta, g)

xr = (xh * fl - xl * fh) / (fl - fh)

Do

If iter > maxiter Or ea < tol Then Exit Do


iter = iter + 1
xold = xr

fl = f(Q, w, xl, theta, g)


fh = f(Q, w, xr, theta, g)
test = fl * fh

If test < 0 Then


xh = xr
iu = 0 'test for 2x same value stuff
il = il + 1
If il >= 2 Then fl = fl / 2
ElseIf test = 0 Then
xr = xr
Else
xl = xr
iu = iu + 1 'test for 2x same value stuff
il = 0
If iu >= 2 Then fh = fh / 2
End If

'must recalculate xr using new values for next loop and solving error
xr = (xh * fl - xl * fh) / (fl - fh)

ea = Abs((xr - xold) / xr)

Loop

Worksheets("Sheet1").Cells(14, 14) = xr
Worksheets("Sheet1").Cells(15, 14) = iter
Worksheets("Sheet1").Cells(16, 14) = ea

End Sub
Module2 - 2

Function f(Q As Double, w As Double, y As Double, theta As Double, g As Double) As Double

b = w + 2 * y / Tan(theta * WorksheetFunction.Pi() / 180)

a = (b + w) / 2 * y

f = 1 - Q ^ 2 * b / (g * a ^ 3)

End Function

You might also like