My Homework Assignment.

Here I could put the description of the problem that I'm working on. Maybe some math. $$ \int_0^1 x^2 \, dx $$ and other stuff. Add more stuff $\sin(x)$.

In [8]:
for i in range(4,-5,-3): # range(start,stop,stride)
    print(i)
4
1
-2
In [5]:
# This would be a while loop. 
i = -3
while i < 100:
    print(i)
    i = i+10
-3
7
17
27
37
47
57
67
77
87
97
In [9]:
A = [];
for i in range(5):
    A.append(i)
In [10]:
print(A)
[0, 1, 2, 3, 4]
In [11]:
print(A[1])
1
In [15]:
def power(x,y=2):
    value = x**y
    return value
In [13]:
print(f(4))
16
In [14]:
print(f())
9
In [17]:
power(4,1)
Out[17]:
4
In [4]:
class Student:
    def __init__(self, FirstName, LastName, Grade = "A"):
        self.FirstName = FirstName
        self.LastName = LastName
        self.Grade = Grade
    def showStudent(self):
        print("Student Info: ")
        print("  First Name: ", self.FirstName)
        print("  Last  Name: ", self.LastName)
        print("  Grade     : ", self.Grade)
In [6]:
S1 = Student("John","Chrispell")
In [8]:
S1.showStudent()
Student Info: 
('  First Name: ', 'John')
('  Last  Name: ', 'Chrispell')
('  Grade     : ', 'A')

Graph and save a set of points.

Let use a helix. For this I will use a radius $r$, parameter $p$, Starting point $t_0$, and Ending point $T$, and number of points $n$.

In [9]:
r  = 5.0
p  = 1.0
t0 = -10.0
T  = 10.0
n  = 100
In [11]:
from math import *
dt = (T - t0)/n
xpts = []
ypts = []
zpts = []

for i in range(n): 
    t = t0 + i*dt
    xpts.append(r*cos(t))
    ypts.append(r*sin(t))
    zpts.append((p*t)/(2.0*pi))

Output the points to a file

The next block of code will save our set of points neatly to a file.

In [14]:
# Output the points. 
f = open("Points.txt",'w')
for i in range(len(xpts)):
    Point = str(int(xpts[i])) + " " + str(int(ypts[i])) + " " + str(int(zpts[i])) +"\n"
    f.write(Point)
    
f.close()
In [15]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib inline
In [16]:
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(xpts,ypts,zpts)
plt.show()

Symbolic Python

In [18]:
import numpy
import sympy
from matplotlib import rcParams
rcParams['font.family']='serif'
rcParams['font.size']='16'
In [19]:
from sympy import init_printing
init_printing()
In [28]:
x, f, nu, t = sympy.symbols('x f nu t')
In [22]:
x
Out[22]:
$$x$$
In [24]:
f = x**2 + 3*x
In [25]:
f
Out[25]:
$$x^{2} + 3 x$$
In [26]:
fprime = f.diff(x)
In [27]:
fprime
Out[27]:
$$2 x + 3$$
In [29]:
phi = sympy.exp(-(x-4*t)**2/(4*nu*(t+1)))
In [30]:
phi
Out[30]:
$$e^{- \frac{\left(- 4 t + x\right)^{2}}{4 \nu \left(t + 1\right)}}$$
In [33]:
phiprime = phi.integrate(x)
In [34]:
phiprime
Out[34]:
$$\sqrt{\pi} \sqrt{\nu} \operatorname{erf}{\left (\frac{- 4 t + x}{2 \sqrt{\nu} \sqrt{\operatorname{polar\_lift}{\left (t + 1 \right )}}} \right )} \sqrt{\operatorname{polar\_lift}{\left (t + 1 \right )}}$$
In [40]:
%%bash
cat Points.txt
-4 2 -1
-4 1 -1
-4 0 -1
-4 0 -1
-4 -1 -1
-4 -2 -1
-4 -2 -1
-3 -3 -1
-2 -4 -1
-1 -4 -1
0 -4 -1
0 -4 -1
1 -4 -1
2 -4 -1
3 -3 -1
3 -3 -1
4 -2 -1
4 -1 -1
4 0 -1
4 0 0
4 1 0
4 2 0
3 3 0
3 3 0
2 4 0
1 4 0
0 4 0
0 4 0
-1 4 0
-2 4 0
-3 3 0
-3 3 0
-4 2 0
-4 1 0
-4 0 0
-4 0 0
-4 -1 0
-4 -2 0
-3 -3 0
-2 -4 0
-2 -4 0
-1 -4 0
0 -4 0
0 -4 0
1 -4 0
2 -4 0
3 -3 0
4 -2 0
4 -1 0
4 0 0
5 0 0
4 0 0
4 1 0
4 2 0
3 3 0
2 4 0
1 4 0
0 4 0
0 4 0
-1 4 0
-2 4 0
-2 4 0
-3 3 0
-4 2 0
-4 1 0
-4 0 0
-4 0 0
-4 -1 0
-4 -2 0
-3 -3 0
-3 -3 0
-2 -4 0
-1 -4 0
0 -4 0
0 -4 0
1 -4 0
2 -4 0
3 -3 0
3 -3 0
4 -2 0
4 -1 0
4 0 0
4 0 1
4 1 1
4 2 1
3 3 1
3 3 1
2 4 1
1 4 1
0 4 1
0 4 1
-1 4 1
-2 4 1
-3 3 1
-4 2 1
-4 2 1
-4 1 1
-4 0 1
-4 0 1
-4 -1 1

Widgets !

Some stuff to interact with your plots and data quickly.

In [2]:
import numpy as np
import matplotlib.pyplot as plt

from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import display
In [3]:
# This is a function plot x, y, and adds a title. 
def plt_arrays(x,y,title="", color="red", linestyle="dashed", linewidth="2"):
    fig = plt.figure()
    axes = fig.add_subplot(111)
    axes.plot(x,y,color=color,linestyle=linestyle,linewidth=linewidth)
    axes.set_title(title)
    axes.grid()
    plt.show()
    

We should look at a function with the following form: $$ f(x) = a e^{(bx)} + c\sin(d\,x) $$ where $a, b, c,$ and $d$ are constants.

In [13]:
from math import *
def f(a,b,c,d, **kwargs): 
    x = np.linspace(-10,10,40)
    y = a*np.exp(b*x) + c*np.sin(d*x)
    
    title = "$f(x) = %s e^{(%sx)} + (%s)\sin(%s\,x) $" % (a,b,c,d)
    plt_arrays(x,y,title = title, **kwargs)
In [14]:
%matplotlib inline
# Define the constants. 
a = 1.0
b = 1.0
c = 1.0
d = 1.0
f(a,b,c,d)
In [15]:
i = interact(f,
             a=(-10.,10),
             b=(-10.,10),
             c=(-10.,10),
             d=(-10.,10),
             color = ['red', 'blue','green','black'],
             linestyle=["solid","dashed"],
             linewidth=(1,5)
             )
In [ ]: