def newton(x0, tol, iteration): if (iteration < 0 or tol <= 0): eprint("the iterations and tolerance must be positive") else: fx = f(x0, funcion) dfx = f(x0, derivada) cont = 0 error = tol + 1 print "-----------||------------||-------------||--------------" print " X f(x) f'(x) Error " print x0, " | ", fx, " | ", dfx, " | ", error, " | " #If the stop parameters haven't been broken here we can calculate where is the root while (error > tol and fx != 0 and dfx != 0 and cont < iteration): x1 = x0 - fx / dfx fx = f(x1, funcion) dfx = f(x1, derivada) error = abs(x1 - x0) x0 = x1 cont = cont + 1 print x0, " | ", fx, " | ", dfx, " | ", error, " | " #Here we show the root or the aproximation if the given point's not a root if (fx == 0): print(x0, "is root") elif (error < tol): print(x0, " is an aproximation to a root with tolerance ", tol) elif (dfx == 0): print(x0, " maybe a multiple root") else: eprint("failed in the ", iteration, " iteration")
def puntoFijo(Xm, tolerancia, iteraciones): if (iteraciones < 0 or tolerancia < 0): eprint("el numero de iteraciones y tolerancia deben de ser positivos") else: print "-----------||------------||-------------||--------------" print " X G(x) f(x) Error " FXm = f(Xm, funcion) print Xm, " | | ", FXm, " | | " cont = 0 error = tolerancia + 1 #If the stop parameters haven't been broken here we can calculate where is the root while (FXm != 0 and cont <= iteraciones and error > tolerancia): GXm = f(Xm, funcionG) FXm = f(Xm, funcion) error = (GXm - Xm) / GXm Xm = GXm cont += 1 print Xm, " | ", GXm, " | ", FXm, " | ", error, " | " #Here we show the root or the aproximation if the given point's not a root if (FXm == 0): print(Xm, "es una raiz") elif (error <= tolerancia): print("la raiz obtenida con un grado de tolerancia", tolerancia, " de error es", Xm) else: eprint("El calculo excedio el numero de iteraciones esperadas", iteraciones)
def incrementalSearch(x0, delta, iter): if (delta == 0): eprint("Delta no puede ser cero") if (iter <= 0): eprint("Las iteraciones no pueden cer menores o iguales a cero") fx0 = f(x0, funcion) if (fx0 == 0): print(x0 + " es raiz") else: x1 = x0 + delta contador = 1 fx1 = f(x1, funcion) print "-----------||------------" print " X f(x) " print x0, " | ", fx0, " | " print x1, " | ", fx1, " | " #If the stop parameters haven't been broken here we can calculate the interval while (fx0 * fx1 > 0 and contador <= iter): x0 = x1 fx0 = fx1 x1 = x0 + delta fx1 = f(x1, funcion) contador += 1 print x1, " | ", fx1, " | " #Here we can show the interval that has a posible root if (fx1 == 0): print(x1, " es la raiz") elif (fx0 * fx1 < 0): print("Hay raiz entre ", x0, " y ", x1) else: eprint("Fracaso en ", iter, " numero iteraciones")
def biseccion(Xinferior, Xsuperior, tolerancia, iteraciones): if(tolerancia <0 or iteraciones < 0) : eprint("La tolerancia o las iteraciones no pueden ser menores que cero") else: funcionXinferior = f(Xinferior,funcion) funcionXsuperior = f(Xsuperior,funcion) if (funcionXinferior == 0) : print(Xinferior," es raiz.") elif(funcionXsuperior == 0): print(Xsuperior," es raiz.") elif(funcionXinferior*funcionXsuperior < 0): xm = (Xinferior+Xsuperior)/2 funcionXm = f(xm,funcion) contador = 1 errorAbs = tolerancia+1 print "-------------||------------||-------------||--------------||------------" print " Xinferior Xsuperior Xmedio f(Xmedio) Error " print Xinferior ," | ", Xsuperior," | " , xm," | ", funcionXm," | ", errorAbs," | " #If the stop parameters haven't been broken here we can calculate where is the root while(errorAbs > tolerancia and funcionXm !=0 and contador <= iteraciones) : if(funcionXinferior * funcionXm < 0) : Xsuperior = xm funcionXsuperior = funcionXm else: Xinferior = xm funcionXinferior = funcionXm xaux = xm xm = (Xinferior + Xsuperior)/2 funcionXm = f(xm,funcion) errorAbs = abs(xm - xaux) contador = contador + 1 print Xinferior ," | ", Xsuperior," | " , xm," | ", funcionXm," | ", errorAbs," | " #Here we show the root or the aproximation if is not an exact root between the interval if(funcionXm == 0): print ("Xm es raiz.","%.50f"%xm) elif(errorAbs < tolerancia): print (xm, "es aproximacion a una raiz con tolerancia de:", tolerancia) else: eprint ("Fracaso en iteraciones ", iteraciones) else: eprint ("Intervalo inadecuado")
def raicesMultiples(Xo, tol, nIter): fx = f(Xo, funcion) dfx = f(Xo, derivada) ddfx = f(Xo, segundaderivada) count = 0 error = tol + 1 den = (dfx * dfx) - (fx * ddfx) print "-----------||------------||-------------||-------------||--------------" print " X f(x) f'(x) f''(x) Error " print Xo, " | ", fx, " | ", dfx, " | ", error, " | " #If the stop parameters haven't been broken here we can calculate where is the root while fx != 0 and error > tol and den != 0 and count < nIter: Xi = Xo - (fx * dfx) / den error = abs(Xi - Xo) Xo = Xi fx = f(Xo, funcion) dfx = f(Xo, derivada) ddfx = f(Xo, segundaderivada) den = (dfx * dfx) - (fx * ddfx) count += 1 print Xo, " | ", fx, " | ", dfx, " | ", error, " | " #Here we show the root or the aproximation if the given point's not a root if (fx == 0): print Xi, " is a root" elif (error <= tol): print Xi, " is an approximation with ", tol, " tolerance" elif (dfx == 0): print "there are possible multiple roots at ", Xi else: eprint("failure after N iterations")
def secanteMethod(x0, x1, tol, iter): y0 = f(x0, funcion) #Here we ask if the point is a root if y0 == 0: print "X0 is root" else: y1 = f(x1, funcion) count = 0 error = tol + 1 Den = y1 - y0 print "-------------||------------||-------------" print " Xn f(Xn) Error " print x0, " | ", y0, " | ", " | " print x1, " | ", y1, " | ", " | " #If the stop parameters haven't been broken here we can calculate where is the root while error > tol and y1 != 0 and Den != 0 and count < iter: Xaux = x1 - ((y1 * (x1 - x0)) / Den) error = abs((Xaux - x1) / Xaux) x0 = x1 y0 = y1 x1 = Xaux y1 = f(x1, funcion) Den = y1 - y0 count += 1 print x1, " | ", y1, " | ", error, " | " #Here we show the root or the aproximation if is not an exact root between the interval if y1 == 0: print x1, " es root" elif error < tol: print x1, " is an approximate root with tolerance: ", tol elif Den == 0: print "Might be a multiple root" else: eprint("Failed in ", iter, " iterations")