我目前正在为uni创建一个轨道模拟器,具体使用.ode而不是odeint和“dopri5”。
我正试图找到x&随着时间的推移,地球的y分量围绕着静止的太阳旋转。我无法提取结果。
这是我的代码:
import math
import numpy as np
from Planet import Planet
from scipy.integrate import ode
import matplotlib.pyplot as plt
#Import file for data
infile = open("Earth.ini", "r")
#Read Earth data from file
P = Planet.from_file(infile)
def fun(t, z, c):
r = np.linalg.norm(x)
x = z[0]
y = z[1]
f = [-(c*x)/(r**3), y]
return f
solver = ode(fun)
solver.set_integrator('dopri5')
G = 6.67408E-11
M = 1.9891E+30
c = G*M
solver.set_f_params(c)
t0 = 0.0
z0 = [P.position, P.velocity]
solver.set_initial_value(z0,t0)
t1 = 6.307E+7
N = 730
t = np.linspace(t0,t1,N)
z0 = np.asarray(z0)
z0 = np.concatenate(z0)
xlist = [z0[0]]
ylist = [z0[1]]
k = 1
while solver.successful() and solver.t<t1:
solver.integrate(t[k])
xlist.append(ode.z[0])
ylist.append(ode.z[1])
k +=1
我收到错误:AttributeError:type object'ode'没有属性'z' 和另一个:C:\ Users \ user \ Anaconda3 \ lib \ site-packages \ scipy \ integrate_ode.py:1035:UserWarning:dopri5:输入不一致 self.messages.get(idid,'Unexpected idid =%s'%idid))
我在网上发现了一个示例代码,我正在尝试适应我的代码,但我正在使用向量。
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
def fun(t, z, omega):
"""
Right hand side of the differential equations
dx/dt = -omega * y
dy/dt = omega * x
"""
x, y = z
f = [-omega*y, omega*x]
return f
# Create an `ode` instance to solve the system of differential
# equations defined by `fun`, and set the solver method to 'dop853'.
solver = ode(fun)
solver.set_integrator('dop853')
# Give the value of omega to the solver. This is passed to
# `fun` when the solver calls it.
omega = 2 * np.pi
solver.set_f_params(omega)
# Set the initial value z(0) = z0.
t0 = 0.0
z0 = [1, -0.25]
solver.set_initial_value(z0, t0)
# Create the array `t` of time values at which to compute
# the solution, and create an array to hold the solution.
# Put the initial value in the solution array.
t1 = 2.5
N = 75
t = np.linspace(t0, t1, N)
sol = np.empty((N, 2))
sol[0] = z0
# Repeatedly call the `integrate` method to advance the
# solution to time t[k], and save the solution in sol[k].
k = 1
while solver.successful() and solver.t < t1:
solver.integrate(t[k])
sol[k] = solver.y
k += 1
# Plot the solution...
plt.plot(t, sol[:,0], label='x')
plt.plot(t, sol[:,1], label='y')
plt.xlabel('t')
plt.grid(True)
plt.legend()
plt.show()
如果有人可以提供帮助,那就太棒了。