代码拉取完成,页面将自动刷新
"""
Created on Sat Nov 09 13:53:23 2013
PHYS 613, Assignment 8
Nick Crump
"""
# Exercise 5.16
"""
Implement the Runge-Kutta-Fehlberg method to solve the second
order ODE below and compare to the analytic result.
"""
import numpy as np
import ODEsolve as ode
import matplotlib.pyplot as plt
# function for ODE y''(t) = -4y(t), y(0)=1, y'(0)=0
# ***************************************************
def f(ti,IC):
ui = IC[0] # y'(t)
yi = IC[1] # y (t)
func = np.array([-4*yi, ui])
return func
# ***************************************************
# set initial conditions as y'(0), y(0)
IC = [0, 1]
# call RKF45 method from my ODE solve module
t,s = ode.RKF45HO(f,IC,0,2*np.pi,0.01,1e-5,stp=1)
# get analytic solution for comparison
# this ODE is the harmonic oscillator for w = 2
yAct = IC[1]*np.cos(2*t) + 0.5*np.sin(2*t)
# plot numerical and analytic solutions
plt.plot(t,s[1],'b', label='RKF45 Solution')
plt.plot(t,yAct,'r', label='Analytic Solution')
plt.xlabel('Time')
plt.ylabel('Position')
plt.legend()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。