help in writing the code. What am I doing wrong?
My goal is to find the roots of an equation by an iterative method with precision: ξ = 10^-5f(x)
(or ε = 0.00001
).
Equation: 2.056x^43+3x^31+4x^12+x^8-3,478 = 0
.
Code:
# -*- coding: utf-8 -*-
import math
#Definition of function
def phi (x):
return 2.056*(x**43)+3*(x**31)+4*(x**12)+(x**8)-3.478
#Recursive search function
def findRoot (f, x, q, epsilon):
fx=f(x)
#Checking the ending condition
if (1 / (1-q) * abs (fx-x) <epsilon):
print 'Root value', fx
print '1 / (1-q) * abs (fx-x)=', 1 / (1-q) * abs (fx-x)
else:
print 'Current approximation', fx
print '1 / (1-q) * abs (fx-x)=', 1 / (1-q) * abs(fx-x)
findRoot (f, fx, q, epsilon)
findRoot(phi, 0.5, 0.5, 0.00001)
Execution
Current approximation -3.4731171861
1 / (1-q) * abs (fx-x)= 7.94623437221
Current approximation -3.66403074312e+23
1 / (1-q) * abs (fx-x)= 7.32806148624e+23
Traceback (most recent call last):
File "Zavd1f.py", line 17, in <module>
findRoot(phi, 0.5, 0.5, 0.00001)
File "Zavd1f.py", line 16, in findRoot
findRoot (f, fx, q, epsilon)
File "Zavd1f.py", line 16, in findRoot
findRoot (f, fx, q, epsilon)
File "Zavd1f.py", line 8, in findRoot
fx=f(x)
File "Zavd1f.py", line 5, in phi
return 2.056*(x**43)+3*(x**31)+4*(x**12)+(x**8)-3.478
OverflowError: (34, 'Numerical result out of range')