scipy optimize.root_scalar() with multiple arguments each being a list

81 Views Asked by At
def  f(m_l, m_B, y):
    value = -1.0 + np.sum(m_l*np.exp(-m_B*y))
    return value
def  df(m_l, m_B, y):
    value = -np.sum(m_B*m_l*np.exp(-m_B*y))
    return value

where m_l and m_b is a numpy array. I use give the following input

   m_l = np.array([0.0036132256153053369,0.95110068028445593])
   m_B = np.array([0.48884897299905006,0.95605658765269563])
   guess=-0.048557088449677460
   root = optimize.root_scalar(f, guess, fprime=df, args=(m_l,m_B),method='newton',rtol=1e-9,maxiter=1000)

I get the following error:

   TypeError                                 Traceback (most recent call last)
   ~\AppData\Local\Temp/ipykernel_15644/3699083294.py in <module>
  4 print(f(m_l, m_B, y))
  5 guess=-0.048557088449677460
  ----> 6 root = optimize.root_scalar(f, guess, fprime=df, args= 
  (m_l,m_B),method='newton',rtol=1e-9,maxiter=1000)

   TypeError: root_scalar() got multiple values for argument 'args'

I guess the error is happening because the input arguments are not passed in correctly. Can someone please help out with what should be the correct way to pass the argument. Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER
import numpy as np
from scipy import optimize

def f(y, m_l, m_B):
    value = -1.0 + np.sum(m_l * np.exp(-m_B * y))
    return value

def df(y, m_l, m_B):
    value = -np.sum(m_B * m_l * np.exp(-m_B * y))
    return value

m_l = np.array([0.0036132256153053369, 0.95110068028445593])
m_B = np.array([0.48884897299905006, 0.95605658765269563])
guess = -0.048557088449677460

root = optimize.root_scalar(f, args=(m_l, m_B), x0=guess, fprime=df, method='newton', rtol=1e-9, maxiter=1000)

print("Root found:", root.root)