PLease convert the following vba code to working java code. Option Explicit Sub
ID: 3649737 • Letter: P
Question
PLease convert the following vba code to working java code.Option Explicit
Sub TestBisectMin()
Dim imax As Integer, iter As Integer
Dim x As Double, xl As Double, xu As Double
Dim es As Double, ea As Double, xr As Double
Dim root As Double
'input information from the user
Sheets("Sheet1").Select
Range("b4").Select
xl = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
xu = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
es = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
imax = ActiveCell.Value
Range("b4").Select
If f(xl) * f(xu) < 0 Then
'if the initial guesses are valid, implement bisection
'and display results
root = BisectMin(xl, xu, es, imax, xr, iter, ea)
MsgBox "The root is: " & root
MsgBox "Iterations: " & iter
MsgBox "Estimated error: " & ea
MsgBox "f(xr) = " & f(xr)
Else
'if the initial guesses are invalid,
'display an error message
MsgBox "No sign change between initial guesses"
End If
End Sub
Function BisectMin(xl, xu, es, imax, xr, iter, ea)
Dim xrold As Double, test As Double, fl As Double, fr As Double
iter = 0
fl = f(xl)
Do
xrold = xr
'determine new root estimate
xr = (xl + xu) / 2
fr = f(xr)
iter = iter + 1
'determine approximate error
If xr <> 0 Then
ea = Abs((xr - xrold) / xr) * 100
End If
'determine new bracket
test = fl * fr
If test < 0 Then
xu = xr
ElseIf test > 0 Then
xl = xr
fl = fr
Else
ea = 0
End If
'terminate computation if stopping criterion is met
'or maximum iterations are exceeded
If ea < es Or iter >= imax Then Exit Do
Loop
BisectMin = xr
End Function
Function f(x)
f = x ^ 10 - 1
End Function
Explanation / Answer
Range("E3:H18").Select ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("F4:F18"), _ xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0) ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("F4:F18"), _ xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 255, 0) With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("E3:H18") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.