Esercizio: scrivere un programma che calcoli numericamente
la derivata della funzione
(
)
e valutare l'effetto di
.
Un esempio di soluzione può essere
def deriv(f,x,h=0.01):
''' derivative of function f(x)'''
return ( f(x+h/2.0) - f(x-h/2.0) )/h
if __name__ == '__main__':
import math
m,M=(0.0,math.pi)
N=20 # 20 examples
step=(M-m)/N
f=math.sin
df=math.cos
x=m
while x <=M:
print x,df(x),deriv(f,x)
x = x + step