I am trying to define the following vector function, but keep getting an error

Question:

Where am I going wrong? The function:

enter image description here

The code I have written is as follows! Note the second def function is an attempt to integrate it using solve_ivp! please let me know if there are any issues there as well:

def fun(s, rho_0, rho_1):
return lambda t, s:np.dot(np.array([0.775416, 0,0, 0.308968]).reshape(2,2), s) + np.array([rho_0,rho_1]).reshape(2,1)

def fun2(t, rho_0, rho_1):
    res = solve_ivp(fun, [0, 5], y0 = [0, 0], t_eval=np.arange(0,5), args = (rho_0, rho_1), vectorized = True)
    return res.y[1]

fun2(t = 0, rho_0 = 0.0099532, rho_1 = 0.001699)

The error I get:

TypeError: fun() takes 3 positional arguments but 4 were given
Asked By: Carl

||

Answers:

From the documentation, fun should take t and y as it’s first two arguments. Thus, you need to define fun as follows:

fun(t, y, rho1, rho2)

That’s why you’re facing this error, it is passing t, y, and then the arguments specified in args (you’ve provided 2 arguments, so you end up with 4 in total including t and y).

Answered By: Kraigolas
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.