You can write standard Python:
from math import * cos(pi)
import math print(dir(math)) help(math.log) log(2,10)
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
import requests resp = requests.get("http://www.physics.rutgers.edu/grad/509/") resp.content
In a different type of cell, you can:
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$.
Toggling toolbars.
The different types of Return:
Choosing cell type:
Use ! to use shell comman right inside notebook
!ls -ltr
You can just send people the .ipynb files; they'll contain your input and (optionally) your output.
Other ways to view and run:
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:
!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:
import sys sys.executable
Close and Halt
from scipy import * from numpy import *
import scipy.linalg as la
from scipy.special import jn, yn, jn_zeros, yn_zeros
%matplotlib inline import matplotlib.pyplot as plt
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()
n=1.0 x=0.3 print("J_%d(%f) = %f" % (n, x, jn(n, x)))
from scipy.integrate import quad, dblquad, tplquad
val, abserr=quad(lambda x,n: jn(n,x), 0,1, args=(0,)) print ('val=', val, 'err=', abserr)
quad(lambda x: exp(-x*x)/sqrt(pi), -Inf,Inf)
dblquad(lambda x,y: (exp(-x*x)*exp(-y*y))/pi, -Inf,Inf, lambda x: -Inf, lambda x: Inf)
from scipy.integrate import odeint, ode
help(odeint)
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])
x0=[pi/4,pi/2,0,0] t = linspace(0,10,250)
x = odeint(dx, x0, t)
plt.plot(t, x[:,0], label='theta1') plt.plot(t, x[:,1], label='theta2') plt.legend(loc='best')
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')
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)
def dy(y, t, zeta, w0): x, p = y dx = p dp = -2*zeta*w0*p - w0**2 * x return array([dx,dp])
y0 = [1.0,0.0] t = linspace(0,10,1000) w0 = 2*pi
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
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')