Attribute VB_Name = "Stability_Code"			'to '


Function Sx(d1 As Double, d2 As Double, ma As Double, q As Double)

f1 = ma + 2 * q 	'calculate the potential for first time step
f2 = ma - 2 * q		'calculate the potential for first second step

l = WorksheetFunction.Pi() - d1 - d2 'calculate the time spent in the t2 state

If f1 > 0 Then


a = Cos(d1 * Sqr(f1))
b = (1 / Sqr(f1)) * Sin(d1 * Sqr(f1))		'calculate the matrix elements for the high state of the x-axis
c = -1 * Sqr(f1) * Sin(d1 * Sqr(f1))
d = Cos(d1 * Sqr(f1))

End If

If f1 < 0 Then

a = WorksheetFunction.Cosh(d1 * Sqr(-1 * f1))
b = (1 / Sqr(-1 * f1)) * WorksheetFunction.Sinh(d1 * Sqr(-1 * f1))
c = Sqr(-1 * f1) * WorksheetFunction.Sinh(d1 * Sqr(-1 * f1))
d = WorksheetFunction.Cosh(d1 * Sqr(-1 * f1))

End If

If f2 > 0 Then

e = Cos(d2 * Sqr(f2))
f = (1 / Sqr(f2)) * Sin(d2 * Sqr(f2))
g = -1 * Sqr(f2) * Sin(d2 * Sqr(f2))
h = Cos(d2 * Sqr(f2))

End If

If f2 < 0 Then

e = WorksheetFunction.Cosh(d2 * Sqr(-1 * f2))
f = (1 / Sqr(-1 * f2)) * WorksheetFunction.Sinh(d2 * Sqr(-1 * f2))
g = Sqr(-1 * f2) * WorksheetFunction.Sinh(d2 * Sqr(-1 * f2))			'calculate the matrix elements for the low state of the x-axis
h = WorksheetFunction.Cosh(d2 * Sqr(-1 * f2))

End If


Sx = a * e + b * g + c * f + d * h + a * l * g + c * l * h  		'algebraic representation of the transfer matrix trace'


End Function
