牛顿法python代码_python中的二维牛顿法

我正在用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

不是所有这些都需要,但有些是!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值