ghoshwin
New Member
- Joined
- Dec 10, 2019
- Messages
- 1
- Office Version
- 2019
- 2016
- 2013
- 2010
- Platform
- Windows
I am very new to excel macro. What i need to write a function to find the root of a Cubic function. But the function did not return the proper value. I tried but not able to find out the mistake. It return when H<0 (checked with a=2, b=-4, c=-22, d=34) but not for other cases when H>0 (eg. a=24, b=-4, c=-22, d=34). Please help. The function is as below.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ReturnRoot(a, b, c, d)
'
'
F = ((3 * c / a) - ((b) ^ 2 / (a) ^ 2)) / 3
G = ((2 * (b) ^ 3 / (a) ^ 3) - (9 * b * c / (a) ^ 2) + (27 * d / a)) / 27
H = ((G) ^ 2 / 4) + ((F) ^ 3 / 27)
'
'
If F = 0 And G = 0 And H = 0 Then GoTo A1:
If H <= 0 Then GoTo A2:
If H > 0 Then GoTo A3:
'
'''''''''''''''''''''''''''''''''''''''''''
A1:
X1 = (d / a) ^ (1 / 3) * (-1)
GoTo a:
'''''''''''''''''''''''''''''''''''''''''''
A2:
L = (-1) * (((G) ^ 2 / 4 - H) ^ (1 / 2)) ^ (1 / 3)
M = Cos((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3)
N = (3) ^ (1 / 2) * Sin((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3)
P = (b / (3 * a)) * (-1)
'
'
X1 = 2 * ((((G) ^ 2 / 4 - H) ^ (1 / 2)) ^ (1 / 3)) * Cos((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3) - (b / (3 * a))
GoTo a:
''''''''''''''''''''''''''''''''''''''''''
A3:
R = (-1) * (G / 2) + H ^ (1 / 2)
S = (R) ^ (1 / 3)
T = (-1) * (G / 2) - H ^ (1 / 2)
U = (T) ^ (1 / 3)
'
'
X1 = (S + U) - (b / (3 * a))
GoTo a:
''''''''''''''''''''''''''''''''''''''''''
a:
ReturnRoot = X1
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ReturnRoot(a, b, c, d)
'
'
F = ((3 * c / a) - ((b) ^ 2 / (a) ^ 2)) / 3
G = ((2 * (b) ^ 3 / (a) ^ 3) - (9 * b * c / (a) ^ 2) + (27 * d / a)) / 27
H = ((G) ^ 2 / 4) + ((F) ^ 3 / 27)
'
'
If F = 0 And G = 0 And H = 0 Then GoTo A1:
If H <= 0 Then GoTo A2:
If H > 0 Then GoTo A3:
'
'''''''''''''''''''''''''''''''''''''''''''
A1:
X1 = (d / a) ^ (1 / 3) * (-1)
GoTo a:
'''''''''''''''''''''''''''''''''''''''''''
A2:
L = (-1) * (((G) ^ 2 / 4 - H) ^ (1 / 2)) ^ (1 / 3)
M = Cos((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3)
N = (3) ^ (1 / 2) * Sin((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3)
P = (b / (3 * a)) * (-1)
'
'
X1 = 2 * ((((G) ^ 2 / 4 - H) ^ (1 / 2)) ^ (1 / 3)) * Cos((WorksheetFunction.Acos(-G / (2 * (((G) ^ 2 / 4 - H) ^ (1 / 2))))) / 3) - (b / (3 * a))
GoTo a:
''''''''''''''''''''''''''''''''''''''''''
A3:
R = (-1) * (G / 2) + H ^ (1 / 2)
S = (R) ^ (1 / 3)
T = (-1) * (G / 2) - H ^ (1 / 2)
U = (T) ^ (1 / 3)
'
'
X1 = (S + U) - (b / (3 * a))
GoTo a:
''''''''''''''''''''''''''''''''''''''''''
a:
ReturnRoot = X1
End Function