No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Ejemplo en aproximaciones de Taylor

17/28
Recursos

Aportes 17

Preguntas 1

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Reg铆strate o inicia sesi贸n para participar.

Con el 谩nimo de complementar un poco, me tom茅 el tiempo para graficar la comparaci贸n entre la primera funci贸n que se presenta en la clase y su aproximaxic贸n de Taylor. Este fue el resultado:

En este caso, por ser una funci贸n que contiene dos variables, se puede graficar como una superficie. Por lo tanto, su approximaci贸n de Taylor es un plano que toca a la superficie en z.

Tambi茅n grafiqu茅 el error:

Este es el link por si quieres echar un vistazo al c贸digo para graficar.

Menos mal estudi茅 ingenier铆a, porque creo que muchos se perder铆an con tantas cosas 馃槷

No se si entendi muy bien el ejercico que teniamos que hacer, pero esto es lo que consegui aproximando en f(x) = 3.7183 + [-1.72, 2.72] [x0 - 1, x1 - 2]

x = [1 2], f(x) = 3.718281828459045, taylor(x,z) = 3.718281828459045, |f(x) - taylor(x,z)| = 0.0
x = [2 4], f(x) = 9.38905609893065, taylor(x,z) = 7.43656365691809, |f(x) - taylor(x,z)| = 1.9524924420125602
x = [3 6], f(x) = 23.085536923187668, taylor(x,z) = 11.154845485377136, |f(x) - taylor(x,z)| = 11.930691437810532
x = [4 8], f(x) = 58.598150033144236, taylor(x,z) = 14.87312731383618, |f(x) - taylor(x,z)| = 43.72502271930806
x = [ 5 10], f(x) = 153.4131591025766, taylor(x,z) = 18.591409142295227, |f(x) - taylor(x,z)| = 134.82174996028138

Hice el ejercicio, a partir de vectores x incrementales sobre el original [1 2] y esos fueron los resultados obtenidos

Dejo la tarea de la clase:

Ejercicio propuesto en la clase:

Me he tardado bastante con este ejercicio y aun asi pienso que mi c贸digo es incorrecto. Me ser铆a de mucha utilidad si me pueden apoyar con alguna sugerencia.
Mi c贸digo en Deepnote

Sinceramente no entendi la parte de derivar la funcion evaluada en z, por ejemplo, si tengo que derivarla a mano, tendria que sacar, en el primer ejemplo la derivada parcial de x + e^(2 - x) y en el segundo ejemplo la derivada pacial 1 + e^( x - 1) ?

para z = [1, 2]

f(x) - taylor_f(x) = 0

##Error de aproximaci贸n 
x = np.arange(-10,12,.5)
y = np.arange(-9,13,.5)
coords = [[i,j] for i,j in zip(x,y)]
print(coords)

def error(coords):
  for i in coords:
    print('x = {}, f(x) = {}, Taylor f(x) = {}, |f(x) - Taylor f(x)| = {}'.format(i,F(i),taylor_F(i,z),np.abs(F(i)-taylor_F(i,z))))
    print('---'*10)

error(coords)

<h3>x = [-6.0, -5.0], f(x) = -3.281718171540955, Taylor f(x) = -3.281718171540955, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-5.5, -4.5], f(x) = -2.781718171540955, Taylor f(x) = -2.781718171540954, |f(x) - Taylor f(x)| = 8.881784197001252e-16</h3> <h3>x = [-5.0, -4.0], f(x) = -2.281718171540955, Taylor f(x) = -2.281718171540954, |f(x) - Taylor f(x)| = 8.881784197001252e-16</h3> <h3>x = [-4.5, -3.5], f(x) = -1.781718171540955, Taylor f(x) = -1.7817181715409558, |f(x) - Taylor f(x)| = 8.881784197001252e-16</h3> <h3>x = [-4.0, -3.0], f(x) = -1.281718171540955, Taylor f(x) = -1.281718171540955, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-3.5, -2.5], f(x) = -0.7817181715409549, Taylor f(x) = -0.7817181715409549, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-3.0, -2.0], f(x) = -0.2817181715409549, Taylor f(x) = -0.2817181715409549, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-2.5, -1.5], f(x) = 0.2182818284590451, Taylor f(x) = 0.2182818284590451, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-2.0, -1.0], f(x) = 0.7182818284590451, Taylor f(x) = 0.7182818284590455, |f(x) - Taylor f(x)| = 4.440892098500626e-16</h3> <h3>x = [-1.5, -0.5], f(x) = 1.218281828459045, Taylor f(x) = 1.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-1.0, 0.0], f(x) = 1.718281828459045, Taylor f(x) = 1.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [-0.5, 0.5], f(x) = 2.218281828459045, Taylor f(x) = 2.2182818284590455, |f(x) - Taylor f(x)| = 4.440892098500626e-16</h3> <h3>x = [0.0, 1.0], f(x) = 2.718281828459045, Taylor f(x) = 2.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [0.5, 1.5], f(x) = 3.218281828459045, Taylor f(x) = 3.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [1.0, 2.0], f(x) = 3.718281828459045, Taylor f(x) = 3.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [1.5, 2.5], f(x) = 4.218281828459045, Taylor f(x) = 4.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [2.0, 3.0], f(x) = 4.718281828459045, Taylor f(x) = 4.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [2.5, 3.5], f(x) = 5.218281828459045, Taylor f(x) = 5.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [3.0, 4.0], f(x) = 5.718281828459045, Taylor f(x) = 5.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [3.5, 4.5], f(x) = 6.218281828459045, Taylor f(x) = 6.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [4.0, 5.0], f(x) = 6.718281828459045, Taylor f(x) = 6.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [4.5, 5.5], f(x) = 7.218281828459045, Taylor f(x) = 7.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [5.0, 6.0], f(x) = 7.718281828459045, Taylor f(x) = 7.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [5.5, 6.5], f(x) = 8.218281828459045, Taylor f(x) = 8.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [6.0, 7.0], f(x) = 8.718281828459045, Taylor f(x) = 8.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [6.5, 7.5], f(x) = 9.218281828459045, Taylor f(x) = 9.218281828459046, |f(x) - Taylor f(x)| = 1.7763568394002505e-15</h3> <h3>x = [7.0, 8.0], f(x) = 9.718281828459045, Taylor f(x) = 9.718281828459045, |f(x) - Taylor f(x)| = 0.0</h3> <h3>x = [7.5, 8.5], f(x) = 10.218281828459045, Taylor f(x) = 10.218281828459045, |f(x) - Taylor f(x)| = 0.0</h3>

x = [8.0, 9.0], f(x) = 10.718281828459045, Taylor f(x) = 10.718281828459045, |f(x) - Taylor f(x)| = 0.0

z = np.array([1,2])

x_punto = np.array([1,2])
error1 = np.abs(F(x_punto) - taylor_F(x_punto,z))
print(f'Error1: {error1}')

x_punto = np.array([2,4])
error2 = np.abs(F(x_punto) - taylor_F(x_punto,z))
print(f'Error2: {error2}')

x_punto = np.array([3,6])
error3 = np.abs(F(x_punto) - taylor_F(x_punto,z))
print(f'Error3: {error3}')

x_punto = np.array([4,8])
error4 = np.abs(F(x_punto) - taylor_F(x_punto,z))
print(f'Error4: {error4}')

Modificando los valores del profe se calculo los errores de la funcion:
definos los puntos a evaluar:
se importo numpy primero.

z = np.array([1,2])
def gradF(z):
    return np.array([1-np.exp(z[1]-z[0]),np.exp(z[1]-z[0])])
print('grad(f)[1,2] = ', gradF(z))
grad(F)[1,2] =  [-1.71828183   2.71828183]

def F(x):
    return x[0] + np.exp(x[1]-x[0])
print('F([1,2]) =',F(z))
F([1,2]) = 3.718281828459045

con esto evaluamos para varios puntos:

j = np.array([1.5,3])
y = np.array([2,4])
w = np.array([3,6])
k = np.array([4,8])
f([1,2]) = 3.718281828459045
f([1.5,3]) = 5.9816890703380645
f([2,4]) = 9.38905609893065
f([3,6]) = 23.085536923187668
f([4,8]) = 58.598150033144236
taylor_F([1,2],[1,2])= 3.718281828459045
taylor_F([1.5,3],[1,2]) =  5.577422742688568
taylor_F([2,4],[1,2]) =  7.43656365691809
taylor_F([3,6],[1,2]) =  11.154845485377136
taylor_F([4,8],[1,2]) =  14.873127313836179
El error definido por |f([1,2]) - Taylor f([1,2],[1,2])| es =  0.0
El error definido por |f([1.5,3]) - Taylor f([1.5,3],[1,2])| es =  0.40426632764949666
El error definido por |f([2,4]) - Taylor f([2,4],[1,2])| es =  1.9524924420125602
El error definido por |f([3,6]) - Taylor f([3,6],[1,2])| es =  11.930691437810532
El error definido por |f([4,8]) - Taylor f([4,8],[1,2])| es =  43.72502271930806 

Reto.

import numpy as np 

def gradF(z):
    return np.array([1-np.exp(z[1]-z[0]), np.exp(z[1]-z[0])])

def F(x):
    return x[0] + np.exp(x[1]-x[0])

def Taylor(x, z):
    fz = F(z)
    gz = gradF(z)
    return fz + [email protected](x - z)

if __name__ == '__main__':

    z = np.array([1, 2])

    print('grad([1, 2]) = ', gradF(z))
    print('F([1, 2]) = ', F(z))
    print('Taylor([1, 2], [1, 2]) = ', Taylor(np.array([1, 2]), np.array([1, 2])))

    print(f'x = [1.2, 2.2], f(x) = {F([1, 2.2])}, Taylor f(x) = {Taylor(np.array([1.2, 2.2]), np.array([1, 2]))}, |f(x) - Taylor f(x)| = {np.abs(F([1, 2]) - Taylor(np.array([1.2, 2.2]), np.array([1, 2])))}')
    print(f'x = [1.1, 2.1], f(x) = {F([1, 2.1])}, Taylor f(x) = {Taylor(np.array([1.1, 2.1]), np.array([1, 2]))}, |f(x) - Taylor f(x)| = {np.abs(F([1, 2]) - Taylor(np.array([1.1, 2.1]), np.array([1, 2])))}')
    print(f'x = [1, 2], f(x) = {F([1, 2])}, Taylor f(x) = {Taylor(np.array([1, 2]), np.array([1, 2]))}, |f(x) - Taylor f(x)| = {np.abs(F([1, 2]) - Taylor(np.array([1, 2]), np.array([1, 2])))}')
    print(f'x = [0.9, 1.9], f(x) = {F([1, 1.9])}, Taylor f(x) = {Taylor(np.array([0.9, 1.9]), np.array([1, 2]))}, |f(x) - Taylor f(x)| = {np.abs(F([1, 2]) - Taylor(np.array([0.9, 1.9]), np.array([1, 2])))}')
    print(f'x = [0.8, 1.8], f(x) = {F([1, 1.8])}, Taylor f(x) = {Taylor(np.array([0.8, 1.8]), np.array([1, 2]))}, |f(x) - Taylor f(x)| = {np.abs(F([1, 2]) - Taylor(np.array([0.8, 1.8]), np.array([1, 2])))}')

RPTA: 
位 py aproximacion_taylor.py
grad([1, 2]) =  [-1.71828183  2.71828183]
F([1, 2]) =  3.718281828459045
Taylor([1, 2], [1, 2]) =  3.718281828459045
x = [1.2, 2.2], f(x) = 4.320116922736548, Taylor f(x) = 3.9182818284590457, |f(x) - Taylor f(x)| = 0.20000000000000062
x = [1.1, 2.1], f(x) = 4.004166023946434, Taylor f(x) = 3.818281828459045, |f(x) - Taylor f(x)| = 0.10000000000000009
x = [1, 2], f(x) = 3.718281828459045, Taylor f(x) = 3.718281828459045, |f(x) - Taylor f(x)| = 0.0
x = [0.9, 1.9], f(x) = 3.4596031111569494, Taylor f(x) = 3.618281828459045, |f(x) - Taylor f(x)| = 0.10000000000000009
x = [0.8, 1.8], f(x) = 3.225540928492468, Taylor f(x) = 3.5182818284590454, |f(x) - Taylor f(x)| = 0.19999999999999973

Ejercicio 2:

import matplotlib.pyplot as plt
import numpy as np

#%matlplotlib inline
def gradF(z):
    return 2*z - 3*z**2

def f(x):
    return x**2 - x**3 

def tf(x, z):
   return f(z) + gradF(z)*(x - z)

fig, ax = plt.subplots(1,1,figsize=(7,7), dpi=120)
x = np.linspace(0, 1, 1000)
taf = lambda x:tf(x, 0.4)

ax.plot(x,f(x), label='original')
ax.plot(x,taf(x), label='aproximaciones')
ax.scatter(0.4,f(0.4),color='red')
ax.legend()
plt.show()

print(f'x = 0.3, f(x) = {f(0.3)}, Taylor f(x) = {taf(0.3)}, |f(x) - Taylor f(x)| = {np.abs(f(0.3) - taf(0.3))}')
print(f'x = 0.35, f(x) = {f(0.35)}, Taylor f(x) = {taf(0.35)}, |f(x) - Taylor f(x)| = {np.abs(f(0.35) - taf(0.35))}')
print(f'x = 0.4, f(x) = {f(0.4)}, Taylor f(x) = {taf(0.4)}, |f(x) - Taylor f(x)| = {np.abs(f(0.4) - taf(0.4))}')
print(f'x = 0.45, f(x) = {f(0.45)}, Taylor f(x) = {taf(0.45)}, |f(x) - Taylor f(x)| = {np.abs(f(0.45) - taf(0.45))}')
print(f'x = 0.5, f(x) = {f(0.5)}, Taylor f(x) = {taf(0.5)}, |f(x) - Taylor f(x)| = {np.abs(f(0.5) - taf(0.5))}')
print(f'x = 0.6, f(x) = {f(0.6)}, Taylor f(x) = {taf(0.6)}, |f(x) - Taylor f(x)| = {np.abs(f(0.6) - taf(0.6))}')

RPTA

位 py aproximacion_taylor3.py
x = 0.3, f(x) = 0.063, Taylor f(x) = 0.064, |f(x) - Taylor f(x)| = 0.0010000000000000009
x = 0.35, f(x) = 0.079625, Taylor f(x) = 0.08, |f(x) - Taylor f(x)| = 0.00037500000000000033
x = 0.4, f(x) = 0.09600000000000002, Taylor f(x) = 0.09600000000000002, |f(x) - Taylor f(x)| = 0.0
x = 0.45, f(x) = 0.111375, Taylor f(x) = 0.11200000000000002, |f(x) - Taylor f(x)| = 0.0006250000000000144
x = 0.5, f(x) = 0.125, Taylor f(x) = 0.128, |f(x) - Taylor f(x)| = 0.0030000000000000027
x = 0.6, f(x) = 0.14400000000000002, Taylor f(x) = 0.15999999999999998, |f(x) - Taylor f(x)| = 0.01599999999999996

Ejercicio 2


def gradF(z):
    return 2*z - 3*z**2

def F(x):
    return x**2 - x**3 

if __name__ == '__main__':

    z = 0.4

    print(f'F(0.4) = {F(z)}')
    print(f'grad(0.4) = {gradF(z)}')
    print(f'F(x) = {F(z)} + {gradF(z)}*(x - {z})')

RPTA:

0.09600000000000002 + 0.31999999999999995*(x - 0.4)

C:\Users\USER\Documents\Platzi\Matematica
位 py aproximacion_taylor2.py
F(0.4) = 0.09600000000000002
grad(0.4) = 0.31999999999999995
F(x) = 0.09600000000000002 + 0.31999999999999995*(x - 0.4)

Taylor Ejercicio 1

import numpy as np 

def gradF(z):
    return np.array([1-np.exp(z[1]-z[0]), np.exp(z[1]-z[0])])

def F(x):
    return x[0] + np.exp(x[1]-x[0])

def Taylor(x, z):
    fz = F(z)
    gz = gradF(z)
    return fz + [email protected](x - z)

if __name__ == '__main__':

    z = np.array([1, 2])

    print('grad([1, 2]) = ', gradF(z))
    print('F([1, 2]) = ', F(z))
    print('Taylor([1, 2], [1, 2]) = ', Taylor(np.array([1, 2]), np.array([1, 2])))


Yo prob茅 diferentes valores con el siguiente c贸digo:

valores =[(1,2),(2,2),(2,4),(3,4),(4,5),(5,5),(5,4),(6,5),(5,6)]
for tupla in valores:
  x = np.array([tupla[0],tupla[1]])
  print(f' x = {x} f(x) ={F(x)}   Taylor f(x) = {taylor_F(x,z)}   |f(x) - Taylor f(x)| = {np.abs(F(x) - taylor_F(x,z))}')

y obtuve estos resultados:
x = [1 2] f(x) =3.718281828459045 Taylor f(x) = 3.718281828459045 |f(x) - Taylor f(x)| = 0.0
x = [2 2] f(x) =3.0 Taylor f(x) = 2.0 |f(x) - Taylor f(x)| = 1.0
x = [2 4] f(x) =9.38905609893065 Taylor f(x) = 7.43656365691809 |f(x) - Taylor f(x)| = 1.9524924420125602
x = [3 4] f(x) =5.718281828459045 Taylor f(x) = 5.718281828459045 |f(x) - Taylor f(x)| = 0.0
x = [4 5] f(x) =6.718281828459045 Taylor f(x) = 6.718281828459045 |f(x) - Taylor f(x)| = 0.0
x = [5 5] f(x) =6.0 Taylor f(x) = 5.0 |f(x) - Taylor f(x)| = 1.0
x = [5 4] f(x) =5.367879441171443 Taylor f(x) = 2.281718171540955 |f(x) - Taylor f(x)| = 3.086161269630488
x = [6 5] f(x) =6.367879441171443 Taylor f(x) = 3.2817181715409554 |f(x) - Taylor f(x)| = 3.0861612696304874
x = [5 6] f(x) =7.718281828459045 Taylor f(x) = 7.718281828459045 |f(x) - Taylor f(x)| = 0.0