TRABAJO PRACTICO EN MATLAB – PYTHON
APELLIDOS Y NOMBRES: VELIZ MOLLERICONA JHONATAN ISRAEL
import math
import [Link] as plt
import numpy as np
a = float(input("ingrese a: "))
b = float(input("ingrese b: "))
maxi = int(input("iteraciones: "))
def f(x):
return [Link](x) - x
print("i | a | b | c | f(c)")
for i in range(maxi):
c = (a + b) / 2
print(i+1, "|", f"{a:.4f}", "|", f"{b:.4f}", "|", f"{c:.4f}", "|", f"{f(c):.4f}")
if f(a) * f(c) < 0:
b=c
else:
a=c
x = [Link](0, 1, 100)
[Link](x, [f(i) for i in x])
[Link](0, color='black')
[Link]("Biseccion: Busca el punto medio")
[Link]()
import math
import [Link] as plt
import numpy as np
# Funcion: f(x) = ln(x) + x - 2
def f(x):
return [Link](x) + x - 2
# Puntos iniciales segun el ejercicio
a = 1.0
b = 2.0
fa, fb = f(a), f(b)
# Formula de Interpolacion Lineal Inversa
xr = b - (fb * (a - b)) / (fa - fb)
print("--- 1.4.3 INTERPOLACION LINEAL INVERSA ---")
print(f"Punto a: {a} | f(a): {fa:.4f}")
print(f"Punto b: {b} | f(b): {fb:.4f}")
print(f"La raiz calculada (xr) es: {xr:.6f}")
# Codigo para la Grafica
x_vals = [Link](0.5, 2.5, 100)
y_vals = [f(i) for i in x_vals]
[Link](x_vals, y_vals, label="f(x) = ln(x)+x-2")
[Link]([a, b], [fa, fb], 'r--o', label="Linea Secante") # Linea roja entre puntos
[Link](0, color='black', linewidth=1) # Eje X
[Link]("Interpolacion Lineal Inversa")
[Link]()
[Link](True)
[Link]()
import math
import [Link] as plt
import numpy as np
# Definir la funcion del ejercicio
def f(x):
return [Link](-x) - x
# Valores iniciales
a = 0.0
b = 1.0
iteraciones = 8
print("Iter | a | b | xr | f(xr)")
print("-" * 45)
# Listas para la grafica
historial_x = []
for i in range(iteraciones):
# Formula de la falsa posicion
xr = b - (f(b) * (a - b)) / (f(a) - f(b))
fxr = f(xr)
historial_x.append(xr)
print(f"{i+1} | {a:.4f} | {b:.4f} | {xr:.4f} | {fxr:.4f}")
# Cambio de intervalos
if f(a) * fxr < 0:
b = xr
else:
a = xr
# --- PARTE DE LA GRAFICA ---
x = [Link](-0.5, 1.5, 100)
y = [f(val) for val in x]
[Link](x, y, color='blue', label='f(x)') # La curva
[Link](0, color='black') # Linea del cero
[Link](True) # Cuadricula sencilla
# Dibujar los puntos donde el metodo trabajo
[Link](historial_x, [f(i) for i in historial_x], color='red')
[Link]("Metodo Falsa Posicion 1.4.4")
[Link]()
import math
import [Link] as plt
import numpy as np
# Funcion del ejercicio 1.4.5: f(x) = x^3 - x - 1
def f(x):
return x**3 - x - 1
# Datos iniciales del problema
a = 1.0
b = 2.0
iteraciones = 6
print("it | a | b | xr | f(xr)")
print("-" * 45)
historial_xr = []
fa = f(a)
fb = f(b)
for i in range(iteraciones):
# Formula de la Falsa Posicion
xr = b - (fb * (a - b)) / (fa - fb)
fxr = f(xr)
historial_xr.append(xr)
print(f"{i+1} | {a:.4f} | {b:.4f} | {xr:.4f} | {fxr:.4f}")
# Logica del metodo de Illinois (Modificado)
if fa * fxr < 0:
b = xr
fb = fxr
fa = fa / 2 # Se reduce a la mitad para acelerar
else:
a = xr
fa = fxr
fb = fb / 2 # Se reduce a la mitad para acelerar
# --- CREAR LA GRAFICA ---
x_rango = [Link](0.5, 2.5, 100)
y_rango = [f(i) for i in x_rango
[Link](x_rango, y_rango, color='blue', label='f(x) = x^3 - x - 1')
[Link](0, color='black', linewidth=1) # Eje X
[Link](True, linestyle='--')
# Dibujar los puntos encontrados
[Link](historial_xr, [f(i) for i in historial_xr], color='red', label='Raiz aproximada')
[Link]("Metodo de Falsa Posicion Modificada (Illinois)")
[Link]()
[Link]()
import math
import
[Link] as plt
import numpy as np
# Funcion 1.4.6: f(x) = x
* ln(x) - 1
def f(x):
return x * [Link](x) - 1
# Valores iniciales de tu ejercicio
x0 = 1.0
x1 = 2.0
iteraciones = 6
print("i | xi | f(xi)")
print("-" * 30)
# Para guardar puntos y graficar
historial_x = [x0, x1]
for i in range(iteraciones):
f0 = f(x0)
f1 = f(x1)
# Formula de la Secante
# x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))
x_nuevo = x1 - f1 * (x1 - x0) / (f1 - f0)
# Actualizar valores para el siguiente paso
x0 = x1
x1 = x_nuevo
historial_x.append(x1)
print(f"{i+1} | {x1:.4f} | {f(x1):.4f}")
# --- GRAFICA ---
x_rango = [Link](0.5, 2.5, 100)
y_rango = [f(val) for val in x_rango]
[Link](x_rango, y_rango, color='blue', label='f(x)')
[Link](0, color='black', linewidth=1)
[Link](True, linestyle=':')
# Dibujar las aproximaciones
[Link](historial_x, [f(n) for n in historial_x], color='red', label='Aproximaciones')
[Link]("Metodo de la Secante - Ejercicio 1.4.6")
[Link]()
[Link]()
import math
import [Link] as plt
import numpy as np
# Funcion 1.4.7: f(x) = sin(x) - 0.5
def f(x):
return [Link](x) - 0.5
# Derivada: f'(x) = cos(x)
def df(x):
return [Link](x)
# Valor inicial
x0 = 1.0
iteraciones = 5
print("i | xi | f(xi)")
print("-" * 25)
historial_x = [x0]
for i in range(iteraciones):
# Formula de Newton-Raphson: x_nuevo = x - f(x)/f'(x)
x_nuevo = x0 - f(x0) / df(x0)
x0 = x_nuevo
historial_x.append(x0)
print(f"{i+1} | {x0:.4f} | {f(x0):.4f}")
# --- CODIGO DE LA GRAFICA ---
x_vals = [Link](0, 2, 100)
y_vals = [f(val) for val in x_vals]
[Link](x_vals, y_vals, color='blue', label='f(x) = sin(x)-0.5')
[Link](0, color='black', linewidth=1)
[Link](True, linestyle='--')
# Dibujar los puntos y las tangentes
[Link](historial_x, [f(n) for n in historial_x], color='red', label='Iteraciones')
[Link]("Metodo de Newton-Raphson 1.4.7")
[Link]()
[Link]()
import math
import [Link] as plt
import numpy as np
# Funcion 1.4.8: f(x) = e^x - 3x
def f(x):
return [Link](x) - 3*x
# Datos del ejercicio
x0 = 1.0
delta = 0.0001
iteraciones = 5
print("i | xi | f(xi)")
print("-" * 25)
historial_x = [x0]
for i in range(iteraciones):
fx = f(x0)
# Calculo del punto desplazado por delta
f_delta = f(x0 + delta * x0)
# Formula de la Secante Modificada
x_nuevo = x0 - (delta * x0 * fx) / (f_delta - fx)
x0 = x_nuevo
historial_x.append(x0)
print(f"{i+1} | {x0:.4f} | {f(x0):.4f}")
# --- GRAFICA ---
x_plot = [Link](0, 2, 100)
y_plot = [f(val) for val in x_plot]
[Link](x_plot, y_plot, color='blue', label='f(x) = e^x - 3x')
[Link](0, color='black', linewidth=1)
[Link](True, linestyle='--')
# Marcar los puntos de las iteraciones
[Link](historial_x, [f(n) for n in historial_x], color='red', label='Iteraciones')
[Link]("Metodo de la Secante Modificada 1.4.8")
[Link]()
[Link]()