def method(fx,xi,xf,tol,iter,e = 000): xi = float(xi) xf = float(xf) tol = float(tol) yi = eval(fx,xi) yf = eval(fx,xf) xis = [] xfs = [] xms = [] yms = [] errors = [] if(yi*yf>0): return {'status': "FAIL", 'message': "The interval is not valid"} elif (yi==0): return {'status': "FAIL", 'message': "The lower limit "+str(xi)+" is a root"} elif (yf==0): return {'status': "FAIL", 'message': "The lower limit "+str(xf)+" is a root"} else: error = tol*2 con = 1 ym = 1 xm = (xi+xf)/2 while(ym!=0 and error>tol and con<=int(iter)): if(con>1): xm = nxm ym = float(eval(fx,xm)) xxi = xi xxf = xf if(ym*yi>0): xi = xm else: xf = xm nxm = float((xi+xf)/2) if(e==1): error = abs((nxm-xm)/nxm) else: error = abs(nxm-xm) xis.append(xxi) xfs.append(xxf) xms.append(xm) yms.append(ym) errors.append(error) con += 1 table = {'iter': con-1, 'xi' : xis, 'xf': xfs, 'ym':yms,'error':errors} if(ym == 0): return {'status': "SUCESS", 'message': str(xm)+" is a root with a error of " + str(error), 'xm' : xm, 'error':error,'table':table, 'stopBy':'xm'} elif(error<tol): return {'status': "SUCESS", 'message': str(xm)+" is a root with a error of " + str(error), 'xm' : xm, 'error':error, 'table':table, 'stopBy':'tol'} else: return {'status': "SUCESS", 'message': str(xm)+" is a possible root with a error of " + str(error), 'xm' : xm, 'error':error, 'table':table, 'stopBy':'iter'}
def method(tolerance,inferior_limit,superior_limit,iterations,function,e): function_0=float(eval(function,inferior_limit)) function_1=float(eval(function,superior_limit)) if(function_0==0): return {'status': "FAIL", 'message': "The lower limit "+str(inferior_limit)+" is a root"} else: counter=0 den=function_1-function_0 error=tolerance+1 errors=[] xns = [] function1s = [] while error>tolerance and function_1!=0 and den!=0 and counter < iterations: x_n= superior_limit-function_1*((superior_limit-inferior_limit)/den) if(e==1): error=abs((x_n-superior_limit)/x_n) else: error=abs(x_n-superior_limit) inferior_limit=superior_limit function_0=function_1 superior_limit=x_n function_1=eval(function,superior_limit) den=function_1-function_0 errors.append(error) xns.append(x_n) function1s.append(function_1) counter=counter+1 table = {'iter': counter-1, 'xi' : inferior_limit, 'xf': superior_limit, 'xn':xns,'error':errors} if (function_1 == 0): return {'status': "SUCESS", 'message': str(superior_limit) + " is a root with a error of " + str(error), 'xn': x_n, 'error': error, 'table': table, 'stopBy': 'xm'} elif (error < tolerance): return {'status': "SUCESS", 'message': str(x_n) + " is a root with a error of " + str(error), 'xn': x_n, 'error': error, 'table': table, 'stopBy': 'tol'} elif (den==0): return {'status': "FAIL", 'message':'There is a possible multiple root', 'error':error,'table':table, 'stopBy':'derivate'} else: return {'status': "SUCESS", 'message': str(x_n) + " is a possible root with a error of " + str(error), 'x_n': x_n, 'error': error, 'table': table, 'stopBy': 'iter'}
def method(tolerance,xapproximate,iterations,function,derivative_function,e): function_evaluated=float(eval(function,xapproximate)) derivative_evaluated= float(eval(derivative_function,xapproximate)) counter=0 error=tolerance+1 xns=[] errors = [] fns = [] dfns = [] while error>tolerance and function_evaluated!=0 and derivative_function!=0 and counter<iterations: x_n = xapproximate-(function_evaluated/derivative_evaluated) f_n=float(eval(function,x_n)) d_f_n=float(eval(derivative_function,x_n)) function_evaluated=f_n derivative_evaluated=d_f_n if(e==1): error=abs((x_n-xapproximate)/x_n) else: error=abs(x_n-xapproximate) xapproximate=x_n errors.append(error) xns.append(x_n) fns.append(f_n) dfns.append(d_f_n) counter=counter+1 table = {'iter': counter-1, 'xns' : xns, 'fns': fns, 'dfns':dfns,'error':errors} if(function_evaluated == 0): return {'status': "SUCESS", 'message': str(xapproximate)+" is a root with a error of " + str(error), 'xn' : xapproximate, 'error':error,'table':table, 'stopBy':'xm'} elif(error<tolerance): return {'status': "SUCESS", 'message': str(xapproximate)+" is a root with a error of " + str(error), 'xn' : xapproximate, 'error':error, 'table':table, 'stopBy':'tol'} elif(derivative_evaluated==0): return {'status': "FAIL", 'message': str(xapproximate)+" is a root with a possible multiple root", 'xn' : xapproximate, 'error':error,'table':table, 'stopBy':'derivate'} else: return {'status': "SUCESS", 'message': str(xapproximate)+" is a possible root with a error of " + str(error), 'xn' : xapproximate, 'error':error, 'table':table, 'stopBy':'iter'}
def method(fx,delta,xi): yis = [] yyis = [] xi = float(xi) delta = float(delta) yi = eval(fx,xi) yis.append(yi) xxi = xi+ delta yyi = eval(fx,xxi) yyis.append(yyi) c=0 while(int(yi*yyi)>0): xi = xi+delta yi = eval(fx,xi) yis.append(yi) xxi = xi+ delta yyi = eval(fx,xxi) yyis.append(yyi) c += 1 if c<=10: print str(xi) + "\n" yis.append(yyis[len(yyis)-1]) return {'status': 'SUCCES', 'messaje':'There is a root in the interval ['+ str(xi)+ ' , '+str(xxi)+']', 'xi':str(xi), 'xf':str(xxi), 'list':yis }