Search code examples
pythonscipysympynewtons-methodscipy-optimize

Multivariate Newton's method for equations derived using Sympy


Converting Sympy derived equations to be compatible with scipy.optimize.newton for multivariate problem

I referred to Error using 'exp' in sympy -TypeError and Attribute Error is displayed. I found out that using lambdify will make the symbolic equations compatible with other packages such as numpy and scipy.

This is a continuation of my previous post: 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)

I can't seem to get the Newton's method working. Any information or resources is appreciated.


Solution

  • Instead of defining symbols, we can use the 'DeferredVector' in sympy to define the symbols. Instead of

    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"
    

    do the following: x = DeferredVector('x') f = -(-n +sum([sym.log(x[0]x[1](num**(c-1)) for num in data]))

    Now use diff w.r.t x[0] and x[1] and run the scipy.optimize.newton().