Issues with numpy.linspace()

asked 2020-05-07 22:45:23 -0600

nooniensoong97 gravatar image

updated 2020-05-08 02:56:26 -0600

slelievre gravatar image

The following 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+2*v
  vdot = -(1-a)*(y/(sqrt((x-a)^2 + y^2))^3)-a*(y/(sqrt((x+1-a)^2 + y^2))^3)+y-2*u
  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)

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

TypeError: data type not understood
edit retag flag offensive close merge delete

Comments

Hi nooniensoong97

you should use the code button (the one marked 101. I think there is a lot of syntax errors in your code.

have a look at this post : coupled second order differential equation

maybe it could help you ?

So strange so that I wonder if your message is not a troll ? Sorry if it is not.

ortollj gravatar imageortollj ( 2020-05-08 01:22:58 -0600 )edit

How could there be a lot of syntax errors? I was following a couple of youtube videos on ODE?

nooniensoong97 gravatar imagenooniensoong97 ( 2020-05-08 07:50:19 -0600 )edit

Anyways thanks for the link to the other example. I had to change a few things around. I still needed to have access each individual data for other data analysis. So I used the .tolist() function on the generated arrays.

nooniensoong97 gravatar imagenooniensoong97 ( 2020-05-08 09:33:44 -0600 )edit

I regret my remark, it comes from the fact that the first time I looked at your message, the indentation was not correct, I don't know why, but when I came back a few hours later everything was ok, sorry

ortollj gravatar imageortollj ( 2020-05-08 11:51:56 -0600 )edit

The way the code appeared while the code portions of the question were not properly formatted as code, the asterisks for multiplication (*) were interpreted as markdown for "italics" and thus not displayed, which could give the impression of syntax errors.

@nooniensoong97 -- if you managed to solve the problem, please post the solution as an answer here, and accept the answer so that the question is marked as solved.

slelievre gravatar imageslelievre ( 2020-05-08 16:03:08 -0600 )edit