Вы находитесь на странице: 1из 10

VBA

IN CLASS 1
Function enthalpy(T, xCO2, xH2O, xO2, xN2)
Dim a(4), b(4), c(4), d(4), ent(4)

a(1) = 36.11
b(1) = 4.233 * 10 ^ (-2)
c(1) = -2.887 * 10 ^ (-5)
d(1) = 7.464 * 10 ^ (-9)

a(2) = 33.46
b(2) = 6.88 * 10 ^ (-3)
c(2) = 7.604 * 10 ^ (-6)
d(2) = 3.593 * 10 ^ (-9)

a(3) = 29.1
b(3) = 1.158 * 10 ^ (-2)
c(3) = -6.076 * 10 ^ (-6)
d(3) = 1.311 * 10 ^ (-9)

a(4) = 29
b(4) = 2.199 * 10 ^ (-3)
c(4) = 5.723 * 10 ^ (-6)
d(4) = -2.787 * 10 ^ (-9)

For i = 1 To 4
ent(i) = a(i) * T + b(i) * (T ^ 2) / 2 + c(i) * (T ^ 3) / 3 + d(i) * (T ^ 4) / 4

Next i
enthalpy = ent(1) * xCO2 + ent(2) * xH2O + ent(3) * xO2 + ent(4) * xN2


End Function

Option Explicit
Function BisectionX(Lo, Up, xCO2, xH2O, xO2, xN2, tolx)
Dim Mi, c1, c2, c3, i, n

n = Abs(Up - Lo) / tolx
'Err = Abs(Up - Lo)

For i = 1 To n
Mi = (Lo + Up) / 2:
c1 = ff(Lo, xCO2, xH2O, xO2, xN2)
c2 = ff(Up, xCO2, xH2O, xO2, xN2)
c3 = ff(Mi, xCO2, xH2O, xO2, xN2)

If c1 * c3 < 0 Then
Up = Mi
Else
Lo = Mi
End If

'Err = Up - Lo

Next i

BisectionX = Mi

End Function
Function ff(T, xCO2, xH2O, xO2, xN2)
ff = enthalpy(T, xCO2, xH2O, xO2, xN2) - enthalpy(25, xCO2, xH2O, xO2, xN2) - 827000
End Function

IN CLASS 2
Option Explicit
Option Base 0
Function dabcdt(A0, B0, C0, k1, k2, dt, tmax)
Dim matrix(1000, 4), i, n, dadt, dbdt, dcdt
n = tmax / dt
matrix(0, 0) = 0: matrix(0, 1) = A0: matrix(0, 2) = B0: matrix(0, 3) = C0:
For i = 1 To n
matrix(i, 0) = matrix(i - 1, 0) + dt

dadt = -k1 * matrix(i - 1, 1)
dbdt = k1 * matrix(i - 1, 1) - k2 * matrix(i - 1, 2)
dcdt = k2 * matrix(i - 1, 2)
matrix(i, 1) = matrix(i - 1, 1) + dadt * dt
matrix(i, 2) = matrix(i - 1, 2) + dbdt * dt
matrix(i, 3) = matrix(i - 1, 3) + dcdt * dt

Next i
dabcdt = matrix
End Function

Function newfunction(A0, B0, C0, k1, k2, dt, tmax)
Dim matrix(1000, 4), i, n, dadt, dbdt, dcdt, finalConc
n = Round(tmax / dt, 0)
matrix(0, 0) = 0: matrix(0, 1) = A0: matrix(0, 2) = B0: matrix(0, 3) = C0:
For i = 1 To n
matrix(i, 0) = matrix(i - 1, 0) + dt

dadt = -k1 * matrix(i - 1, 1)
dbdt = k1 * matrix(i - 1, 1) - k2 * matrix(i - 1, 2)
dcdt = k2 * matrix(i - 1, 2)
matrix(i, 1) = matrix(i - 1, 1) + dadt * dt
matrix(i, 2) = matrix(i - 1, 2) + dbdt * dt
matrix(i, 3) = matrix(i - 1, 3) + dcdt * dt

Next i
newfunction = matrix(n, 2)
End Function

IN CLASS 3
Option Explicit
Option Base 1 'if 0 then the answers vector start from 0!
Function GS(A, b, Cguess, tol)
Dim err(10), i, row, col, n, sum, Cin(10), cnew(10), MaxErr, k
MaxErr = 1: k = 0
n = A.Rows.Count 'number of rows in matrix a

For row = 1 To n
Cin(row) = Cguess(row) 'the inputs are used as the initial values for 1st iterations
Next row

While MaxErr > tol And k < 345 'k is the number of iterations, 15 is max. Do we need more than
15????
k = k + 1: MaxErr = 0 'increase k after each iteration
For row = 1 To n
sum = 0 'reset sum. If we don't have this line, what would happen??????
For col = 1 To n
sum = sum + A(row, col) * Cin(col)
Next col

cnew(row) = (b(row) - sum + Cin(row) * A(row, row)) / A(row, row) 'calculate Cnew. Look up
lecture notes
err(row) = Abs(cnew(row) - Cin(row)) 'there will be n errors, the code stops when ALL errors <
tol, or sum of all errors < tol....
Cin(row) = cnew(row) ' updating values, cin(1,2,3,4) will be the input at start of next loop
If err(row) > MaxErr Then
MaxErr = err(row) 'change MaxErr if it gets bigger/ Or you can just add the 4 errors
together....
End If

Next row
Debug.Print k, Cin(1), Cin(2), Cin(3), Cin(4), MaxErr 'these will be printed out in immediate window
Wend

Cin(n + 1) = k
Cin(n + 2) = MaxErr

GS = Cin 'we will have k, 4 concs, and MaxErr in a horizontal line in Excel....

End Function
Function GSwithL(A, b, Cguess, tol, lamda)
Dim err(10), i, row, col, n, sum, Cin(10), cnew(10), MaxErr, k
MaxErr = 1: k = 0
n = A.Rows.Count 'number of rows in matrix a

For row = 1 To n
Cin(row) = Cguess(row) 'the inputs are used as the initial values for 1st iterations
Next row

While MaxErr > tol And k < 345 'k is the number of iterations, 15 is max. Do we need more than
15????
k = k + 1: MaxErr = 0 'increase k after each iteration
For row = 1 To n
sum = 0 'reset sum. If we don't have this line, what would happen??????
For col = 1 To n
sum = sum + A(row, col) * Cin(col)
Next col

cnew(row) = (b(row) - sum + Cin(row) * A(row, row)) / A(row, row) 'calculate Cnew. Look up
lecture notes
err(row) = Abs(cnew(row) - Cin(row)) 'there will be n errors, the code stops when ALL errors <
tol, or sum of all errors < tol....
'Cin(row) = cnew(row) ' updating values, cin(1,2,3,4) will be the input at start of next loop
Cin(row) = lamda * cnew(row) + (1 - lamda) * Cin(row)
If err(row) > MaxErr Then
MaxErr = err(row) 'change MaxErr if it gets bigger/ Or you can just add the 4 errors
together....
End If

Next row
Debug.Print k, Cin(1), Cin(2), Cin(3), Cin(4), MaxErr 'these will be printed out in immediate window
Wend

Cin(n + 1) = k
Cin(n + 2) = MaxErr

GSwithL = Cin 'we will have k, 4 concs, and MaxErr in a horizontal line in Excel....

End Function

Option Explicit
Option Base 0
Function Euler(xn, x0, y0, h)
Dim n, i ', x(1000), y(1000)
n = (xn - x0) / h:
For i = 1 To n
y0 = y0 + fxy(x0, y0) * h
x0 = x0 + h
Debug.Print i, x0, y0
Next i

Euler = y0
End Function
Function Euler2(xn, x0, y0, h) ' this is based on the dabdt in previous week.....
Dim n, i, t(1000), A(1000)
n = (xn - x0) / h:
t(0) = x0: A(0) = y0
For i = 1 To n
t(i) = t(i - 1) + h
A(i) = A(i - 1) + fxy(t(i - 1), A(i - 1)) * h
Next i

Euler2 = A(n)
End Function
Function Runge_Kutta(xn, x0, y0, h)
Dim n, i
Dim k1, k2, k3, k4

n = xn / h
For i = 1 To n
k1 = h * fxy(x0, y0)
k2 = h * fxy(x0 + h / 2, y0 + k1 / 2)
k3 = h * fxy(x0 + h / 2, y0 + k2 / 2)
k4 = h * fxy(x0 + h, y0 + k3)

y0 = y0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6
x0 = x0 + h

Next i
Runge_Kutta = y0
End Function
Function fxy(x, y)
fxy = y + Exp(x)
End Function

IN CLASS 4
Option Explicit

Function Factorial(dblNumber As Double) As Double
Dim dblCtr As Double
Dim dblResult As Double
dblResult = 1
For dblCtr = 1 To dblNumber
dblResult = dblResult * dblCtr
Next dblCtr
Factorial = dblResult

End Function


Function ArcSin(x)
Dim i, Sol, a, b, y
y = x
For i = 1 To 40
a = Factorial(2 * i)
b = Factorial(1 * i)
Sol = Sol + ((a) * x ^ (2 * i + 1)) / ((4 ^ i) * (b ^ 2) * (2 * i + 1))

Next
ArcSin = y + Sol
End Function

Вам также может понравиться