Trying to Solve Numerical Diff Eq Using Euler's Method, Invalid Value Error
<p>I am trying to learn it from this website: <a href="http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/01_phugoid/01_03_PhugoidFullModel.ipynb" rel="nofollow">http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/01_phugoid/01_03_PhugoidFullModel.ipynb</a></p>

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

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

<p>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?</p>

<pre><code>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);
</code></pre>

<p>The solution is very simple. You forgot the return statement in euler_step.
Change</p>

<pre><code>def euler_step(u, f, dt):
    u + dt * f(u)
</code></pre>

<p>to</p>

<pre><code>def euler_step(u, f, dt):
    return u + dt * f(u)
</code></pre>

<p>and it will work</p>

python numpy matplotlib numerical-methods