我正在用Python编写一个反向Euler方法,在编写Newton部分时遇到问题。我们得到了1e-4的公差,利用这个,我得到了牛顿法的输出向量中非常小的数字。在
这是我的代码:def Newt(U,x,tol): #takes in matrix, x vector, tolerance
error=np.matrix([[1],[1]]) #set an error matrix of 1,1
while abs(LA.norm(error))>tol:
func=function(U,x) #define a f(x) vector
jac=functionprime(U,x) #define the inverse jacobian vector
y0=jac*func #the change vector is the jacobian inverse times the function
xn=x-y0 #the new x is the difference
error=xn-x #set the error
x=xn
print(x)
我使用这些函数来解决这个问题:
^{pr2}$
当我运行它时,我得到一个矩阵:[[ 4.31664687e-27],
[ 2.15832344e-27]]
它太小了,不能用在向后的欧拉函数中,这让我觉得牛顿出了问题。有谁能帮我弄清楚我做错了什么吗?从我对牛顿函数的理解来看,这似乎是正确的,但很明显,它并不是我所需要的。在
另外,要在代码顶部运行此函数,我需要:import matplotlib.pyplot as plt
import math
import numpy as np
from pylab import *
from numpy import linalg as LA
不是所有这些都需要,但有些是!在