将 Sympy 派生方程转换为与 scipy.optimize.newton 兼容以解决多变量问题
我提到了Error using 'exp' in sympy -TypeError 和 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)
我似乎无法让牛顿的方法起作用。任何信息或资源表示赞赏。