xm = nxm ym = float(parser.eval(fx,xm)) xxi = xi xxf = xf if(ym*yi>0): xi = xm yi = ym else: xf = xm yf = ym nxm = float((xi+xf)/2) if(e==1): error = abs((nxm-xm)/nxm) else: error = abs(nxm-xm) v = (con,xxi,xxf,xm,ym,error) rows.append(v) con += 1 t = Table(rows=rows, names=('iter', 'xi', 'xf', 'xm','f(xm)','error')) print(t) print("") plot.graficar(fx) if(ym == 0): print str(xm)+" is a root with a error of " + str(error) elif(error<tol): print str(xm)+" is a root with a error of " + str(error) print "The method has stoped because the error is < tolerance" else: print "The method has exceeded the number maximum of iterations"
import math import plot def cuadrado(x): return x * x plot.dibujar_ejes() plot.color('red') plot.graficar(math.sin) plot.color('blue') plot.graficar(math.exp) plot.color('green') plot.graficar(cuadrado) raw_input()
derivative_function=raw_input("Type the derivative of the function \n") function_evaluated=float(parser.eval(function,xapproximate)) derivative_evaluated= float(parser.eval(derivative_function,xapproximate)) counter=0 error=tolerance+1 rows=[] while error>tolerance and function_evaluated!=0 and derivative_function!=0 and counter<iterations: x_n = xapproximate-(function_evaluated/derivative_evaluated) f_n=float(parser.eval(function,x_n)) d_f_n=float(parser.eval(derivative_function,x_n)) function_evaluated=f_n derivative_evaluated=d_f_n error=abs(x_n-xapproximate) xapproximate=x_n v=(counter,x_n,f_n,d_f_n,error) rows.append(v) counter=counter+1 t = Table(rows=rows, names=('iter', 'x_n', 'f_x_n','f_d_x_n','error')) if function_evaluated==0: print str(xapproximate)+"is a root" elif error<tolerance: str(x_n)+ "is an approximation to a root with a tolerance of"+str(tolerance) elif derivative_evaluated==0: str(x_n)+ "is a posible multiple root" else: "Fail after"+str(iterations)+"iteraciones" print t plot.graficar(function)