[英]My function is not iterating over a loop and I can't find the problem
I'm trying to create a Function that does the Newton-Raphson root finding for multiple variables (dx = -Jinv^-1 * F), but it won't iterate over a loop continuously (ie output won't become the next input for next iteration).我正在尝试创建一个 Function,它对多个变量 (dx = -Jinv^-1 * F) 进行牛顿-拉夫森根查找,但它不会连续迭代循环(即 output 不会成为下一个下一次迭代的输入)。 Does anyone see the issue?有人看到这个问题吗?
PS.附言。 I'm trying to code it without using any of the built in matrix functions so those solutions won't apply here.我试图在不使用任何内置矩阵函数的情况下对其进行编码,因此这些解决方案不适用于此处。
# Multivariable Root Finding via Newton Raphson
# import necessary functions
import numpy as np
# define array function for f1 and f2
x = np.arange(-3,3,.01)
def f1(x):
return 2*(x[0]**3) - 6*(x[0]*(x[1]**2)) - 1
def f2(x):
return -2*(x[1]**3) + 6*(x[1]*(x[0]**2)) + 3
# define matrix F(x) 2x1
def F(x_vec):
return [f1(x_vec),f2(x_vec)]
#print(F([2,3]))
# Create Inverse Jacobian 2x2
# InvJacobian Upper Left
def JUL(x):
return (6*x[0]**2 - 6*x[1]**2)/((6*x[0]**2 - 6*x[1]**2)**2 + 144*(x[0]**2)*(x[1]**2))
# InvJacobian Upper Right
def JUR(x):
return (12*x[0]*x[1])/((6*x[0]**2 - 6*x[1]**2)**2 + 144*(x[0]**2)*(x[1]**2))
# InvJacobian Lower Left
def JLL(x):
return (-12*x[0]*x[1])/((6*x[0]**2 - 6*x[1]**2)**2 + 144*(x[0]**2)*(x[1]**2))
# InvJacobian Lower Left
def JLR(x):
return (6*x[0]**2 - 6*x[1]**2)/((6*x[0]**2 - 6*x[1]**2)**2 + 144*(x[0]**2)*(x[1]**2))
# Combine all Jacobian into Matrix
def Jinv(x_vec):
return [JUL(x_vec),JUR(x_vec),JLL(x_vec),JLR(x_vec)]
# Create Newton Raphson Functon
def rf_newton2d(F_system, Jinv_system, x_vec0, tol, maxiter):
# let ou be the starting array x_vec0
ou = x_vec0
i = 0
err = 10**-4
# F*Jinv should output a 2x1 matrix
while err > tol and i <= maxiter:
F_system = F(ou)
Jinv_system = Jinv(ou)
# w is the [0] position of the 2x1 output matrix
w = F_system[0]*Jinv_system[0] + F_system[1]*Jinv_system[1]
# z is the [1] position of the 2x1 output
z = F_system[0]*Jinv_system[2] + F_system[1]*Jinv_system[3]
# u is the output matrix of F*Jinv
u = [w,z]
# out is the new array involving u + ou
out = [ou[0] + u[0],ou[1] +u[1]]
#define nu as out
nu = out
# let nu be the ou for the next iteration
ou = nu
#run continuous iterations
i += 1
return nu
print(rf_newton2d(F([x_vec0]),Jinv([x_vec0]),[2,3],10**-5, 5))
return
statements in a function immediately stop the function and continue running the main code from where the function was called. function 中的return
语句立即停止 function 并从调用 function 的地方继续运行主代码。 It looks to me like the return
statement in the while
loop is not supposed to be in the while
loop.在我看来, while
循环中的return
语句不应该在while
循环中。
In your final line you have the function F([x_vec0])
, this means within the function F
the variable x_vec = [x_vec0]
.在您的最后一行中,您有 function F([x_vec0])
,这意味着在 function F
中变量x_vec = [x_vec0]
。 You then pass this variable to the function f1
, so within f1
, x = [x_vec0]
.然后将此变量传递给 function f1
,因此在f1
内, x = [x_vec0]
。 Within x[1]
can be found within f1
.在x[1]
内可以在f1
内找到。 The problem is that x
will never have a second entry.问题是x
永远不会有第二个条目。 If x_vec0 = [0,1]
(or some other array object such as an np.ndarray
), then [x_vec0] = [[0,1]]
, ie [x_vec0][0] = [0,1]
and [x_vec0][1]
does not exist.如果x_vec0 = [0,1]
(或其他一些数组 object,例如np.ndarray
),则[x_vec0] = [[0,1]]
,即[x_vec0][0] = [0,1]
和[x_vec0][1]
不存在。
I got all the information from the traceback (obviously I had to invent a value for x_vec0
):我从回溯中获得了所有信息(显然我必须为x_vec0
发明一个值):
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-4-d57a9d10dd94> in <module>
80 return nu
81 x_vec0 = np.array([1,2])
---> 82 print(rf_newton2d(F([x_vec0]),Jinv([x_vec0]),[2,3],10**-5, 5))
<ipython-input-4-d57a9d10dd94> in F(x_vec)
16 # define matrix F(x) 2x1
17 def F(x_vec):
---> 18 return [f1(x_vec),f2(x_vec)]
19
20 #print(F([2,3]))
<ipython-input-4-d57a9d10dd94> in f1(x)
9
10 def f1(x):
---> 11 return 2*(x[0]**3) - 6*(x[0]*(x[1]**2)) - 1
12
13 def f2(x):
IndexError: list index out of range
Without knowing the maths of Newton-Raphson roots I can't suggest what the fix is.在不知道 Newton-Raphson 根的数学知识的情况下,我无法提出解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.