Solve a system of non-linear equations in Python (scipy.optimize.fsolve)

前端 未结 1 398
醉梦人生
醉梦人生 2020-12-07 04:50

I am trying to solve the following simple system of non-linear equations (Source(second example)):

(I) y - x^2 = 7 - 5x
(II) 4y - 8x = -21

相关标签:
1条回答
  • 2020-12-07 05:29

    This works perfectly fine:

    In [1]: %paste
    from scipy.optimize import fsolve
    
    def equations(p):
        x, y = p
        return (y - x**2 -7 + 5*x, 4*y - 8*x + 21)
    
    x, y =  fsolve(equations, (5, 5))
    
    print(equations((x, y)))
    
    ## -- End pasted text --
    (0.0, 0.0)
    
    In [2]: x
    Out[2]: 3.5000000414181831
    
    In [3]: y
    Out[3]: 1.7500000828363667
    

    equations(x, y) being (0, 0) means that both y - x**2 -7 + 5*x and 4*y - 8*x + 21 are 0.

    Maybe you got confused and meant to print(x, y) instead of print(equations(x, y)) ?

    0 讨论(0)
提交回复
热议问题