Uploaded by Javier Carrasco

Gradiente Seno

advertisement
import matplotlib.pyplot as plt
from numpy.random import randint
from numpy import linspace
import sympy as sp
from math import *
import pandas as pd
import numpy as np
from sympy import *
#encontrar el gradiente de la ecuaci
x = sp.symbols('x')
sp.diff(((x**2) * sin((x**0.5)))-5) x_inicial = randint(10)
alpha = 0.2
n_iteraciones = 150
iteraciones = []
y = []
x = x_inicial
for i in range(n_iteraciones):
print('------------------------')
print('iteración ', str(i+1))
# Calcular gradiente
gradiente = ((2*x) * sin(x**0.5)) + ((0.5*x**1.5) * cos(x**0.5))
# Actualizar "x" usando gradiente descendente
x = x - alpha*gradiente
# Almacenar iteración y valor correspondiente
y.append(((x**2) * sin((x**0.5)))-5)
iteraciones.append(i+1)
# Imprimir resultados
print('x = ', str(x), ', y = ', str(((x**2) * sin((x**0.5)))-5))
Download