created Script for Sekantenverfahren

This commit is contained in:
schrom01 2022-11-03 11:34:51 +01:00
parent 3367c775b9
commit 213f60594a
1 changed files with 8 additions and 6 deletions

View File

@ -12,11 +12,14 @@ def f_diff(x):
return 2 * x * math.pow(math.pow(math.e, x), 2) - 3 * math.pow(x, -4)
def newtonStep(x0, function, function_diff):
return x0 - (function(x0)) / (function_diff(x0))
def newtonStep(xn, function, function_diff):
return xn - (function(xn)) / (function_diff(xn))
def simpleNewtonStep(x0, function, function_diff_x0):
return x0 - ((function(x0)) / (function_diff_x0))
def simpleNewtonStep(xn, function, function_diff_x0):
return xn - ((function(xn)) / (function_diff_x0))
def secantStep(xn, xn_1, function):
return xn - (xn - xn_1)/(function(xn) - function(xn_1)) * f(xn)
def newton(x0, iterations, function, function_diff):
print("Newton Verfahren:")
@ -24,14 +27,13 @@ def newton(x0, iterations, function, function_diff):
x.append(x0)
print("x" + str(0) + ":", str(x[0]))
for i in range(iterations):
x.append(newtonStep(x0=x[i], function=function, function_diff=function_diff))
x.append(newtonStep(xn=x[i], function=function, function_diff=function_diff))
print("x" + str(i + 1) + ":", str(x[i + 1]))
if __name__ == '__main__':
newton(x0=2, iterations=4, function=f, function_diff=f_diff)
simpleNewton(x0=0.5, iterations=4, function=f, function_diff=f_diff)