尝试使用欧拉方法求解数值差分方程,无效值错误
Trying to Solve Numerical Diff Eq Using Euler's Method, Invalid Value Error
我正在尝试从这个网站学习它:http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/01_phugoid/01_03_PhugoidFullModel.ipynb
我试图在尽可能少的帮助下编写代码,但我一直收到此错误:
C:\Users\"My Real Name"\Anaconda2\lib\site-packages\ipykernel__main__.py:29: RuntimeWarning: 在 double_scalars
中遇到无效值
我的图上没有数据点。所以我直接从网站上粘贴了所有代码,但我仍然遇到错误!我放弃了,有人可以帮助python新手吗?
import numpy as np
from matplotlib import pyplot
from math import sin, cos, log, ceil
%matplotlib inline
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
# model parameters:
g = 9.8 # gravity in m s^{-2}
v_t = 30.0 # trim velocity in m s^{-1}
C_D = 1/40 # drag coefficient --- or D/L if C_L=1
C_L = 1 # for convenience, use C_L = 1
### set initial conditions ###
v0 = v_t # start at the trim velocity (or add a delta)
theta0 = 0 # initial angle of trajectory
x0 = 0 # horizotal position is arbitrary
y0 = 1000 # initial altitude
def f(u):
v = u[0]
theta = u[1]
x = u[2]
y = u[3]
return np.array([-g*sin(theta) - C_D/C_L*g/v_t**2*v**2, -g*cos(theta)/v + g/v_t**2*v, v*cos(theta), v*sin(theta)])
def euler_step(u, f, dt):
u + dt * f(u)
T = 100 # final time
dt = 0.1 # time increment
N = int(T/dt) + 1 # number of time-steps
t = np.linspace(0, T, N) # time discretization
# initialize the array containing the solution for each time-step
u = np.empty((N, 4))
u[0] = np.array([v0, theta0, x0, y0])# fill 1st element with initial values
# time loop - Euler method
for n in range(N-1):
u[n+1] = euler_step(u[n], f, dt)
x = u[:,2]
y = u[:,3]
pyplot.figure(figsize=(8,6))
pyplot.grid(True)
pyplot.xlabel(r'x', fontsize=18)
pyplot.ylabel(r'y', fontsize=18)
pyplot.title('Glider trajectory, flight time = %.2f' % T, fontsize=18)
pyplot.plot(x,y, 'k-', lw=2);
解决方法很简单。您忘记了 euler_step 中的 return 语句。
变化
def euler_step(u, f, dt):
u + dt * f(u)
至
def euler_step(u, f, dt):
return u + dt * f(u)
它会起作用
我正在尝试从这个网站学习它:http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/01_phugoid/01_03_PhugoidFullModel.ipynb
我试图在尽可能少的帮助下编写代码,但我一直收到此错误:
C:\Users\"My Real Name"\Anaconda2\lib\site-packages\ipykernel__main__.py:29: RuntimeWarning: 在 double_scalars
中遇到无效值我的图上没有数据点。所以我直接从网站上粘贴了所有代码,但我仍然遇到错误!我放弃了,有人可以帮助python新手吗?
import numpy as np
from matplotlib import pyplot
from math import sin, cos, log, ceil
%matplotlib inline
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
# model parameters:
g = 9.8 # gravity in m s^{-2}
v_t = 30.0 # trim velocity in m s^{-1}
C_D = 1/40 # drag coefficient --- or D/L if C_L=1
C_L = 1 # for convenience, use C_L = 1
### set initial conditions ###
v0 = v_t # start at the trim velocity (or add a delta)
theta0 = 0 # initial angle of trajectory
x0 = 0 # horizotal position is arbitrary
y0 = 1000 # initial altitude
def f(u):
v = u[0]
theta = u[1]
x = u[2]
y = u[3]
return np.array([-g*sin(theta) - C_D/C_L*g/v_t**2*v**2, -g*cos(theta)/v + g/v_t**2*v, v*cos(theta), v*sin(theta)])
def euler_step(u, f, dt):
u + dt * f(u)
T = 100 # final time
dt = 0.1 # time increment
N = int(T/dt) + 1 # number of time-steps
t = np.linspace(0, T, N) # time discretization
# initialize the array containing the solution for each time-step
u = np.empty((N, 4))
u[0] = np.array([v0, theta0, x0, y0])# fill 1st element with initial values
# time loop - Euler method
for n in range(N-1):
u[n+1] = euler_step(u[n], f, dt)
x = u[:,2]
y = u[:,3]
pyplot.figure(figsize=(8,6))
pyplot.grid(True)
pyplot.xlabel(r'x', fontsize=18)
pyplot.ylabel(r'y', fontsize=18)
pyplot.title('Glider trajectory, flight time = %.2f' % T, fontsize=18)
pyplot.plot(x,y, 'k-', lw=2);
解决方法很简单。您忘记了 euler_step 中的 return 语句。 变化
def euler_step(u, f, dt):
u + dt * f(u)
至
def euler_step(u, f, dt):
return u + dt * f(u)
它会起作用