Nullstelle mit Regula Falsi bestimmen
Verfasst: Mittwoch 14. April 2010, 23:18
Hier eine Funktion, der man zwei Startwerte, die Anzahl der maximalen Näherungsschritte und eine Funktion zum Berechnen der y Werte übergibt.
Code: Alles auswählen
# -*- coding: cp1252 -*-
#Rekursive Approximationsfunktions
def app(x1, x2, count, funktion):
#Count wird als Zähler der maximalen Approximationsschritte genutzt
if x1 == x2:
return x1
elif funktion(x1) == funktion(x2):
return x1
#Nächster Näherungswert
x3 = x2 - ( float(x2 - x1) / float(funktion(x2) - funktion(x1) ) ) * funktion(x2)
if count == 0:
return x3
return app(x2, x3, count - 1, funktion)
if __name__ == "__main__":
print app(3, 4, 10, lambda x: x**2 - 3*x - 3)