Ask Your Question

Revision history [back]

Issues with numpy.linspace()

Code:

Restricted Three Body Problem

Plotting x,y,dx,dy

Import

import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt

Variables

a,x,y,u,v,xdot,ydot,udot,vdot = var('a,x,y,u,v,xdot,ydot,udot,vdot')

Sun-Jupiter Ratio

Em = 317.83 Ms = 5.9736*10^24 Sm = Em/Ms b = Sm/(1+Sm)

Restricted Three Body Problem

def r3bp(a,x,y,t): a = b x = x[0] y = y[0] u = u[0] v = v[0] xdot = u ydot = v udot = -(1-a)((x-a)/(sqrt((x-a)^2 + y^2))^3)-a((x+1-a)/(sqrt((x+1-a)^2 + y^2))^3)+x+2v vdot = -(1-a)(y/(sqrt((x-a)^2 + y^2))^3)-a(y/(sqrt((x+1-a)^2 + y^2))^3)+y-2u return[xdot,ydot,udot,vdot]

Initial conditions

x0 = -0.509 y0 = 0.883 u0 = 0.0259 v0 = 0.0149 t = np.linspace(0,100,num=1000) x,y,u,v = odeint(r3bp,x0,y0,u0,v0,t)

Data

x = x[:,0] y = y[:,0] u = u[:,0] v = v[:,0]

Plot functions

px = plt.plot(t,x) py = plt.plot(t,y) pu = plt.plot(t,u) pv = plt.plot(t,v) plot.show(px) plot.show(py) plot.show(pu) plot.show(pv)


TypeError Traceback (most recent call last) <ipython-input-37-2a6a37bd25e9> in <module>() 34 u0 = RealNumber('0.0259') 35 v0 = RealNumber('0.0149') ---> 36 t = np.linspace(Integer(0),Integer(100),num=Integer(1000)) 37 x,y,u,v = odeint(r3bp,x0,y0,u0,v0,t) 38

/opt/sagemath-9.0/local/lib/python3.7/site-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis) 136 stop = asanyarray(stop) * 1.0 137 --> 138 dt = result_type(start, stop, float(num)) 139 if dtype is None: 140 dtype = dt

TypeError: data type not understood

Issues with numpy.linspace()

Code:The following code:

# Restricted Three Body Problem

Problem # Plotting x,y,dx,dy

Import

x,y,dx,dy # Import import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt

Variables

plt # Variables a,x,y,u,v,xdot,ydot,udot,vdot = var('a,x,y,u,v,xdot,ydot,udot,vdot')

var('a,x,y,u,v,xdot,ydot,udot,vdot') # Sun-Jupiter Ratio

Ratio Em = 317.83 Ms = 5.9736*10^24 Sm = Em/Ms b = Sm/(1+Sm)

Sm/(1+Sm) # Restricted Three Body Problem

Problem def r3bp(a,x,y,t): a = b x = x[0] y = y[0] u = u[0] v = v[0] xdot = u ydot = v udot = -(1-a)((x-a)/(sqrt((x-a)^2 -(1-a)*((x-a)/(sqrt((x-a)^2 + y^2))^3)-a((x+1-a)/(sqrt((x+1-a)^2 y^2))^3)-a*((x+1-a)/(sqrt((x+1-a)^2 + y^2))^3)+x+2v y^2))^3)+x+2*v vdot = -(1-a)(y/(sqrt((x-a)^2 -(1-a)*(y/(sqrt((x-a)^2 + y^2))^3)-a(y/(sqrt((x+1-a)^2 y^2))^3)-a*(y/(sqrt((x+1-a)^2 + y^2))^3)+y-2u return[xdot,ydot,udot,vdot]

y^2))^3)+y-2*u return[xdot,ydot,udot,vdot] # Initial conditions

conditions x0 = -0.509 y0 = 0.883 u0 = 0.0259 v0 = 0.0149 t = np.linspace(0,100,num=1000) x,y,u,v = odeint(r3bp,x0,y0,u0,v0,t)

Data

odeint(r3bp,x0,y0,u0,v0,t) # Data x = x[:,0] y = y[:,0] u = u[:,0] v = v[:,0]

v[:,0] # Plot functions

functions px = plt.plot(t,x) py = plt.plot(t,y) pu = plt.plot(t,u) pv = plt.plot(t,v) plot.show(px) plot.show(py) plot.show(pu) plot.show(pv)plot.show(pv)

produces the following error:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-2a6a37bd25e9> in <module>()
     34 u0 = RealNumber('0.0259')
     35 v0 = RealNumber('0.0149')
---> 36 t = np.linspace(Integer(0),Integer(100),num=Integer(1000))
     37 x,y,u,v = odeint(r3bp,x0,y0,u0,v0,t)
     38 

/opt/sagemath-9.0/local/lib/python3.7/site-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis) 136 stop = asanyarray(stop) * 1.0 137 --> 138 dt = result_type(start, stop, float(num)) 139 if dtype is None: 140 dtype = dt

dt TypeError: data type not understood

understood