Load in packages first
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
%config InlineBackend.figure_format='retina'
def Compound_interest(t, P, r, n):
return P*(1+r/n)**(n*t)
Compound_interest(1,1000,0.03,12)
P = 13000
r=0.067
n=12
X_vals= np.arange(0,100)
plt.plot(X_vals,Compound_interest(X_vals,P,r,n),c='red')
plt.title("Funky Growth rate")
plt.xlabel('Time in Years')
plt.ylabel('Money in Dollars')
plt.show()
for rate in range(3,8):
r = rate/100
plt.plot(X_vals,Compound_interest(X_vals,P,r,n),label=r)
plt.title("Funky Growth rate")
plt.xlabel('Time in Years')
plt.ylabel('Money in Dollars')
plt.legend()
plt.show()
X=np.arange(0,6*np.pi)
plt.plot(X,np.sin(X))
plt.show()
X=np.arange(0,6*np.pi)
plt.plot(X,np.sin(X),'x')
plt.show()
X=np.arange(0,6*np.pi,1)
plt.plot(X,np.sin(X),c='r')
plt.show()
X=np.arange(0,6*np.pi,0.01)
plt.plot(X,np.sin(X),c='r')
plt.show()
plt.figure(figsize=(13,5))
X=np.arange(0,6*np.pi,0.01)
plt.plot(X,np.sin(X),c='r')
plt.show()
X=np.arange(0,6*np.pi,0.01)
plt.figure(figsize=(15,5))
plt.subplot(2,1,1)
plt.plot(X,np.sin(X),c='r')
plt.subplot(2,1,2)
plt.plot(X,np.cos(X),c='b')
X=np.arange(0,6*np.pi,0.01)
plt.figure(figsize=(13,6))
plt.subplot(2,2,1)
plt.plot(X,np.sin(X),c='r')
plt.subplot(2,2,2)
plt.plot(X,np.sin(X),c='b')
plt.subplot(2,2,3)
plt.plot(X,np.sin(X),c='g')
plt.subplot(2,2,4)
plt.plot(X,np.cos(X),c='y')
X=np.arange(0,6*np.pi,0.01)
plt.figure(figsize=(13,6))
plt.subplot(2,2,1)
plt.plot(X,np.sin(X),c='r',linestyle='-.')
plt.subplot(2,2,2)
plt.plot(X,np.sin(X),c='aqua',linestyle='--',alpha=0.83)
plt.subplot(2,2,3)
plt.plot(X,np.sin(X),c='g')
plt.grid( color='grey', alpha=0.3, linestyle='--', linewidth=2)
plt.subplot(2,2,4)
plt.plot(X,np.cos(X),c='b',alpha = 0.2)
plt.tick_params(axis='both', direction='out', length=6, width=2, labelcolor='b', colors='r', grid_color='gray', grid_alpha=0.5)
plt.grid()
plt.savefig('Random.png')
print(plt.style.available)
plt.style.use('tableau-colorblind10')
plt.figure(figsize=(13,5))
X=np.arange(0,6*np.pi,0.01)
plt.plot(X,np.sin(X),c='r')
plt.show()
plt.style.use('default')
Xb = [2,6,4,3,8,9]
Xb
len(Xb)
Xb.pop?
Xb.pop()
len(Xb)
Xb.pop(2)
len(Xb)
Xb
Xb.sort()
Xb
Xb.index(3)
x,y = 2,3
x/y
print(x/y)
print(round(x/y,2))
print("{:.4f}".format(x/y))
type(round(x/y,2))
type("{:.4f}".format(x/y))
np.random.random()
np.random.random?
np.random.normal()
np.random.normal?
def Move(x,y):
rand = np.random.random()
if rand<=0.25:
x = x+rand
elif rand<=0.5:
y = y + rand-0.25
elif rand <=0.75:
x = x - (rand - 0.5)
else:
y = y - (rand -0.75)
return x,y
Move(401,23)
plt.xlim(-5,5)
plt.ylim(-5,5)
x,y=0,0
count = 0
plt.plot(x,y,'o',c='b')
while x**2<=25 and y**2<=25 and count<=1000:
plt.plot(x,y,'.',c='r',alpha = 0.1)
x,y = Move(x,y)
count =count + 1
plt.show()
print(count)
for L in range(1,10):
plt.xlim(-5,5)
plt.ylim(-5,5)
x,y=0,0
count = 0
plt.plot(x,y,'o',c='b')
while x**2<=25 and y**2<=25 and count<=5000:
plt.plot(x,y,'.',c='r',alpha = 0.1)
x,y = Move(x,y)
count =count + 1
plt.show()
print(count)
randomize throwing ink on a painting, then
$$\frac{\text{green area}}{\text{green + red}} = \frac{\pi}{4} $$First circle
x=np.arange(-1,1,0.00001)
top_half = (1-x**2)**(0.5)
bottom_half = -(1-x**2)**(0.5)
plt.figure(figsize=(7,7))
plt.plot(x,top_half,c='b')
plt.plot(x,bottom_half,c='b')
Another way is to use trig!!
plt.figure(figsize=(7,7))
t=np.arange(-2*np.pi,2*np.pi,0.0001)
plt.plot(np.cos(t), np.sin(t), linewidth=1)
Inside = 0
Outside =0
plt.figure(figsize=(10,10))
t=np.arange(-2*np.pi,2*np.pi,0.0001)
plt.plot(np.cos(t), np.sin(t), linewidth=1)
%%time
Inside = 0
Outside =0
plt.figure(figsize=(7,7))
t=np.arange(-2*np.pi,2*np.pi,0.0001)
plt.plot(np.cos(t), np.sin(t), linewidth=1)
for count in range(0,5000):
x = np.random.random()*np.random.choice([-1,1])
y = np.random.random()*np.random.choice([-1,1])
if x**2 +y**2<1:
color = 'g'
Inside +=1
else:
color = 'r'
Outside +=1
plt.plot(x,y,'o',c=color, alpha =0.1)
print("{:.8f}".format(4*Inside/(Outside+Inside)))
#plt.savefig('Name_of_Graph.png')
%%time
Inside = 0
Outside =0
Num_of_Points=5000
plt.figure(figsize=(7,7))
t=np.arange(-2*np.pi,2*np.pi,0.0001)
plt.plot(np.cos(t), np.sin(t), linewidth=1)
x = np.random.random_sample(Num_of_Points)*np.random.choice([-1,1],size=Num_of_Points)
y = np.random.random_sample(Num_of_Points)*np.random.choice([-1,1],size=Num_of_Points)
for count in range(0,Num_of_Points):
if x[count]**2 +y[count]**2<1:
color = 'g'
Inside +=1
else:
color = 'r'
Outside +=1
plt.plot(x[count],y[count],'o',c=color, alpha =0.2)
print("{:.8f}".format(4*Inside/(Outside+Inside)))
import pandas as pd
%%time
Inside = 0
Outside =0
Num_of_Points=5000000
plt.figure(figsize=(7,7))
t=np.arange(-2*np.pi,2*np.pi,0.0001)
plt.plot(np.cos(t), np.sin(t), linewidth=1)
x = np.random.random_sample(Num_of_Points)*np.random.choice([-1,1],size=Num_of_Points)
y = np.random.random_sample(Num_of_Points)*np.random.choice([-1,1],size=Num_of_Points)
z=x**2+y**2-1
df = pd.DataFrame({'x':x,'y':y,'z':z})
df_in=df[df.z<0]
df_out=df[df.z>=0]
plt.plot(df_in.x,df_in.y,'o',c='g',alpha=0.01)
plt.plot(df_out.x,df_out.y,'o',c='r',alpha=0.01)
print("{:.8f}".format(4*len(df_in)/(len(df_out)+len(df_in))))