现在的位置: 首页 > 问答 > 正文

Trying to Solve Numerical Diff Eq Using Euler's Method, Invalid Value Error

2016年10月17日 问答 ⁄ 共 1651字 ⁄ 字号 暂无评论

I am trying to learn it from this website: http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/01_phugoid/01_03_PhugoidFullModel.ipynb

I was trying to code it with as little help as possible, but I kept getting this error:

C:\Users\"My Real Name"\Anaconda2\lib\site-packages\ipykernel__main__.py:29: RuntimeWarning: invalid value encountered in double_scalars

With no data points on my plot. So I literally pasted all the code in directly from the website and I still get there error! I give up, can someone help a python newbie?

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);

给我留言

您必须 [ 登录 ] 才能发表留言!

×