用矩阵方程优化.root [英] optimize.root with a matrix equation

查看:67
本文介绍了用矩阵方程优化.root的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 optimize.root 解决以下线性系统

I am trying to solve the following linear system using optimize.root

AX = b

使用以下代码.

A = [[0,1,0],[2,1,0],[1,4,1]]

def foo(X):
   b = np.matrix([2,1,1])
   out = np.dot(A,X) - b
   return out.tolist()

sol = scipy.optimize.root(foo,[0,0,0])

我知道我可以简单地使用 numpy.linalg.solve 来轻松地做到这一点.但我实际上是在尝试解决矩阵形式的非线性系统.请参阅我的问题此处.所以我需要找到一种方法来使这种方法起作用.为此,我试图在这个简单的案例中解决这个问题.但我收到错误

I know that I can simply use the numpy.linalg.solve to do this easily. But I am actually trying to solve a non linear system that is in matrix form. See my question here. So I need to find a way to make this method work. To do that I am trying to solve this problem in this simple case. But I get the error

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'foo'.Shape should be (3,) but it is (1, 3).

从我从其他类似的 stackoverflow 问题中读到的,这是因为 foo 函数的输出与初始猜测的形状不兼容 [0,0,0]

From what I have read from other similar stackoverflow questions this happens because the out put of the foo function is not compatible with the shape of the initial guess [0,0,0]

当然有一种方法可以使用 scipy.optimize.root 来解决这个方程.有人可以帮忙吗?

Surely there is a way to solve this equation using scipy.optimize.root. Can anyone please help?

推荐答案

(我假设 .dot 中的大写 B 的拼写错误A.)

(I'm assuming the capital B in your .dot is a typo for A.)

尝试将 np.array 用于 b.np.matrix 创建一个行向量",即形状 (1, 3) 而您的初始猜测形状为 (3,).

Try using np.array for b. np.matrix creates a "row vector", i.e. shape (1, 3) whereas your initial guess has shape (3,).

这篇关于用矩阵方程优化.root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆