''' Compute real, positive, root 4th order polynomial. x^4 + 3x^3 -2x -6 = 0 by iteration(s). Try the following iterations: x^4 (next) = -3x^3 +2x +6 3x^3 (next) = -x^4 +2x +6 2x (next) = x^4 +3x^3 -6 From someone who plotted this polynomial we know that there is a single positive root somewhere between x=1.2 and x=1.6. Analytical solution x = 2^(1/3) In this second version we format output a bit better & break the iteration when the increment of x drops to 0. (addition gives less than machine accuracy, i.e. all digits of x stabilize) ''' x = 1.4 # staring from a guess print("\n 1st guess x= ",x) for i in range(1,99): # compute the increment (del_x) of x in this iteration # this is the r.h.s. of the iteration equation minus x. del_x = ((-x**4 +2*x +6)/3)**(1/3) - x x = x + del_x print('iter. %d x = %18.16f del_x = %8.3e' %(i,x,del_x)) if (del_x==0.): break # breaks out from the loop when del_x==0. print("analytically obtained, exact res. is = 2**(1/3) =") print(" x = ",2**(1/3)) print('\n') # one new line (empty line) after results