1

I am trying to solve an initial value problem with solve_ivp. The problem is that my function f(x,y)=dy/dx contain extra arguments. I tried to use this:

Pass args for solve_ivp (new SciPy ODE API)

but it keeps giving me errors. Here is the code:

from numpy import arange
import math
import numpy as np
from scipy.integrate import solve_ivp
from numpy import pi

sigmav = 1.0e-9
Mpl = 1.22e19
ms=100.0
gg=100.0
gs=106.75

def Yeq(x):
    return 0.145*(gg/gs)*(x)**(3/2)*np.exp(-x)

def ss(x,m_dm):
    return (2/45)*((pi)**2)*gs*(m_dm**3/x**3)

def hubb(x,m_dm):
    return 1.66*(gg**(1/2))*(m_dm**2/Mpl)*(1/x**2)

def derivada(x,yl,m_dm,σv):
    return -(σv*ss(x,m_dm)*((yl**2)-(Yeq(x)**2)))/(x*hubb(x,m_dm))


xx=np.logspace(np.log10(3),3,100)
y00 = Yeq(xx[0])
x00 = xx[0] 
sigmav = 1.7475e-9
ms=100.0

solucion1 = solve_ivp(fun=lambda x, y: derivada(x, y, args=(ms,sigmav),[np.min(xx),np.max(xx)],[y00],method='BDF',rtol=1e-10,atol=1e-10))

When I try to run the cell for solucion1, it returns the following:

File "<ipython-input-17-afcf6c3782a9>", line 6
solucion1 = solve_ivp(fun=lambda x, y: derivada(x, y, args=(ms,sigmav),[np.min(xx),np.max(xx)],[y00],method='BDF',rtol=1e-10,atol=1e-10))
                                                                      ^
SyntaxError: positional argument follows keyword argument

It must be something very easy but I am just starting to work with python, so please help me with this.

Ah77
  • 85
  • 1
  • 9

3 Answers3

2

You simply mis-placed a closing parenthesis. As derivada has no named args, remove the args statement there, the lambda expression already binds these additional parameters. Also, the passed function is not a named argument

solucion1 = solve_ivp(lambda x, y: derivada(x, y, ms, sigmav),[np.min(xx),np.max(xx)], [y00], method='BDF', rtol=1e-10, atol=1e-10)

or in the form that you cite from the github discussion, where the parameter list is constructed elsewhere

args = ( ms, sigmav )

solucion1 = solve_ivp(lambda x, y: derivada(x, y, *args),[np.min(xx),np.max(xx)], [y00], method='BDF', rtol=1e-10, atol=1e-10)
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
1

The error message gives the answer: all positional arguments need to come before keyword arguments. In your case, I'd suggest using partial:

from functools import partial

f = partial(derivada, m_dm=ms, σv=sigmav)
solucion1 = solve_ivp(f, [np.min(xx),np.max(xx)],[y00],method='BDF',rtol=1e-10,atol=1e-10)
Tim
  • 2,756
  • 1
  • 15
  • 31
1

This issue has been resolved in SciPy 1.4+.

https://github.com/scipy/scipy/issues/8352#issuecomment-619243137

You should be able to add extra arguments to the function as you did with odeint, lambda function not required. I think this is what you intended for the solution

solucion1 = solve_ivp(fun=derivada,
                  t_span=[np.min(xx),np.max(xx)],
                  y0=[y00],
                  args=(ms,sigmav),
                  method='BDF',
                  rtol=1e-10,
                  atol=1e-10)