2016-10-27 28 views
0

我完全新的Python,並試圖整合以下頌歌:那我在這Dopri5做錯了實施

$ \點{X} = -2x-Y^2 $

$ \ dot {y} = -yx^2

這會導致數組中的所有內容都爲0但是 我在做什麼錯?它主要是複製代碼,並與另一個,不耦合的代碼,它工作正常。

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


def fun(t, z): 
    """ 
    Right hand side of the differential equations 
     dx/dt = -omega * y 
     dy/dt = omega * x 
    """ 
    x, y = z 
    f = [-2*x-y**2, -y-x**2] 
    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('dopri5') 

# Set the initial value z(0) = z0. 
t0 = 0.0 
z0 = [0, 0] 
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() 

回答

1

你的初始狀態(z0)是[0,0]。此初始狀態的時間導數(fun)也是[0,0]。因此,對於這個初始條件,[0,0]是所有時間的正確解決方案。

如果您將初始條件更改爲某個其他值,則應該觀察到更有趣的結果。

+0

哦對,這是一個固定點,我認爲我在代碼中做了一些錯誤的事情 –

相關問題