В следующем коде я реализовал метод Ньютона в Python.
import math
def Newton(f, dfdx, x, eps):
f_value = f(x)
iteration_counter = 0
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
except ZeroDivisionError:
print ("Error! - derivative zero for x = ", x)
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
# Here, either a solution is found, or too many iterations
if abs(f_value) > eps:
iteration_counter = -1
return x, iteration_counter
def f(x):
return (math.cos(x)-math.sin(x))
def dfdx(x):
return (-math.sin(x)-math.cos(x))
solution, no_iterations = Newton(f, dfdx, x=1, eps=1.0e-14)
if no_iterations > 0: # Solution found
print ("Number of function calls: %d" % (1 + 2*no_iterations))
print ("A solution is: %f" % (solution))
else:
print ("Solution not found!")
Однако теперь я собираюсь построить диаграмму сходимости на том же интервале. Это будет абсолютная ошибка как функция количества итераций.
Я попытался создать итерацию, которая каждый раз выдает 2-кортеж с абсолютной ошибкой и итерацией. Вот мой код ниже, с ошибкой, которую я получаю от него также,
import math
def Newton(f, dfdx, x, eps):
f_value = f(x)
iteration_counter = 0
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
yield interation_counter, abs(f(x))
except ZeroDivisionError:
print ("Error! - derivative zero for x = ", x)
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
# Here, either a solution is found, or too many iterations
if abs(f_value) > eps:
iteration_counter = -1
def f(x):
(math.cos(x)-math.sin(x))
def dfdx(x):
(-math.sin(x)-math.cos(x))
С этим я попытался поместить результат в массив, чтобы я мог отобразить свои результаты
import numpy as np
np.array(list(Newton(f,dfdx, 1,10e-4)))
Однако я получаю следующую ошибку:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-9378f4e2dbe3> in <module>()
1 import numpy as np
----> 2 np.array(list(Newton(f,dfdx, 1,10e-4)))
<ipython-input-19-40b67c2c3121> in Newton(f, dfdx, x, eps)
4 f_value = f(x)
5 iteration_counter = 0
----> 6 while abs(f_value) > eps and iteration_counter < 100:
7 try:
8 x = x - float(f_value)/dfdx(x)
TypeError: bad operand type for abs(): 'NoneType'