Solve Differential Equations with ODEINT

您所在的位置:网站首页 solve_ivp与odeint区别 Solve Differential Equations with ODEINT

Solve Differential Equations with ODEINT

2024-07-13 10:43:33| 来源: 网络整理| 查看: 265

Differential equations are solved in Python with the Scipy.integrate package using function odeint or solve_ivp.

Jupyter Notebook ODEINT Examples on GitHub Jupyter Notebook ODEINT Examples in Google Colab

ODEINT requires three inputs:

y = odeint(model, y0, t) model: Function name that returns derivative values at requested y and t values as dydt = model(y,t) y0: Initial conditions of the differential states t: Time points at which the solution should be reported. Additional internal points are often calculated to maintain accuracy of the solution but are not reported.

An example of using ODEINT is with the following differential equation with parameter k=0.3, the initial condition y0=5 and the following differential equation.

$$\frac{dy(t)}{dt} = -k \; y(t)$$

The Python code first imports the needed Numpy, Scipy, and Matplotlib packages. The model, initial conditions, and time points are defined as inputs to ODEINT to numerically calculate y(t).

import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function that returns dy/dt def model(y,t):     k = 0.3     dydt = -k * y     return dydt # initial condition y0 = 5 # time points t = np.linspace(0,20) # solve ODE y = odeint(model,y0,t) # plot results plt.plot(t,y) plt.xlabel('time') plt.ylabel('y(t)') plt.show() [$[Get Code]]

An optional fourth input is args that allows additional information to be passed into the model function. The args input is a tuple sequence of values. The argument k is now an input to the model function by including an addition argument.

import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function that returns dy/dt def model(y,t,k):     dydt = -k * y     return dydt # initial condition y0 = 5 # time points t = np.linspace(0,20) # solve ODEs k = 0.1 y1 = odeint(model,y0,t,args=(k,)) k = 0.2 y2 = odeint(model,y0,t,args=(k,)) k = 0.5 y3 = odeint(model,y0,t,args=(k,)) # plot results plt.plot(t,y1,'r-',linewidth=2,label='k=0.1') plt.plot(t,y2,'b--',linewidth=2,label='k=0.2') plt.plot(t,y3,'g:',linewidth=2,label='k=0.5') plt.xlabel('time') plt.ylabel('y(t)') plt.legend() plt.show() [$[Get Code]] Exercises

Find a numerical solution to the following differential equations with the associated initial conditions. Expand the requested time horizon until the solution reaches a steady state. Show a plot of the states (x(t) and/or y(t)). Report the final value of each state as `t \to \infty`.

Problem 1

$$\frac{dy(t)}{dt} = -y(t) + 1$$

$$y(0) = 0$$

import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function that returns dy/dt def model(y,t):     dydt = -y + 1.0     return dydt # initial condition y0 = 0 # time points t = np.linspace(0,5) # solve ODE y = odeint(model,y0,t) # plot results plt.plot(t,y) plt.xlabel('time') plt.ylabel('y(t)') plt.show() [$[Get Code]]

Problem 2

$$5 \; \frac{dy(t)}{dt} = -y(t) + u(t)$$

$$y(0) = 1$$

`u` steps from 0 to 2 at `t=10`

import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function that returns dy/dt def model(y,t):     # u steps from 0 to 2 at t=10     if t


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭