This is a long one I know but I would really appreciate the help. I am trying to code the Newton Raphson method into VBA, Code shown below:
Code:
'Code illustrating Newton-Raphson scheme for the equation:
' f(x) = arcCos((x-BCos(H))/S)-arcSin((Bsin(H)-y)/S)
Const ep = 1E-23: Const imax = 100
Private x As Long: Private xnew As Single: Private xl As Single
Private xu As Single: Private xm As Single: Private xmold As Single: Private A As Single: Private B As Single
Private C As Single: Private D As Single
Private i As Integer
Private Failed As Boolean: Private Converged As Boolean
Sub Setup()
Failed = False
Converged = False
i = 0
End Sub
Sub NRRoot()
Set sht = Sheets("Sheet1")
For rw = 2 To 3601
x = sht.Cells(rw, 48)
Setup
Do
Dim fx As Single: Dim fprimex As Single
fx = Application.Acos((Range("O9") - Range("AI5") * Cos(x)) / Range("AL5")) - Application.Asin((Range("AI5") * Sin(x) - Range("P9")) / Range("AL5"))
fprimex = -(Range("AI5") * Sin(x) * Range("AL5")) / (Range("AL5") * Sqr((Range("AL5") ^ 2) - (Range("O9") ^ 2) + 2 * Range("O9") * Range("AI5") * Cos(x) - (Range("AI5") ^ 2) * (Cos(x) ^ 2))) - (Range("AI5") * Cos(x) * Range("AL5")) / (Range("AL5") * Sqr((Range("AL5") ^ 2) - (Range("AI5") ^ 2) * (Sin(x) ^ 2) + 2 * Range("P9") * Range("AI5") * Sin(x) - (Range("P9") ^ 2)))
xnew = x - fx / fprimex
Dim er As Single
er = Abs(2 * (xnew - x) / (xnew + x))
If er < ep Then
Converged = True
ElseIf i >= imax Then
Failed = True
Else
i = i + 1
x = xnew
End If
Loop Until Converged Or Failed
If Failed Then
sht.Cells(rw, 50).Value = "Iteration failed"
Else
sht.Cells(rw, 50).Value = xnew
End If
sht.Cells(rw, 51).Value = i
Next
End Sub
Problem:
I am getting the error message: "Run-time error'13': Type Mismatch", and using the debugger it is shown on this line of code:
fx = Application.Acos((Range("O9") - Range("AI5") * Cos(x)) / Range("AL5")) - Application.Asin((Range("AI5") * Sin(x) - Range("P9")) / Range("AL5"))
I think it has something to do with the Application.Acos & Application.Asin, however I am not too sure. I was having troubles with it for a while and I did some searching and found This showing that I have to put Application.Acos or Application.WorksheetFunction. The values being put in are all in radians from -pi to pi.
If it isn't because of the above text, then I think it could have something to do with the parameters that I am defining... right at the top there it says Private x As Long where maybe it has to be something else. I have tried troubleshooting, but it never really worked :(
The values in cells O9, P9, AI5, and AL5 are listed respectively: 2000, 3000, 5700, 2924.99
P.S. The reason why I am needing to use this method is because I am trying to calculate the angles of 2 sticks when given a certain point x,y (O9,P9). I need these angles to be able to calculate the center of mass of the two sticks. Once I have the center of mass I can then finish my calculations for the project I am doing. I know there are other (much better) methods to do this problem, like wolfram mathematica, however there are other parts to the project that need to be on excel. Therefore to run everything as smooth as possible, sadly, I need to do all of this on excel.
P.P.S. This is not my code by the way, I copied it from Here, however I think it does actually solve the Newton Raphson Method.
Solution
I had the numbers for arcSin starting at pi and going to -pi instead of 90 going to -90...
If I can figure out a better way to program the Newton Raphson Method, I will be sure to make a new post about it.
xof typeLongand not a floating point number? - Nico Schertlersinglebut I was troubleshooting and I guess never changed it back. I will try making x of another type. What is the equivalent tofloat? Do you know? - JakeKDim B As Single : B = Range(...). This will make your code cleaner, anyway. - Nico Schertler