Introduction: The basics of Notebook

You can write standard Python:

In [ ]:
from math import *
cos(pi)
In [ ]:
import math
print(dir(math))
help(math.log)
log(2,10)

Two modes

Command Mode and Edit Mode

"Return" and "Esc" to switch between Command and Edit modes

To Execute : "Shift Return"

To return to Edit mode : "Return/double click"

hide long cell output double click

In [ ]:
import requests
resp = requests.get("http://www.physics.rutgers.edu/grad/509/")
resp.content

Two (main) cell types

In a different type of cell, you can:

  • write Markdown
  • embed LaTex equation in it

So you can add comments to your code explaining that, to calculate your cycling speed, you must make use of the fact that

$[x,p_x]=i \hbar$.

Some basic operations and keystrokes:

Toggling toolbars.

  • Return and Esc
  • A (new cell before) and B (new cell after)
  • X (cut cell), C (copy cell), V (paste cell)
  • DD (delete cell)

The different types of Return:

  • Shift-Return
  • Ctrl-Return
  • Alt-Return

Choosing cell type:

  • M (for Markdown)
  • Y (for code)

Use ! to use shell comman right inside notebook

In [ ]:
!ls -ltr

Sharing your notebooks

You can just send people the .ipynb files; they'll contain your input and (optionally) your output.

Other ways to view and run:

  • nbconvert - e.g. convert to static HTML, LaTeX, PDF, Python script, etc
  • Github & Bitbucket - rendered, but static, e.g. for qhue
  • nbviewer - http://nbviewer.jupyter.org
  • binder or Azure notebooks - take a github repo with notebooks, and run them in a Docker session [mybinder.org]
  • jupyterhub - run locally to make jupyter environments available to users

Understanding the kernel setting

The running Python environment is determined by the kernel setting of your notebook and not, for example, by the virtualenv you happen to be in.

Here's where your shell environment will find python, pip etc:

In [ ]:
!which python

That's often the Python that's running the notebook server.

And here's the Python running this notebook, defined by the choice of kernel:

In [ ]:
import sys
sys.executable

And lastly...

Close and Halt

In [ ]:
from scipy import *
from numpy import *
In [ ]:
import scipy.linalg as la
In [ ]:
from scipy.special import jn, yn, jn_zeros, yn_zeros
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
In [ ]:
x=linspace(0,4*pi,200)
plt.plot(x, jn(0,x), label='$j_0$')
plt.plot(x, jn(2,x), label='$j_2$')
plt.plot(x, jn(4,x), label='$j_4$')
plt.legend()
In [ ]:
n=1.0
x=0.3
print("J_%d(%f) = %f" % (n, x, jn(n, x)))
In [ ]:
from scipy.integrate import quad, dblquad, tplquad
In [ ]:
val, abserr=quad(lambda x,n: jn(n,x), 0,1, args=(0,))
print ('val=', val, 'err=', abserr)
In [ ]:
quad(lambda x: exp(-x*x)/sqrt(pi), -Inf,Inf)
In [ ]:
dblquad(lambda x,y: (exp(-x*x)*exp(-y*y))/pi, -Inf,Inf, lambda x: -Inf, lambda x: Inf)
In [ ]:
from scipy.integrate import odeint, ode
In [ ]:
help(odeint)
In [ ]:
def dx(x,t):
 " x=[x1,x2,x3,x4]"
 g, L, m = 9.82, 0.5, 0.1
 
 x1,x2,x3,x4 = x
 c1 = 1/(m*L**2)
 dx1 = 6.*c1 * (2*x3-3*cos(x1-x2)*x4)/(16.-9.*cos(x1-x2)**2)
 dx2 = 6.*c1 * (8*x4-3*cos(x1-x2)*x3)/(16.-9.*cos(x1-x2)**2)
 dx3 = -0.5/c1 * (dx1*dx2 * sin(x1-x2) + 3*g/L * sin(x1))
 dx4 = -0.5/c1 * (-dx1*dx2 * sin(x1-x2)+ g/L * sin(x2))
 return array([dx1,dx2,dx3,dx4])
In [ ]:
x0=[pi/4,pi/2,0,0]
t = linspace(0,10,250)
In [ ]:
x = odeint(dx, x0, t)
In [ ]:
plt.plot(t, x[:,0], label='theta1')
plt.plot(t, x[:,1], label='theta2')
plt.legend(loc='best')
In [ ]:
L=0.5
x1 = L * sin(x[:,0])
y1 = -L * cos(x[:,0])
x2 = x1 + L * sin(x[:,1])
y2 = y1 - L * cos(x[:,1])
plt.plot(x1,y1, label='pendulum1')
plt.plot(x2,y2, label='pendulum2')
In [ ]:
import time
from IPython.display import clear_output
fig, ax = plt.subplots(figsize=(4,4))
for t_idx, tt in enumerate(t[:200]):
 x1 = + L * sin(x[t_idx, 0])
 y1 = - L * cos(x[t_idx, 0])
 x2 = x1 + L * sin(x[t_idx, 1])
 y2 = y1 - L * cos(x[t_idx, 1])
 
 ax.cla() 
 ax.plot([0, x1], [0, y1], 'r.-')
 ax.plot([x1, x2], [y1, y2], 'b.-')
 ax.set_ylim([-1.5, 0.5])
 ax.set_xlim([1, -1])
 clear_output() 
 display(fig)
 time.sleep(0.1)
In [ ]:
def dy(y, t, zeta, w0):
 x, p = y
 dx = p
 dp = -2*zeta*w0*p - w0**2 * x
 return array([dx,dp])
In [ ]:
y0 = [1.0,0.0]
t = linspace(0,10,1000)
w0 = 2*pi
In [ ]:
y1 = odeint(dy, y0, t, args=(0.0,w0)) # undamped
y2 = odeint(dy, y0, t, args=(0.2,w0)) # under damped
y3 = odeint(dy, y0, t, args=(1.0,w0)) # critical damping
y4 = odeint(dy, y0, t, args=(5.0,w0)) # over damped
In [ ]:
plt.plot(t, y1[:,0], label='undamped')
plt.plot(t, y2[:,0], label='under damped')
plt.plot(t, y3[:,0], label='critical damping')
plt.plot(t, y4[:,0], label='over damped')
plt.legend(loc='best')
In [ ]:
 

AltStyle によって変換されたページ (->オリジナル) /