İleri ve Geri Farklar Yöntemi İle Türev Hesabı
- October 11, 2012
from decimal import Decimal
from math import e
def turev(h,x):
return (f(x+h)-f(x))/h
fonk = input("Fonksiyonu giriniz: ")
calis = """def f(x):
return %s""" %fonk
exec(calis)
x = Decimal(input("x değeri: "))
for i in range(10,100,2):
h = Decimal(10**-i)
xy = turev(h,x)
xg = f(x)
print("Türevin Hesaplanan Değeri: ", xy, "; Gerçek Değer: 4","; Hata: ", xg-xy)
Bu kod ileri farklar ile türev alıyor. Geri farklar yöntemi ile almak istersek basitçe turev fonksiyonunun içerisindeki
(f(x+h)-f(x))/h
kısmını (f(x)-f(x-h))/h şeklinde değiştirmek yeterli olacaktır.