Answer the question
In order to leave comments, you need to log in
I rewrote the code to BASIC from python, why does it work incorrectly?
I have a python code
def f(x):
return (100-2*x)*(75-2*x)*x
eps = 0.00001
l = 0
r = 75/2
while r-l > eps:
m = (r+l)/2
if (f(l)-f(l+eps)) * (f(m)-f(m+eps)) > 0:
l = m
else:
r = m
print((r+l)/2)
Function f(ByVal x As Single) As Single
f = (100 - x * 2) * (75 - x * 2) * x
End Function
Sub program()
Dim l As Single, m As Single, eps As Single, r As Single
eps = 0.00001
l = 0
r = 75 / 2
Do While r - l > eps
m = (r + l) / 2
If (f(l) - f(l + eps)) * (f(m) - f(m + eps)) > 0 Then
l = m
Else
r = m
End If
Loop
Cells(1, 1).Value = (r + l) / 2
End Sub
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question