我能够找到牛顿方法的几种实现,例如this link或maybe this one。
但是,大多数情况下,示例都具有简单的功能,例如:
x ^ 2-9 = 0或x ^ 3-x ^ 2-1 = 0。我正在寻找适合的东西:
我的问题是我不知道如何使用此代码来解决我的问题。例如,我不确定如何将导数(dfdx)应用到包含矩阵的F(x)上。另外,如果我应该直接在“ def f(x)”上输入矩阵
我正在使用的代码:
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 x**2 - 9
def dfdx(x):
return 2*x
solution, no_iterations = Newton(f, dfdx, x=1000, eps=1.0e-6)
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!"
答案 0 :(得分:0)
没有任何特殊的矩阵派生规则-派生仅针对每个元素进行计算。我建议在纸上评估$ [x1,x2]'* M * [x1,x2] $表达式以获得多项式矩阵,然后计算每一个的导数。