I've been set a task to iteratively calculate the value of x of the given formula : x3 - 3x + 4 = 0** using trial and error. The code will first plug in -1.5 as its first guess and then work up or down from there to try and get the answer 0. Every time a number is plugged in the equation the code should output "Answer ## is too big/small"
I've started the code but I'm a bit stuck as it only does it twice and then stops. I used a while loop but I don't think I've used it correctly or that it may be the wrong way to go about this problem. Any suggestions/snippets of code will be greatly appreciated.
import cmath
end = ''
num = 0
guess = -1.5
calculation = (guess**3 - guess * 3 + 4)
print(calculation)
while calculation < 0:
print("Guess is too small")
else:
print("Guess is too large")
while calculation != 0 and calculation < 0:
guess = guess + 0.1
calculation = (guess**3 - guess * 3 + 4)
else:
guess = guess-0.1
calculation = ((guess**3)-3*guess+4)
print(calculation)