x^3+8x^2+18x+7=0
Solve for x
#Let's implement sentences numerical method in python
a0 = int(input('Enter the left spacing value: '))
a1 = int(input('Enter the right span value: '))
solve = [a0,a1]
i = True
while i:
x = (solve[0] + solve[1]) / 2
f = x**3 + 8*x**2 + 18*x + 7
if f > 0:
solve[1] = x
print(solve)
if f < 0:
solve[0] = x
print(solve)
if f == 0:
print(x)
i = False
if abs(solve[0] - solve[1]) <= 0.001:
i = False
print(f'{solve}')
#Output:
#Enter the left spacing value: -1000
#Enter the right span value: 1000
#[-1000, 0.0]
#[-500.0, 0.0]
#[-250.0, 0.0]
#[-125.0, 0.0]
#[-62.5, 0.0]
#[-31.25, 0.0]
#[-15.625, 0.0]
#[-7.8125, 0.0]
#[-3.90625, 0.0]
#[-1.953125, 0.0]
#[-0.9765625, 0.0]
#[-0.9765625, -0.48828125]
#[-0.732421875, -0.48828125]
#[-0.6103515625, -0.48828125]
#[-0.54931640625, -0.48828125]
#[-0.518798828125, -0.48828125]
#[-0.5035400390625, -0.48828125]
#[-0.49591064453125, -0.48828125]
#[-0.492095947265625, -0.48828125]
#[-0.4901885986328125, -0.48828125]
#[-0.48923492431640625, -0.48828125]
#[-0.48923492431640625, -0.48828125]
#Answer: take half of our segment -0.488−0.488
Comments
Leave a comment