简体   繁体   English

我的 function 没有迭代循环,我找不到问题

[英]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.

相关问题 如何在Python的for循环中将要迭代的行传递到sort / sorted函数中? - How can I pass the line I'm iterating over into a sort/sorted function in a for loop in Python? 使用 for 循环迭代数据帧行 - .loc 的问题 - Iterating over dataframe rows with for loop - problem with .loc 为什么我不能更改要迭代的集合? - Why can't I change a set I'm iterating over? 我如何使用 Tkinter 从 python 中的另一个文件访问 function。 我找不到任何解决我的具体问题的方法 - How do i access a function from another file in python with Tkinter. I can't find any solution to my specific problem 为什么我们不能在迭代列表时在 for 循环周围使用括号? - Why can't we use parentheses around a for loop while iterating over a list? 迭代字符串和浮点数以在字典中找到最小值的问题 - Problem of iterating over string and float to find minimum value in dictionary 我可以停止 for 循环继续遍历我正在迭代的数组的最后一个索引吗? - Can I stop the for loop from continuing on the last index of the array I am iterating over? 我找不到任何问题,但它不工作 - i can't find any problem but it is not working 我有一个属性错误,我找不到问题的答案 - I have an attribute error and i can't find the answer to my problem 在 for 循环中迭代字典 - iterating over dictionaries in for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM