The first part of the code works fine and prints off values for sin
in increments of .01
radians. Between 0-25
(degrees) that is 2500 values. The second part of the code is trying to evaluate sin(x)
using the central difference method, while applying the same incremental steps.
This second part returns just one number. It doesn't generate the incremental list. This is my first attempt trying to code a differential; what am I doing wrong?
#1 evaluation of sin
import numpy as np
import math
def sin(x):
return math.sin(math.radians(x))
for i in np.arange(0, 25, .01):
print sin(i)
#Evaluation of sin(x) using CDM, which would yield cos
def dfdxsin(f, a, b, n):
h=float(b-a)/n
dfdx=0.0
for i in np.arange(0,25,.01):
dfdx += (f(a+h)+i)-f((a-h)+i)/(2*h)
return dfdx
def f(x):
return math.sin(math.radians(x))
print dfdxsin(f,0,25,2500)