You are on page 1of 5

CIVL2050 - Assignment 5

Georgia Carniel 3162942

Part a)

VB coding:

Option Explicit On
Imports System.Math
Imports System
Imports System.IO
Imports System.Text

Module Slide_inearthquake

Sub Main()
'Define variables
Dim nSlide, nRep As Integer
Dim Fmean, Fcov, Fstddev, g, H, Tweight As Double
Dim A, err, conflim As Double
Dim Tri, F, coeffF As Double
Dim probSlide As Double

'calcuate parameters of friction coefficient from given data

Fmean = 0.5
Fcov = 0.1
Fstddev = Fcov * Fmean 'Cov=stddev/mean
g = 9.8 'gravity
Tweight = 1800 'tank weight
err = 0.001

'begin monte carlo simulation


Do
nRep = nRep + 1

'calculate frictionel force

coeffF = normal(Fmean, Fstddev)


F = Tweight * coeffF

'sample inputs from probability distribution


'as the area under the PDF is equal to one we can find A

Tri = Rnd()
A = 14.715 - Sqrt((1 - Tri) / 0.007216)

'Calculate Horizontal inertial force, H


H = (Tweight / g) * A

'Determine if tank slides

If H > F Then
nSlide = nSlide + 1
End If

'calculate probability of sliding every 100 replicates

If nRep Mod 100 = 0 Then


probSlide = CDbl(nSlide) / CDbl(nRep)
conflim = 2.33 * (Sqrt((1 - probSlide) * probSlide / CDbl(nRep))) 'for
99% accuracy
If conflim < err Then 'if confidence limit is less then error
Exit Do
End If
End If

Loop

'Write output, i.e probability of failure


Console.WriteLine("Probability of tank sliding=" & Format(probSlide))
Console.WriteLine("with 99% confidence=" & Format(conflim))
Console.WriteLine("for replicates=" & nRep)
Console.ReadKey()

End Sub

End Module

Output Console Application:


Part a)
VB Coding:
Option Explicit On
Imports System.Math
Imports System
Imports System.IO
Imports System.Text

Module Resevoir

Sub Main()
'Declare Variables
Dim S, C, meanQ, stddevQ, covQ As Double
Dim lambdaQ, zetaQ, Q As Double 'Q is resevoir inflow
Dim err, prob, actualtransfer, demand As Double
Dim nRep As Long
Dim nShortage, wStorage, totaltransfer As Double
Dim pShortage, conflim, percent As Double
Dim transferyear, pReliability As Double

'initialise Variables
C = 100 'Capacity
meanQ = 50
stddevQ = 20
covQ = stddevQ / meanQ 'Calculation for CoV
err = 0.001
S = 100 'assume volume of resevoir is at capcity

'Test out different capacities using a loop


Do

nRep = nRep + 1 'initialise counter

'Get log-Normal parameters

zetaQ = Sqrt(Log(1 + (covQ) ^ 2))


lambdaQ = Log(meanQ) - (0.5 * (zetaQ) ^ 2)

Q = Exp(normal(lambdaQ, zetaQ)) 'Check for wet year

prob = Rnd() 'random numers 0 to 1


percent = Rnd()
'sample demand
If prob < 0.96 Then
demand = 40 ' 96% of the time demand is 40ML
Else
demand = 50 ' 4% of the time the demand in 50ML
End If

'Decide if there can be a transfer

If Q > meanQ Then 'wet year


If percent > 0.5 Then
actualtransfer = 0
Else

actualtransfer = 7.8996 'Desired value found in Excell


End If
Else
actualtransfer = 7.8996
End If

'perform water balance for resevoir

totaltransfer = totaltransfer + actualtransfer 'counts the total


amount of water sold

wStorage = S + Q - demand - actualtransfer ' calculate the water


supply
If wStorage <= 0 Then
nShortage = nShortage + 1 'counter
S = 0 'resets to 0 if there is
shortage
Else
If wStorage > C Then 'storage volume can not
exceed capacity
S = C
Else
S = wStorage 'resets for next loop
End If
End If

'Determine if desired accuracy is obtained


If nRep Mod 100 = 0 Then 'calculate probability of sliding every 100
replicates
pShortage = CDbl(nShortage) / CDbl(nRep)
pReliability = 1 - pShortage 'calculate probability of it
being reliable (%)
conflim = 2.33 * (Sqrt((1 - pShortage) * pShortage / CDbl(nRep))) '
99% certainty

'Exit loop if desired accuracy is obtained

If conflim < err Then

Exit Do
End If
End If
Loop

transferyear = totaltransfer / nRep 'amout transfered in one year

'Outputs:

Console.WriteLine("probability of water shortage = " & pShortage)


Console.WriteLine("with 99% reliability = " & conflim)
Console.WriteLine("replicates=" & nRep)
Console.WriteLine("annual income = " & transferyear * 500)
Console.WriteLine("percentage of reliabitity = " & pReliability)
Console.ReadKey()

End Sub

End Module
Excel Interpolation:
Percentage of
water transferred
Shortage
7 3.5492
7.1 3.67
7.2 3.7873
7.3 3.9647
7.4 4.1248
7.5 4.2947
7.6 4.4518
7.7 4.664
7.8 4.8401
7.9 5.022
8 5.1977

Water Transferred vs. Percentage of


shortage
8.2
8
f(x) = 0.59129495737476 x + 4.94311696991136
Water Transferred ML

7.8 R² = 0.996305126132952
7.6
Series2
7.4
Linear (Series2)
7.2
7
6.8
6.6
6.4
3.4 3.6 3.8 4 4.2 4.4 4.6 4.8 5 5.2 5.4
Percentage(%)

Output:

You might also like