【问题标题】:How to calculate the error in fsolve python like resnorm in python?如何计算 fsolve python 中的错误,如 python 中的 resnorm?
【发布时间】:2021-08-31 03:08:21
【问题描述】:

我需要计算每次迭代中的误差,以在 python 中使用 fsolve 求解非线性方程组,例如 MATLAB 中的 resnormfsolve。如果在 python 中可能,有人可以帮助我吗?

【问题讨论】:

  • 请分享您到目前为止所尝试的内容
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python nonlinear-optimization convergence


【解决方案1】:

这可以通过查看docs 轻松实现。

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

# Use the function from the docs
def func(x):
    return [x[0] * np.cos(x[1]) - 4,
            x[1] * x[0] - x[1] - 5]

roots_dict = fsolve(func, x0 = [1,1], full_output = True)

# calculate the square sum of the residuals:
res = np.sum(roots_dict[1]['fvec'] ** 2)

残差的平方和为:

res
>> 2.754320890449926e-22

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多