1. import numpy as np
  2. from scipy.integrate import odeint
  3. import matplotlib.pyplot as plt
  4.  
  5. # function that returns dz/dt
  6. def model(z,t,u):
  7. x = z[0]
  8. y = z[1]
  9. dxdt = (-x + u)/2.0
  10. dydt = (-y + x)/5.0
  11. dzdt = [dxdt,dydt]
  12. return dzdt
  13.  
  14. # initial condition
  15. z0 = [0,0]
  16.  
  17. # number of time points
  18. n = 401
  19.  
  20. # time points
  21. t = np.linspace(0,40,n)
  22.  
  23. # step input
  24. u = np.zeros(n)
  25. # change to 2.0 at time = 5.0
  26. u[51:] = 2.0
  27.  
  28. # store solution
  29. x = np.empty_like(t)
  30. y = np.empty_like(t)
  31. # record initial conditions
  32. x[0] = z0[0]
  33. y[0] = z0[1]
  34.  
  35. # solve ODE
  36. for i in range(1,n):
  37. # span for next time step
  38. tspan = [t[i-1],t[i]]
  39. # solve for next step
  40. z = odeint(model,z0,tspan,args=(u[i],))
  41. # store solution for plotting
  42. x[i] = z[1][0]
  43. y[i] = z[1][1]
  44. # next initial condition
  45. z0 = z[1]
  46.  
  47. # plot results
  48. plt.plot(t,u,'g:',label='u(t)')
  49. plt.plot(t,x,'b-',label='x(t)')
  50. plt.plot(t,y,'r--',label='y(t)')
  51. plt.ylabel('values')
  52. plt.xlabel('time')
  53. plt.legend(loc='best')
  54. plt.show()

ODEINT 求解常微分方程(4)的更多相关文章

  1. ODEINT 求解常微分方程(3)

    import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function tha ...

  2. ODEINT 求解常微分方程(2)

    import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt # function tha ...

  3. ODEINT 求解常微分方程(1)

    An example of using ODEINT is with the following differential equation with parameter k=0.3, the ini ...

  4. MATLAB求解常微分方程:ode45函数与dsolve函数

    ode45函数无法求出解析解,dsolve可以求出解析解(若有),但是速度较慢. 1.      ode45函数 ①求一阶常微分方程的初值问题 [t,y] = ode45(@(t,y)y-2*t/y, ...

  5. 欧拉法求解常微分方程(c++)

    #include<iostream> #include<iomanip> using namespace std; int main() { double x, y, h;   ...

  6. 改进欧拉公式求解常微分方程(c++)

    #include<iostream> #include<iomanip> using namespace std; int main() { double x,y,h,temp ...

  7. 梯形法求解常微分方程(c++)

    #include<iostream> #include<iomanip> using namespace std; int main() { double x,y,yn,h,t ...

  8. 后退欧拉法求解常微分方程(c++)

    #include<iostream> #include<iomanip> using namespace std; int main() { double x,y,yn,h,t ...

  9. 欧拉法求解常微分方程(c++)【转载】

    摘自<c++和面向对象数值计算>,代码简洁明快,采用类进行封装实现代码,增强代码的重用性,通过继承可实现代码的重用,采用函数指针,通用性增强,在函数改变时只需要单独改变函数部分的代码,无需 ...

随机推荐

  1. java 精确加减

    /** * 提供精确的加法运算. * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public double add(double v1, dou ...

  2. Oracle备份与恢复详解

    http://www.360doc.com/content/10/1015/15/3267996_61218717.shtml --------摘自 360doc 为了能有效地备份和恢复数据库,建议大 ...

  3. Windows系统下Git的下载和配置

    简介:Git是一款免费.开源的分布式版本控制系统,可记录文件每次改动,便于多人协作编辑. 下载:git-for-windows下载地址https://git-for-windows.github.io ...

  4. spring cloud系列教程第六篇-Eureka集群版

    spring cloud系列教程第六篇-Eureka集群版 本文主要内容: 本文来源:本文由凯哥Java(kaigejava)发布在博客园博客的.转载请注明 1:Eureka执行步骤理解 2:集群原理 ...

  5. [JavaWeb基础] 021.Action中result的各种转发类型

    在struts2中, struts.xml中result的类型有多种,它们类似于struts1中的forward,常用的类型有dispatcher(默认值).redirect.redirectActi ...

  6. [优文翻译]001.真正程序员该是什么样的(How To Be A Real Programmer)

    01.Real Programmers don't write specs -- users should consider themselves lucky to get any programs ...

  7. for循环结构的使用

    /* for循环格式: for(①初始化条件; ②循环条件 :③迭代部分){ //④循环体 } 执行顺序:①-②-④-③---②-④-③-----直至循环条件不满足 退出当前循环 * */ publi ...

  8. switch-case与if-else的转换

    对学会成绩大于60分的,输出合格,低于60分的输出不合格 import java.util.Scanner; public class TestSwitch3 { public static void ...

  9. 04.Django-视图与路由

    视图层 1. HTTP请求 HttpRequest对象 request.path #使用GET方法时,只会得到路径. request.get_full_path() #使用GET方法时,会得到包括路径 ...

  10. 50个SQL语句(MySQL版) 问题八

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...