Skip to content

Instantly share code, notes, and snippets.

@carlosal1015
Created July 11, 2023 14:44
Show Gist options
  • Save carlosal1015/fca51a2d54f3d7a0a80b726227b322d5 to your computer and use it in GitHub Desktop.
Save carlosal1015/fca51a2d54f3d7a0a80b726227b322d5 to your computer and use it in GitHub Desktop.
Fixed point (scalar and vectorial)
from math import sqrt
"""
[ f(x) = g(x) - x = 0 ] ==> x = g(x), x es punto fijo de g
punto_fijo = 1.365230013
print(abs(g_1(punto_fijo) - punto_fijo))
"""
def fixed_point(f, x0, eps = 1e-5, max_iteraciones = 20):
"""Paso 1"""
i = 0
"""Paso 2"""
while i < max_iteraciones:
"""Paso 3"""
f0 = f(x0)
print(f"x0: {x0}\t f(x0): {f0}")
"""Paso 4"""
if abs(f0 - x0) < eps:
print(f"Converge porque |f(x_0) - x_0| = {abs(f0 - x0)} < {eps}")
break
"""Paso 5"""
i = i + 1
"""Paso 6"""
x0 = f0
print(f"Solution: c = {x0}")
print(f"Numero de iteraciones: {i}")
g_1 = lambda x: x - x**3 - 4 * x**2 + 10
g_2 = lambda x: sqrt(10 / x - 4 * x)
g_3 = lambda x: 1 / 2 * sqrt(10 - x**3)
g_4 = lambda x: sqrt(10 / (4 + x))
g_5 = lambda x: x - (x**3 + 4 * x**2 - 10) / (3 * x**2 + 8 * x)
if __name__ == "__main__":
fixed_point(f=g_3, x0=1.5)
import numpy as np
# from fixed_point import fixed_point
"""
x_1_ast = np.array(object=[0, 1], dtype=float)
x_2_ast = np.array(object=[4/5, -3/5], dtype=float)
print(np.allclose(a=G_1(x_1_ast), b=x_1_ast))
print(np.allclose(a=G_2(x_2_ast), b=x_2_ast))
"""
def G_1(x: np.array):
return np.array(object=[(1 - x[1]) / 2, np.sqrt(1 - x[0] ** 2)], dtype=float)
def G_2(x: np.array):
return np.array(object=[(1 - x[1]) / 2, -np.sqrt(1 - x[0] ** 2)], dtype=float)
def fixed_point_rn(F, X0, eps=1e-5, max_iteraciones=20):
"""Paso 1"""
i = 0
"""Paso 2"""
while i < max_iteraciones:
"""Paso 3"""
F0 = F(X0)
print(f"X0: {X0}\t F(X0): {F0}")
"""Paso 4"""
if np.linalg.norm(F0 - X0, np.inf) < eps:
print(
f"Converge porque ||F(X_0) - X_0||_oo = {np.linalg.norm(F0 - X0, np.inf)} < {eps}"
)
break
"""Paso 5"""
i = i + 1
"""Paso 6"""
X0 = F0
print(f"Solution: c = {X0}")
print(f"Numero de iteraciones: {i}")
if __name__ == "__main__":
fixed_point_rn(
F=G_1,
X0=np.array(object=[-0.9, 0.9], dtype=float),
eps=1e-10,
max_iteraciones=10,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment