def secante(xn, fn, error): errorTemp = 1000000 maxIt = 100 cont = 0 h = error / 10 xAnt = xn prs = Parse() prs.setEc(fn) historial = [] historial.append([xn, 0.0]) while (errorTemp > error and maxIt > cont): prs.addVariable("x", xAnt) resfn = prs.evaluate() prs.addVariable("x", xAnt + h) resfxh = prs.evaluate() prs.addVariable("x", xAnt - h) resfx_h = prs.evaluate() if ((resfxh - resfx_h) == 0): print("division entre zero") break x = xAnt - ((2 * h * resfn) / (resfxh - resfx_h)) errorTemp = abs(xAnt - x) historial.append([xAnt, errorTemp]) xAnt = x if (errorTemp < error): break cont += 1 return x, historial
def bolzano(fn, a, b): prs = Parse() prs.setEc(fn) prs.addVariable("x", a) evA = prs.evaluate() prs.addVariable("x", b) evB = prs.evaluate() print("bolzano val", evA, evB) if ((evA * evB) < 0): return True return False
def trapecio(a, b, fn, n): h = (b - a) / n prs = Parse() prs.setEc(fn) i = a + h sum = 0 while (i < b): prs.addVariable("x", i) sum += prs.evaluate() i += h prs.addVariable("x", a) fa = prs.evaluate() prs.addVariable("x", b) fb = prs.evaluate() resp = h * (((fa + fb) / 2) + sum) return resp
def imagenNR(LFn, Lx): tparse = Parse() LFxn = [] for i in range(len(LFn)): tparse.setEc(LFn[i]) tparse.addVarFromList(Lx) LFxn.append(tparse.evaluate()) return LFxn
def getY(formula, x): prs = Parse() y = [] prs.setEc(formula) for i in x: prs.addVariable("x", i) yT = prs.evaluate() #print(yT) y.append(yT) return y
def simpson(a, b, fn, n): h = (b - a) / (2 * n) prs = Parse() prs.setEc(fn) parSum = 0 imparSum = 0 i = a + h xi = np.arange(i, b, h) for index in range(len(xi)): prs.addVariable("x", xi[index]) if (index % 2 == 0): imparSum += prs.evaluate() else: parSum += prs.evaluate() prs.addVariable("x", a) fa = prs.evaluate() prs.addVariable("x", b) fb = prs.evaluate() resp = (h / 3) * (fa + fb + (2 * parSum) + (4 * imparSum)) return resp
def jacobiana(LFn, Lx, err): tparse = Parse() Jmatrix = [] h = err / 10 TLx = [] for i in range(len(LFn)): mTemp = [] for j in range(len(Lx)): TLx = list(Lx) TLx[j] = Lx[j] + h tparse.setEc(LFn[i]) tparse.addVarFromList(TLx) rH = tparse.evaluate() TLx = list(Lx) TLx[j] = Lx[j] - h tparse.addVarFromList(TLx) r_H = tparse.evaluate() DR = (rH - r_H) / (2 * (h * 100)) mTemp.append(DR) Jmatrix.append(mTemp) result = Jmatrix return result
def eulerSimple(df, xn, yn, h, xf): prs = Parse() prs.setEc(df) h = float(h) xt = xn yt = yn while (xt < xf): prs.addVariable("x", xt) prs.addVariable("y", yt) yt = yt + h * (prs.evaluate()) print(yt) xt = round(xt + h, 15) return yt
def puntofijo(xn, fn, error): maxIt = 100 historial = [] xr = xn prs = Parse() fn = fn + "+x" prs.setEc(fn) historial.append([xn, 0]) for i in range(maxIt): xAnt = xr prs.addVariable("x", xr) xr = prs.evaluate() print("xr", xr) errorTemp = abs(xAnt - xr) historial.append([xr, errorTemp]) if (errorTemp < error): break return xr, historial