#! usr/bin/env python # -*- coding: utf-8 -*- #nibunhou.py import random import math def f(x): return x*x*x - x +1 a = float(raw_input("set a as f(a)>0 : ")) b = float(raw_input("set b as f(b)<0 : ")) det = math.fabs((a-b)*1.0 / a ) if math.fabs(a)!=math.fabs(b): EPS = 0.000001#収束条件値 while det > EPS: det = math.fabs((a-b)*1.0 / a ) x = float(a + b) / 2 print "x=", x print "f(x)=",f(x) if f(x) > 0: a = x elif f(x) < 0: b = x print "approximate solution= ",x else: print "invalid input"