Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook SomethingGroovy.ipynb

Views: 49
Kernel: Python 2 (SageMath)

My Homework Assignment.

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

for i in range(4,-5,-3): # range(start,stop,stride) print(i)
4 1 -2
# 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
A = []; for i in range(5): A.append(i)
print(A)
[0, 1, 2, 3, 4]
print(A[1])
1
def power(x,y=2): value = x**y return value
print(f(4))
16
print(f())
9
power(4,1)
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)
S1 = Student("John","Chrispell")
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 rr, parameter pp, Starting point t0t_0, and Ending point TT, and number of points nn.

r = 5.0 p = 1.0 t0 = -10.0 T = 10.0 n = 100
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.

# 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()
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D %matplotlib inline
fig = plt.figure() ax = fig.add_subplot(111,projection='3d') ax.scatter(xpts,ypts,zpts) plt.show()
Image in a Jupyter notebook

Symbolic Python

import numpy import sympy from matplotlib import rcParams rcParams['font.family']='serif' rcParams['font.size']='16'
from sympy import init_printing init_printing()
x, f, nu, t = sympy.symbols('x f nu t')
x
xx
f = x**2 + 3*x
f
x2+3xx^{2} + 3 x
fprime = f.diff(x)
fprime
2x+32 x + 3
phi = sympy.exp(-(x-4*t)**2/(4*nu*(t+1)))
phi
e(4t+x)24ν(t+1)e^{- \frac{\left(- 4 t + x\right)^{2}}{4 \nu \left(t + 1\right)}}
phiprime = phi.integrate(x)
phiprime
πνerf(4t+x2νpolar_lift(t+1))polar_lift(t+1)\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 )}}
%%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.

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
# 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)=ae(bx)+csin(dx) f(x) = a e^{(bx)} + c\sin(d\,x) where a,b,c,a, b, c, and dd are constants.

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)
%matplotlib inline # Define the constants. a = 1.0 b = 1.0 c = 1.0 d = 1.0 f(a,b,c,d)
Image in a Jupyter notebook
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) )
Image in a Jupyter notebook