python解决方程组问题_在Python中解决非线性方程组(scipy.optimize.fsolve)

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

which should have only one solution (x=3.5, y=1.75).

My current approach using the scipy stack is the following:

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)))

and yields the following (which is not the result):

(0.0, 0.0)

I have already tried different starting estimates but it doesn't deliver the right solution.

What's wrong with my approach? Am I missing something?

Thanks in advance!

解决方案

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)) ?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值