Hi,
I'm trying to code some coding in VBA in order to solve a non-linear equation
Below are my code and I have tried for hours and seems no luck for me. Can anyone guide me please?
What I'm trying to do was, input a range of number like, 1.000 to 2.000 which is equal to x, and the code guess the numbers between 1.000 to 2.000 which f(x)<=0.001 and f(x)>=-0.001, and show the x out in excel which are the roots of the equation.
The roots are
1.75 (range at x=0 to 2)
3.69 (range at x=2 to 4)
-3.69 (range at x=-4 to -2)
-1.75 (range at x=-2 to 0)
What I'm going to input in excel was =optima(0,2) and i expect the excel to show up 1.75, but it hangs there for more than 5 minutes and I just end the excel and restarting it.
Any advise?
I'm trying to code some coding in VBA in order to solve a non-linear equation
Below are my code and I have tried for hours and seems no luck for me. Can anyone guide me please?
What I'm trying to do was, input a range of number like, 1.000 to 2.000 which is equal to x, and the code guess the numbers between 1.000 to 2.000 which f(x)<=0.001 and f(x)>=-0.001, and show the x out in excel which are the roots of the equation.
The roots are
1.75 (range at x=0 to 2)
3.69 (range at x=2 to 4)
-3.69 (range at x=-4 to -2)
-1.75 (range at x=-2 to 0)
What I'm going to input in excel was =optima(0,2) and i expect the excel to show up 1.75, but it hangs there for more than 5 minutes and I just end the excel and restarting it.
Any advise?
Function optima(low As Double, up As Double)
Dim a As Double
Dim b As Double
Dim c As Double
Dim y As Integer
Dim x As Double
Dim i_m_very_tired As Double
'force entering the loop
y = 0
'loop
While y = 0 And x <= up
x = low
'formula
a = 3 * Application.WorksheetFunction.Power(x, 2)
b = 10 / Application.WorksheetFunction.Power(x, 2)
c = 2 * WorksheetFunction.Cosh(x)
i_m_very_tired = a - b - c
'condition if the answer is less than 0.001 and more than -0.01, exit the loop
If i_m_very_tired < 0.01 And i_m_very_tired > -0.01 Then
y = 1
'else, continue the loop with increasing x
Else
x = x + 0.01
End If
Wend
optima = x