Python Diferencia hacia atrás
import numpy as np
# Define x, y, and derived y
x = np.linspace(0.78, 0.79, 100)
y = np.sin(x)
dy = np.cos(x)
# Init variable to store backward diff calculation
dyb = [0.0] * len(x)
# set first element by forward difference
dyb[0] = (y[0] - y[1]) / (x[0] - x[1])
# Calculate backward diff
for i in range(1,len(y)):
dyb[i] = (y[i] - y[i-1]) / (x[i]-x[i-1])
# Plot result
plt.figure(figsize = (12,8))
plt.plot(x, dy, label='Exact')
plt.plot(x, dyb, '--', label='backward')
plt.legend()
plt.show()
OwO People