Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python finite difference approximation backward difference

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()
Comment

PREVIOUS NEXT
Code Example
Python :: Python program to print odd numbers in a list 
Python :: python [a]*b means [a,a,...b times] v2 
Python :: boolean python meaning for idiots 
Python :: how to find duplicate numbers in list in python 
Python :: list methods python 
Python :: from matrix to array python 
Python :: __name__== __main__ in python 
Python :: python loop break on keypress 
Python :: python write 
Python :: access-control-allow-origin django 
Python :: python replace part in large file 
Python :: read only the first line python 
Python :: create a new file in python 3 
Python :: python clear screen windows and linux 
Python :: write number of lines in file python 
Python :: how to import matplotlib.pyplo in python 
Python :: show all rows python 
Python :: python transform two columns to a list combine 
Python :: round list of floats python 
Python :: python get nth letter of alphabet 
Python :: remove outliers numpy array 
Python :: select all columns except one pandas 
Python :: parse first characters from string python 
Python :: python string cut substring 
Python :: ValueError: Shapes (None, 1) and (None, 11) are incompatible keras 
Python :: python df select first x columns 
Python :: dataframe print column comma separated 
Python :: two loop type python 
Python :: export a dataframe to excel pandas 
Python :: Django - include app urls 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =