Databases Reference
In-Depth Information
Consider the following case:
2
x
−−
2 5
x
(4.3)
The roots of this equation are: 3,5
Because (4.3) factors into (x
5)
In Equation 4.3, the parameters for the quadratic equation are a
+
3)(x
=
1, b
=
2, and c
=
15.
If the following two commands are typed:
Debug.Print "Root 1: "; QuadEq(1, 2, 15, 1)
Debug.Print "Root 2: "; QuadEq(1, 2, 15, 2)
The following results are displayed:
Root 1: 5
Root 2: 3
This function works well except in cases where the roots are complex. Consider the case of
x
2
+
4
(4.4)
This function does not factor rationally; its roots are complex. Trying to run the
function
on the function produces an error. The error occurs because, in this case, the function is trying to
take the square root of a negative number, which is considered an illegal operation in VBA.
This function can be modified to have the ability to factor a function with complex roots. To
do so first requires that the function check if the quadratic equation to be factored has complex
roots. This can be easily done by looking at the discriminant of the quadratic Equation 4.2. The
discriminant is
QuadEq
b 2
4 ac
(4.5)
which is the portion of the quadratic equation under the radical sign. If the discriminant is negative,
then the roots to be solved for are complex, and special actions must be taken to factor the function.
In this instance, the roots to be solved for must be split into real and complex portions. Depending
upon which root is to be returned, the real and complex portions of the root must be added or
subtracted from each other and returned as a single complex number with both real and imaginary
portions. To do this, the improved function must return a variant or a string. The function
AdvQuadEq is capable of calculating the roots of quadratic equations even when the roots are
complex.
Function AdvQuadEq(ByVal a As Double, ByVal b As Double, ByVal
c As Double, _
Optional RootNumber As Integer = 1) As Variant
'Solves the Quadratic Equation, User must specify which Root
to Return
'Root 1 = + sqroot term : Root 2 = - sqroot term
Dim discriminant As Double, complex As Boolean
Dim real As Single, cmplx As Single
If RootNumber <> 1 And RootNumber <> 2 Then Exit Function
discriminant = b ^ 2 - 4 * a * c
Search WWH ::




Custom Search