Tim Smith
Tim Smith

Reputation: 65

TypeError: deriv() takes 2 positional arguments but 4 were given

I'm getting the above error. I understand in principle what it means, but can't really see how it applies to my code

#project starts here
import numpy as np
import scipy.integrate
import matplotlib.pyplot as plt
from numpy import pi
from scipy.integrate import odeint

def deriv(cond,t):
    for q in range (0,N):
        i=6*q
        dydt[i]=cond[i+3]
        dydt[i+1]=cond[i+4]
        dydt[i+2]=cond[i+5]
        r=sqrt((cond[i])**2 +(cond[i+1])**2 +(cond[i+2])**2)
        dydt[i+3]=-G*M*cond[i]/(r**3)
        dydt[i+4]=-G*M*cond[i+1]/(r**3)
        dydt[i+5]=-G*M*cond[i+2]/(r**3)
    return dydt

G=1
M=1
N=12
vmag=((G*M)/(2))**(0.5)
theta = np.linspace(0,2*pi,N)
x=2*np.cos(theta)
y=2*np.sin(theta)
vx=-vmag*np.sin(theta)
vy=vmag*np.cos(theta)
z=np.zeros(N)
vz=np.zeros(N)
t=np.linspace(0,30,100)

cond=list(item for group in zip(x,y,z,vx,vy,vz) for item in group)
sln=odeint(deriv, cond, t, args=(G,M))

Any ideas where it is coming from? I feel like I have given the correct number of arguments.

Upvotes: 0

Views: 1989

Answers (2)

Zach Fox
Zach Fox

Reputation: 614

You are sending 4 arguments to deriv. Per the odeint docs, you have a function deriv whose first two arguments must be y and t. When you call odeint(deriv,cond,t,...) the cond and t are automatically sent as the first two arguments to deriv. All you need to do is to make deriv(cond,t,G,M).

Upvotes: 1

alpert
alpert

Reputation: 4655

If you look documantation for odeint [1], you will see that your fucntion to call in oneint must be in form func(y, t0, ...). So when you call odeint(deriv, cond, t, args=(G,M)) it actually call your function as deriv(cond,t,G,m). But your function takes just 2 argument.

[1] http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.integrate.odeint.html

Upvotes: 0

Related Questions