odeFunction matlab,Imitate ode45 function from MATLAB in Python

The interface of integrate.ode is not as intuitive as of a simpler method odeint which, however, does not support choosing an ODE integrator. The main difference is that ode does not run a loop for you; if you need a solution at a bunch of points, you have to say at what points, and compute it one point at a time.

import numpy as np

from scipy import integrate

import matplotlib.pyplot as plt

def vdp1(t, y):

return np.array([y[1], (1 - y[0]**2)*y[1] - y[0]])

t0, t1 = 0, 20 # start and end

t = np.linspace(t0, t1, 100) # the points of evaluation of solution

y0 = [2, 0] # initial value

y = np.zeros((len(t), len(y0))) # array for solution

y[0, :] = y0

r = integrate.ode(vdp1).set_integrator("dopri5") # choice of method

r.set_initial_value(y0, t0) # initial values

for i in range(1, t.size):

y[i, :] = r.integrate(t[i]) # get one more value, add it to the array

if not r.successful():

raise RuntimeError("Could not integrate")

plt.plot(t, y)

plt.show()

5d41697404f2f01e34ddf4e505d80ea7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值