简体   繁体   English

Ax = b,其中b取决于x

[英]Ax=b with b dependent on x

I understand how to solve Ax=b, but what if b is dependent on x? 我知道如何求解Ax = b,但是如果b依赖于x怎么办? See in the pictures E3 = function(E4). 如图所示E3 = function(E4)。 I guess this is done iteratively.. what is such a problem called? 我猜这是迭代完成的..这样的问题叫什么? what methods do I use to solve it? 我用什么方法解决呢?

I am trying to solve the following system: 我正在尝试解决以下系统:

系统草图

Giving the following set of equations: 给出以下等式:

方程组

Leading to the following matrix: 导致以下矩阵:

矩阵

Update: As requested some more information: 更新:根据要求提供更多信息:

A = ([
[-1, 0, 0, 0, 0],
[1, -1, -1, 0, 0],
[0, 1, 0, -1, 0],
[0, 0, 1, 1, -1],
[0, 0, 1, 0, 0]
])

b = [-10, 0, 1, 1, 3]

print(np.linalg.solve(A, b))

-> [ 10.   7.   3.   6.   8.]

This works, but what if: 这有效,但是如果:

b = [-10, 0, 1, 1, some_function(x[3])]

So E3 = some_function(E4), thus E3 depends on E4, defined by 'some_function' (non-linear) 因此E3 = some_function(E4),因此E3取决于E4,由“ some_function”(非线性)定义

Yeah, you can solve this with a nonlinear optimization. 是的,您可以使用非线性优化来解决此问题。 scipy.optimize has all the juicy details but here's an example that solves your system assuming some_function(x) is x ** 2 : scipy.optimize包含所有多汁的细节,但这是一个假设some_function(x)x ** 2系统示例:

import numpy as np
import scipy.optimize as opt

A = np.array([
    [-1, 0, 0, 0, 0],
    [1, -1, -1, 0, 0],
    [0, 1, 0, -1, 0],
    [0, 0, 1, 1, -1],
    [0, 0, 1, 0, 0.0]
    ])
b = np.array([-10, 0, 1, 1, 3.0]) # last value is fake

def objectiveFunction(x):
    bNew = b.copy()
    bNew[-1] = x[3] ** 2 # "some_function = lambda x: x**2"
    error = np.dot(A, x) - bNew
    return np.dot(error, error)

solution = opt.minimize(objectiveFunction, np.zeros((5)))

print(solution)

All optimization techniques are basically function minimizations. 所有优化技术基本上都是功能最小化。 You give the optimizer 优化器

  1. a function to minimize (and which takes one vector input argument and returns a scalar number) and 一个使函数最小化的函数(采用一个向量输入参数并返回一个标量数),并且
  2. an initial guess as to which input vector produces the smallest scalar. 关于哪个输入向量产生最小标量的初步猜测。

The optimizer returns the input to that function that produces the smallest output. 优化器输入返回到产生最小输出的函数。

The objectiveFunction function above is being minimized, and it returns the error between A . x - b 上面的objectiveFunction函数被最小化,它返回A . x - b之间的错误A . x - b A . x - b where x is a candidate solution and where b has the form that depends on x . A . x - b其中x是候选解决方案,并且b具有取决于x的形式。

You can get caught in local minima so using optimization methods is a bit of a black art. 您可能会陷入局部最小值,因此使用优化方法有点不明智。 But this case seems pretty straightforward: the code above prints out the following: 但是这种情况似乎很简单:上面的代码打印出以下内容:

      fun: 1.3591186209050682e-11
 hess_inv: array([[ 0.49698215,  0.08279412,  0.40828859,  0.08067816,  0.47743665],
       [ 0.08279412,  0.39205925, -0.22445874, -0.02791535, -0.26595691],
       [ 0.40828859, -0.22445874,  1.01438679,  0.18492456,  1.19990433],
       [ 0.08067816, -0.02791535,  0.18492456,  0.05283296,  0.23785942],
       [ 0.47743665, -0.26595691,  1.19990433,  0.23785942,  1.94819504]])
      jac: array([ -5.37158676e-06,   4.82585577e-06,   7.97108114e-06,
        -6.31780565e-06,   4.76041890e-07])
  message: 'Optimization terminated successfully.'
     nfev: 105
      nit: 13
     njev: 15
   status: 0
  success: True
        x: array([ 10.00000068,   3.54138098,   6.45862309,   2.54138196,   8.00000528])

which is a lot of info but the important bit is the x and fun values: note how x is a vector and fun is a scalar. 其中包含大量信息,但重要的一点是xfun值:请注意x是矢量, fun是标量。 This means that objectiveFunction(solution.x) == solution.fun . 这意味着objectiveFunction(solution.x) == solution.fun This in turn means that the answer b you're looking for (given my assumed some_function ) is solution.x and you can have confidence that this is right because solution.fun (the error between A . x and b ) is close to zero. 反过来,这意味着您正在寻找的答案b (鉴于我假设的some_function )为solution.x并且您可以确信这是正确的,因为solution.funA . xb之间的误差)接近于零。 。

I skipped over a bunch of explanation, but I can elaborate if you need. 我跳过了很多解释,但是如果您需要,我可以详细说明。

If your b(x) is some nonlinear function of x , it doesn't really matter much that on the left hand side you have A*x . 如果b(x)是一些非线性函数x ,它并不真正的问题太多,在左手边你有A*x The simplest way of expression the equation is A*x - b(x)=0 , in other words F(x) = 0 , the general nonlinear equation. 该方程式的最简单表达方式是A*x - b(x)=0 ,换句话说, F(x) = 0 ,这是一般的非线性方程。 Before even trying to solve this, be aware of some of the nasty consequences: 在尝试解决此问题之前,请注意一些令人讨厌的后果:

  • In general, you can't say anything about the distribution of solutions. 通常,您不能说解决方案的分布。 Is there one at all? 有没有一个? Impossible to say without a more detailed analysis. 无需详细分析就可以说。 Perhaps there are a few, or infinitely many? 也许有几个,或无限多个? With linear equation systems ( A*x = b ), all the info is in the matrix, but not so with nonlinear equations. 对于线性方程组( A*x = b ),所有信息都在矩阵中,而对于非线性方程组则不是。

  • Since the nonlinear solver cannot make assumptions about the structure of the solution landscape, no solver is guaranteed to converge. 由于非线性求解器无法对求解景观的结构进行假设,因此不能保证求解器会收敛。 In fact, all non-esoteric solvers are only local, ie, you provide an initial guess that's "close" to a solution, and the solver will converge that guess towards it. 实际上,所有非深奥的求解器都只是局部的,即,您提供的初始猜测与解决方案“很接近”,求解器将把该猜测收敛到解决方案。 To guarantee convergence, you already have to have a good idea of the solution before you start. 为了确保收敛,在开始之前,您已经对解决方案有了一个很好的了解。 In practice, many people just take random guesses and let the local solver do a number steps, keeping their fingers crossed. 在实践中,许多人只是随机猜测,然后让本地求解器执行一些步骤,使手指交叉。

  • Arguably, the most popular local solver is Newton's method; 可以说,最受欢迎的局部求解器是牛顿法。 it's the only solver that achieves quadratic convergence (if you're already close to a solution). 它是唯一实现二次收敛的求解器(如果您已经接近解决方案)。 In each step, you need to solve a linear equation system with the Jacobian, namely J*d = -F(x) . 在每一步中,您都需要用Jacobian求解线性方程组,即J*d = -F(x) That alone can be quite costly if you don't do it carefully. 如果您不仔细做的话,那可能会很昂贵。

Now that you know all that, you can play around with scipy optimize . 现在您已经知道了所有的知识,可以开始使用scipy Optimize了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM