#! /usr/local/bin/python3.6"""Nonlinear equation with Newton method"""importsysimporttracebackclassNonlinearEquationNewton:EPS=1e-08# Precision of truncationLIMIT=50# Number of truncationdef__init__(self):self.f=lambdax:x**3-x+1self.g=lambdax:3*x**2-1defcompute(self):""" Computation of nonlinear equation with bisection method """try:x=-2.0cnt_loop=0forkinrange(1,self.LIMIT+1):cnt_loop=kdx=xx-=self.f(x)/self.g(x)ifabs(x-dx)/abs(dx)<self.EPS:print("x = {:f}".format(x))breakifcnt_loop==self.LIMIT:print("収束しない")exceptExceptionase:raiseif__name__=='__main__':try:obj=NonlinearEquationNewton()obj.compute()exceptExceptionase:traceback.print_exc()sys.exit(1)