I am looking for a way to find a real number in a set of complex numbers in excel-vba. More specifically I have a set of three results out of which one is known to be real and two are known to be complex, however, I don't know which of the results is the real one. The problem is made more complicated by the fact that due to rounding errors in intermediate calculation steps it often happens that the imaginary component of the real number doesn't cancel out to exactly 0 (as it should).
The method I'm currently considering to use consists of the following steps:
- Determine the value of the Real component of each of the three results.
- Determine the absolute value of the Imaginary component of each of the three results.
- Determine the minimum value of these three results.
- Compare each of the Absolute Imaginary components to the minimum value. When these match, take the corresponding real component as the final result.
The code would look like this:
Z1 = Application.WorksheetFunction.ImReal ( Application.WorksheetFunction.ImSum (xi1, x1i2, x1i3) )
Z2 = Application.WorksheetFunction.ImReal ( Application.WorksheetFunction.ImSum (xi1, x2i2, x2i3) )
Z3 = Application.WorksheetFunction.ImReal ( Application.WorksheetFunction.ImSum (xi1, x3i2, x3i3) )
ZIm1 = Abs ( Application.WorksheetFunction.Imaginary ( Application.WorksheetFunction.ImSum (xi1, x1i2, x1i3) ) )
ZIm2 = Abs ( Application.WorksheetFunction.Imaginary ( Application.WorksheetFunction.ImSum (xi1, x2i2, x2i3) ) )
ZIm3 = Abs ( Application.WorksheetFunction.Imaginary ( Application.WorksheetFunction.ImSum (xi1, x3i2, x3i3) ) )
ZImMin = Min (ZIm1, ZIm2, ZIm3)
If Zim1 = ZImMin Then
ZImID = Z1
ElseIf Zim2 = ZImMin Then
ZImID = Z2
Else ZImID = Z3
EndIf
I think this should work, however, I haven't tried to run it yet. Can anyone suggest a better way to find the Real solution?
This issue is part of finding the solution(s) to a cubic equation according to this method:
Thanks!
ImReal
,ImSum
andImaginary
? – DisplayNameApplication.WorksheetFunction.ImReal(...)
and the likes – DisplayName