def newton(x): alpha = backtrack_line_search(x) step = 0 alphas = [] xk_s = [] while (f(x) > 0.1e-18): print(f(x)) xk_s.append(x) alphas.append(alpha) x = (x - np.multiply(alpha, inv(hessiyan(x)).dot(gradient(x)))) alpha = backtrack_line_search(x) step += 1 if (f(x) == 0): break return xk_s, alphas, step
def incrementalSearch(start, step, stop): decimals = 4 return_list = [] evaluated = f(start) return_list.append({ 'iter': 0, 'x': start, 'f(x)': evaluated, 'root': 'NA', 'f(a)*f(b)': 'NA', 'range': 'NA' }) points = np.arange(start + step, stop + step, step) count = 1 for point in points: mult = float(evaluated * f(point)) root = True if mult < 0 else False row = { 'iter': count, 'x': point, 'f(x)': round(f(point), decimals), 'root': root, 'f(a)*f(b)': round(mult, decimals), 'range': [round(point - step, decimals), round(point, decimals)] } count = count + 1 return_list.append(row) evaluated = f(point) return {"iters": return_list}
def trust_region_cauchy(xk): trust_radius = 1.0 iteration = 0 xk_s = [] Ru_s = [] while True: xk_s.append(xk) gk = gradient(xk) Bk = hessiyan(xk) pk = find_cauchy_point(gk, Bk, trust_radius) print(f(xk)) xk, trust_radius, Ru = trust_region_find_delta(xk, pk, gk, Bk, trust_radius) Ru_s.append(Ru) if f(xk) < 0.0012310: break iteration = iteration + 1 showResult(xk_s, Ru_s, iteration) return xk
def backtrack_line_search(x0): alpha = 1 while (f(x0) - (f(x0 - alpha * gradient(x0)) + alpha * C * np.dot(gradient(x0), gradient(x0)))) < 0: alpha *= Ru return alpha
def functionline(function_str: str, x: float) -> float: """ :param function_str: :param x: argumento da função. :return: valor propriamente dito da derivada da função gravada em -> function() """ lib.receiveFunction(function_str) reload_funcao() h = Decimal(0.0000000000000000000000001) return Decimal( Decimal((function.f(Decimal(x) + h) - Decimal(function.f(x))) / h))
def newton(x0, tolerance, max_iterations): res = {} return_list = [] f_x = float(f(x0)) df_x = float(df(x0)) count = 0 error = tolerance + 1 return_list.append({ "count": count, "xSub0": x0, "f_x": f_x, "df_x": df_x, "error": 0 }) while error > tolerance and count < max_iterations and f_x != 0 and df_x != 0: next_x = x0 - (f_x / df_x) f_x = f(next_x) df_x = df(next_x) error = abs(next_x - x0) x0 = next_x count += 1 return_list.append({ "count": count, "xSub0": x0, "f_x": f_x, "df_x": df_x, "error": error }) if f_x == 0: res["iters"] = return_list res["status"] = 'Root found! ;)' res["error"] = False return res elif error <= tolerance: res["iters"] = return_list res["status"] = 'Err lower than tolerance! :)' res["error"] = False return res elif df_x == 0: res["iters"] = return_list res["status"] = 'Possible multiple root! :0' res["error"] = True return res elif (count >= max_iterations): res["iters"] = return_list res["status"] = 'Overpassed max iteration! :(' res["error"] = True return res return res
def falseRule(xi, xs, tol, max_iter): res = {} xr = evaluateFunction(xi, xs) f_xr = f(xr) return_list = [] return_list.append({ 'iter': 1, 'xi': xi, 'xs': xs, 'xr': xr, 'f(xr)': f_xr, 'error': 'NA' }) count = 2 error = tol + 1 while error > tol and count <= max_iter: xi = xr if (f_xr < 0) else xi xs = xr if (f_xr > 0) else xs tempXr = xr xr = evaluateFunction(xi, xs) error = abs(xr - tempXr) f_xr = f(xr) row = { 'iter': count, 'xi': xi, 'xs': xs, 'xr': xr, 'f(xr)': f_xr, 'error': error } return_list.append(row) if f_xr == 0: res["iters"] = return_list res["status"] = 'Root found! ;)' res["error"] = False return res elif error < tol: res["iters"] = return_list res["status"] = 'Err lower than tolerance! :)' res["error"] = True return res elif count >= max_iter: res["iters"] = return_list res["status"] = 'Overpass max iteration! :(' res["error"] = True return res count = count + 1 return res
def falseposition_aux(a: float, b: float, E: float): if function.f(a) * function.f(b) > 0: return "Intervalo inválido!" if abs(function.f(b)) > E: avg = ((a * function.f(b) - b * function.f(a)) / (function.f(b) - function.f(a))) if function.f(avg) * function.f(a) < 0: return falseposition_aux(a, avg, E) else: return falseposition_aux(b, avg, E) return b
def newton_raphson(function_str: str, avg: float, E: float) -> float: """ :param function_str: string da função :param avg: chute inicial :param E: margem de erro :return: valor em que a função atinge a imagem = 0 """ lib.receiveFunction(function_str) reload_funcao() if abs(function.f(avg)) > E: aux = Decimal(avg) - Decimal(function.f(avg)) / functionline( function_str, avg) return newton_raphson(function_str, aux, E) return avg
def midpoint(xi, f, F=[]): """ Use midpoint quadrature based on the subdivision xi Input: xi: location of nodes, defining the subdivision of the interval (including the endpoints) f: function to integrate F: antiderivative (for computing errors) Output: I = the numerical approximation to the integral on each subinterval E = error (absolute value) on each subinterval (only if F is known) """ m = len(xi) - 1 E = zeros(m) I = zeros(m) for i in range(m): hi = xi[i + 1] - xi[i] ci = (xi[i + 1] + xi[i]) / 2 Ii_midpoint = hi * f(ci) if F: Ii_exact = F(xi[i + 1]) - F( xi[i]) # in reality we of course do not know this E[i] = abs(Ii_midpoint - Ii_exact) I[i] = Ii_midpoint return I, E
def bfgs_method(x0): gfk = gradient(x0) I = np.eye(len(x0), dtype=int) Hk = I xk = x0 xs = [] alphas = [] steps = 0 while (f(xk) > 0.1e-18): xs.append(xk) steps += 1 pk = -np.dot(Hk, gfk) alpha_k = backtrack_line_search(x0) alphas.append(alpha_k) xk1 = xk + alpha_k * pk sk = xk1 - xk xk = xk1 gfk1 = gradient(xk) yk = gfk1 - gfk gfk = gfk1 term = 1.0 / (np.dot(yk, sk)) term1 = I - term * sk[:, np.newaxis] * yk[np.newaxis, :] term2 = I - term * yk[:, np.newaxis] * sk[np.newaxis, :] Hk = np.dot(term1, np.dot( Hk, term2)) + (term * sk[:, np.newaxis] * sk[np.newaxis, :]) return xs, alphas, steps
def trust_region_dogleg(xk): trust_radius = 1.0 iteration = 0 xk_s = [] Ru_s = [] while True: xk_s.append(xk) gk = gradient(xk) Bk = hessiyan(xk) Hk = np.linalg.inv(Bk) pk = dogleg_method(Hk, gk, Bk, trust_radius) xk, trust_radius, Ru = trust_region_find_delta(xk, pk, gk, Bk, trust_radius) Ru_s.append(Ru) if f(xk) < 0.1e-18: break iteration = iteration + 1 showResult(xk_s, Ru_s, iteration) return xk
def fixedPoint (xi,tol,max_iter): res = {} f_xi = f(xi) g_xi = g(xi) return_list = [] return_list.append({ 'iter':0, 'xi': xi, 'g(xi)':g_xi, 'f(xi)': f_xi, 'error':'NA' }) count = 1 error = tol + 1 while error > tol and count <= max_iter: xn = g_xi g_xi = g(xn) f_xi = f(xn) error = abs(xn-xi) xi = xn row = { 'iter' : count, 'xi': xi, 'g(xi)':g_xi, 'f(xi)': f_xi, 'error': error } return_list.append(row) if(f_xi == 0): res["iters"] = return_list res["status"] = 'Root found! ;)' res["error"] = False return res elif(error < tol): res["iters"] = return_list res["status"] = 'Err lower than tolerance! :)' res["error"] = False return res elif(count >= max_iter): res["iters"] = return_list res["status"] = 'Overpassed max iteration! :(' res["error"] = True return res count = count + 1 return {"iters" : return_list}
def runge_kutta(h, left, right): x, y = 0, 1 result = [] x_axis = [] while right >= left: result.append(y) x_axis.append(x) left += h k1 = h * f(x, y) k2 = h * f(x + 0.5 * h, y + 0.5 * k1) k3 = h * f(x + 0.5 * h, y + 0.5 * k2) k4 = h * f(x + h, y + k3) y += (k1 + 2 * k2 + 2 * k3 + k4) / 6 x = left result.append(y) x_axis.append(x) return x_axis, result
def multipleRoots(xi, tol, max_iter): res = {} f_xi = f(xi) df_xi = df(xi) ddf_xi = ddf(xi) return_list = [] return_list.append({ 'iter': 0, 'xi': xi, 'f(xi)': f_xi, "f'(xi)": df_xi, "f''(xi)": ddf_xi, 'error': 'NA' }) count = 1 error = tol + 1 while error > tol and count <= max_iter: xiTemp = xi xi = x_next(xi) f_xi = f(xi) df_xi = df(xi) ddf_xi = ddf(xi) error = abs(xi - xiTemp) row = { 'iter': count, 'xi': xi, 'f(xi)': f_xi, "f'(xi)": df_xi, "f''(xi)": ddf_xi, 'error': error } return_list.append(row) if error <= tol: res["iters"] = return_list res["status"] = 'Err lower than tolerance! :)' res["error"] = False return res elif (count >= max_iter): res["iters"] = return_list res["status"] = 'Overpassed max iteration! :(' res["error"] = True return res count = count + 1 return res
def method_secant(x0: float, x1: float, error: float): while True: f_x0 = function.f(x0) f_x0 = auxiliary.rounding(f_x0, error) f_x1 = function.f(x1) f_x1 = auxiliary.rounding(f_x1, error) x = (x0 * f_x1 - x1 * f_x0) / (f_x1 - f_x0) x = auxiliary.rounding(x, error) f_x = function.f(x) f_x = auxiliary.rounding(f_x, error) if (math.fabs(f_x0) <= error): return auxiliary.convert_json('x', x0) elif (math.fabs(f_x1) <= error): return auxiliary.convert_json('x', x1) elif (math.fabs(f_x) <= error): return auxiliary.convert_json('x', x) x0 = x1 x1 = x
def secant(function_str: str, xnmenos: float, xn: float, E: float) -> float: """ :param function_str: string da função :param xnmenos: valor do xi - 1 :param xn: valor do xi :param E: margem de erro :return: valor em que a função atinge a imagem = 0 """ lib.receiveFunction(function_str) reload_funcao() xnmais = ((xnmenos * function.f(xn) - xn * function.f(xnmenos)) / (function.f(xn) - function.f(xnmenos))) if abs(function.f(xn)) > E: return secant(function_str, xn, xnmais, E) else: return xn
def method_false_position(a: float, b: float, error: float): while True: f_a = function.f(a) f_b = function.f(b) if math.fabs(f_a) <= error: return auxiliary.convert_json('x', a) elif math.fabs(f_b) <= error: return auxiliary.convert_json('x', b) elif f_a * f_b < 0: x = (a * f_b - b * f_a) / (f_b - f_a) f_x = function.f(x) if f_a * f_x < 0: b = x else: a = x else: return auxiliary.convert_json('x', 'Inválido')
def method_newton_raphson(x: float, error: float): while True: f_x = function.f(x) f_x = auxiliary.rounding(f_x, error) if math.fabs(f_x) <= error: return auxiliary.convert_json('x', x) f_dx = derivative.f(x) f_dx = auxiliary.rounding(f_dx, error) x = x - f_x / f_dx x = auxiliary.rounding(x, error)
def trapezoid(xi, f, F=[]): """ Use trapezoid quadrature based on the subdivision xi Input: xi: location of nodes, defining the subdivision of the interval (including the endpoints) f: function to integrate Output: I = the numerical approximation to the integral on each subinterval """ m = len(xi)-1 I = np.zeros(m) for i in range(m): hi = xi[i+1]-xi[i] I[i] = hi/2*(f(xi[i])+f(xi[i+1])) return I
def midpoint(xi, f): """ Use midpoint quadrature based on the subdivision xi Input: xi: location of nodes, defining the subdivision of the interval (including the endpoints) f: function to integrate Output: I = the numerical approximation to the integral on each subinterval """ m = len(xi) - 1 I = zeros(m) for i in range(m): hi = xi[i + 1] - xi[i] ci = (xi[i + 1] + xi[i]) / 2 I[i] = (hi / 6) * (f(x[i]) + 4 * f(ci) + f(x[i + 1])) return sum(I)
def get_trajectory(x_0, y_0, n, flag): counter = 1 vector_x = [x_0] vector_y = [y_0] if flag == 0: alpha = 0.001 while counter <= n: v_x = f.f(vector_x[counter - 1], vector_y[counter - 1])[0] v_y = f.f(vector_x[counter - 1], vector_y[counter - 1])[1] normalized_vector_of_speed_x = v_x / math.sqrt(v_x**2 + v_y**2) normalized_vector_of_speed_y = v_y / math.sqrt(v_x**2 + v_y**2) if flag == 1: alpha = abs(alphafind.alpha_1(counter)) elif flag == 2: alpha = abs(alphafind.alpha_2(counter + 1, n)) vector_x.append(vector_x[counter - 1] + alpha * normalized_vector_of_speed_x) vector_y.append(vector_y[counter - 1] + alpha * normalized_vector_of_speed_y) counter += 1 return vector_x, vector_y
def method_bisection(a: float, b: float, error: float): while True: f_a = function.f(a) f_a = auxiliary.rounding(f_a, error) f_b = function.f(b) f_b = auxiliary.rounding(f_b, error) if math.fabs(f_a) <= error: return auxiliary.convert_json('x', a) elif math.fabs(f_b) <= error: return auxiliary.convert_json('x', b) elif f_a * f_b < 0: x = (a + b) / 2 f_x = function.f(x) f_x = auxiliary.rounding(f_x, error) if f_a * f_x < 0: b = x else: a = x else: return auxiliary.convert_json('x', 'Inválido')
def inexact_newton(w, i): ''' execute the inexact newton method using the conjugate gradient returns the number of steps necessary to achieve the requiered precision ''' k = 0 while(func.f(w) >= epsilon): # This is the heart of the inexact newton # We follow the formula wk+1 = wk - alphak * gradient(wk) / hessian(wk) # But we use the conjugate gradient method that gives use " - gradient(wk) / hessian(wk) " w = w + alpha * cg.conjugate_gradient(w, func.hessian(w), -func.gradient(w), i) k = k+1 return k
def bisection(function_str: str, a: float, b: float, E: float) -> float: """ :param function_str: string da função. :param a: chute numero 1 :param b: chute numero 2 :param E: margem de erro :return: valor em que a função atinge a imagem = 0 """ lib.receiveFunction(function_str) reload_funcao() if function.f(a) * function.f(b) > 0: return "Intervalo inválido!" if abs(function.f(b)) > E: avg = (a + b) / 2 if function.f(avg) * function.f(a) < 0: return bisection(function_str, a, avg, E) else: return bisection(function_str, b, avg, E) return b
def trust_region_find_delta(xk, pk, gk, Bk, trust_radius): max_trust_radius = 100.0 eta = 0.15 func_red = f(xk) - f(xk + pk) model_red = -(np.dot(gk, pk) + 0.5 * np.dot(pk, np.dot(Bk, pk))) if model_red == 0.0: Ru = 1e99 else: Ru = func_red / model_red norm_pk = sqrt(np.dot(pk, pk)) if Ru < 0.25: trust_radius = 0.25 * trust_radius else: if Ru > 0.75 and norm_pk == trust_radius: trust_radius = min(2.0 * trust_radius, max_trust_radius) else: trust_radius = trust_radius if Ru > eta: xk = xk + pk else: xk = xk return xk, trust_radius, Ru
def steepestDescent(x): alpha = backtrack_line_search(x) xk_s = [] alphas = [] step = 0 while (f(x) > 0.1e-18): step += 1 xk_s.append(x) alphas.append(alpha) x = (x - np.multiply(alpha, gradient(x))) alpha = backtrack_line_search(x) return xk_s, alphas, step
for j in w_range: x = unit * j + first[0] if (y == 0): if (x == 0): graph[i].append("+ ") else: graph[i].append("- ") elif (x == 0): graph[i].append("| ") else: graph[i].append(". ") # Write over graph given y=f(x) jay = 0 x = first[0] while (jay + 0.5 * unit < width): j = toint(jay) y = f(x) i = toint((first[1] - y) / unit) if (i >= 0 and i < height): graph[i][j] = "O " jay = jay + prec x = jay * unit + first[0] # Display graph for i in range(0, len(graph)): strand = str(first[1] - unit * i) + "\t" for j in range(0, len(graph[i])): strand = strand + graph[i][j] print strand
def x_next(xn): return xn - ((f(xn) * df(xn)) / ((df(xn)**2) - f(xn) * ddf(xn)))
def bisection(a, b, tolerance, max_iterators): res = {} return_list = [] a_evaluated_f = float(f(a)) b_evaluated_f = float(f(b)) if a_evaluated_f == 0: return {"status": "root on A"} elif b_evaluated_f == 0: return {"status": "root on B"} elif a_evaluated_f * b_evaluated_f < 0: count = 1 x_middle = float((a + b) / 2) y_middle = float(f(x_middle)) error = tolerance + 1 row = { 'x': count, 'a': a, 'b': b, 'x_middle': x_middle, 'y_middle': y_middle, 'error': 0 } return_list.append(row) count += 1 while error > tolerance and y_middle != 0 and count <= max_iterators: if a_evaluated_f * y_middle < 0: b = x_middle b_evaluated_f = y_middle else: a = x_middle a_evaluated_f = y_middle aux_middle = x_middle x_middle = (a + b) / 2 y_middle = f(x_middle) error = abs(x_middle - aux_middle) row = { 'x': count, 'a': a, 'b': b, 'x_middle': x_middle, 'y_middle': y_middle, 'error': error } count += 1 return_list.append(row) if (y_middle == 0): res["iters"] = return_list res["status"] = 'Root found! ;)' res["error"] = False return res elif (error < tolerance): res["iters"] = return_list res["status"] = 'Err lower than tolerance! :)' res["error"] = True return res elif (count >= max_iterators): res["iters"] = return_list res["status"] = 'Overpass max iteration! :(' res["error"] = True return res return res