使用Sympy推导方程的多元牛顿法

时间:2019-05-20 20:52:52

标签: python scipy sympy newtons-method scipy-optimize

将Sympy导出的方程式转换为与scipy.optimize.newton兼容的多变量问题

我提到了Error using 'exp' in sympy -TypeError and Attribute Error is displayed。我发现使用lambdify将使符号方程与numpy和scipy等其他软件包兼容。

这是我上一篇文章的延续:Error using 'exp' in sympy -TypeError and Attribute Error is displayed

import numpy as np
import sympy as sym
import scipy.optimize
from sympy import symbols, diff, exp, log, power
from sympy.utilities.lambdify import lambdify

data = [3, 33, 146, 227, 342, 351, 353, 444, 556, 571, 709, 759, 836, 860, 968, 1056, 1726, 1846, 1872, 1986, 2311, 2366, 2608, 2676, 3098, 3278, 3288, 4434, 5034, 5049, 5085, 5089, 5089, 5097, 5324, 5389,5565, 5623, 6080, 6380, 6477, 6740, 7192, 7447, 7644, 7837, 7843, 7922, 8738, 10089, 10237, 10258, 10491, 10625, 10982, 11175, 11411, 11442, 11811, 12559, 12559, 12791, 13121, 13486, 14708, 15251, 15261, 15277, 15806, 16185, 16229, 16358, 17168, 17458, 17758, 18287, 18568, 18728, 19556, 20567, 21012, 21308, 23063, 24127, 25910, 26770, 27753, 28460, 28493, 29361, 30085, 32408, 35338, 36799, 37642, 37654, 37915, 39715, 40580, 42015, 42045, 42188, 42296, 42296, 45406, 46653, 47596, 48296, 49171, 49416, 50145, 52042, 52489, 52875, 53321, 53443, 54433, 55381, 56463, 56485, 56560, 57042, 62551, 62651, 62661, 63732, 64103, 64893, 71043, 74364, 75409, 76057, 81542, 82702, 84566, 88682]
n = len(data)
tn = data[n-1]


b, c = sym.symbols('b c', real=True)

f = -(-n +sum([sym.log(b*c*(num**(c-1))*sym.exp(-b*(num**c))) for num in data]))

bh = lambdify((b,c),diff(f,b),"numpy")
ch = lambdify((b,c),diff(f,c),"numpy")

sol = scipy.optimize.newton([bh,ch],(0.00404,1.0))
print(sol)

我似乎无法使牛顿方法起作用。任何信息或资源都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

根据scipy.optimize.newton,您的x0应该是标量,而不是数组或元组(这是您在代码中传递给scipy.optimize.newton()的内容)。幸运的是,scipy.optimize.fsolve也可以找到零位置并将数组接受为x0

据我所见,当您仅使用sympy时,使用numpy会使您的生活变得更艰难,这可以加快代码的速度(可以避免for循环)。以下示例向您展示了如何使用它(希望有帮助):

import warnings 
import numpy as np
import scipy.optimize as opt
# filter warnings
warnings.filterwarnings("ignore")

data = np.array([3, 33, 146, 227, 342, 351, 353, 444, 556, 571, 709, 759, 836, 860, 968,
        1056, 1726, 1846, 1872, 1986, 2311, 2366, 2608, 2676, 3098, 3278, 3288,
        4434, 5034, 5049, 5085, 5089, 5089, 5097, 5324, 5389,5565, 5623, 6080, 
        6380, 6477, 6740, 7192, 7447, 7644, 7837, 7843, 7922, 8738, 10089,
        10237, 10258, 10491, 10625, 10982, 11175, 11411, 11442, 11811, 12559, 
        12559, 12791, 13121, 13486, 14708, 15251, 15261, 15277, 15806, 16185,
        16229, 16358, 17168, 17458, 17758, 18287, 18568, 18728, 19556, 20567,
        21012, 21308, 23063, 24127, 25910, 26770, 27753, 28460, 28493, 29361,
        30085, 32408, 35338, 36799, 37642, 37654, 37915, 39715, 40580, 42015,
        42045, 42188, 42296, 42296, 45406, 46653, 47596, 48296, 49171, 49416,
        50145, 2042,  52489, 52875, 53321, 53443, 54433, 55381, 56463, 56485,
        56560, 57042, 62551, 62651, 62661, 63732, 64103, 64893, 71043, 74364,
        75409, 76057, 81542, 82702, 84566, 88682])

n    = len(data)
tn   = data[n-1]

f        = lambda b, c: n - np.sum(np.log(b * c * data**(c-1) * np.exp(-b * data**c)))
obj_func = lambda x   : np.array([f(x[0], x[1]) - x[0], f(x[0], x[1]) - x[1]])


x0  = np.array([0.00404, 1.0])
sol = opt.fsolve(obj_func, x0 = x0)
print('b, c = ', sol)

输出:

b, c =  [0.01737803 0.4300348 ]

答案 1 :(得分:0)

代替定义符号,我们可以在sympy中使用'DeferredVector'来定义符号。代替

b, c = sym.symbols('b c', real=True)
f = -(-n +sum([sym.log(b*c*(num**(c-1))*sym.exp(-b*(num**c))) for num in data]))
bh = lambdify((b,c),diff(f,b),"numpy")
ch = lambdify((b,c),diff(f,c),"numpy"

执行以下操作:     x = DeferredVector('x')     f =-(-n + sum([sym.log(x [0] x [1] (num **(c-1))for num in data]]))

现在使用diff w.r.t x [0]和x [1]并运行scipy.optimize.newton()。