Welcome to Week1 of MAT217:

Modeling and Simulation

Argh, help me. Computers are the enemy, save meeeeeeeee

In [1]:
print('hello world','and something',3+5)
hello world and something 8
Variables and operators
In [3]:
x = 2
y = 13

x+y
Out[3]:
15
In [12]:
x, y = 8,2
x*y
Out[12]:
16
In [13]:
print(x/y)
print(x*y)
print(x**y, y**x)
print( x//y, y//x)
print( x ==y, x==y+x-y)
4.0
16
64 256
4 0
False True
Lists and things
In [14]:
List1 = [2,3,4,5,6,7]
In [16]:
List1[3]
Out[16]:
5
In [17]:
print(List1[0:200])
print(List1[2:8])
print(List1[2:])
print(List1[1:8:2])
print(List1[0::3])
[2, 3, 4, 5, 6, 7]
[4, 5, 6, 7]
[4, 5, 6, 7]
[3, 5, 7]
[2, 5]
In [18]:
List2 = [2,3,4,5,6,7]
In [21]:
for count in range(0,len(List2)):
    print(List2[count])
    print(count)
2
0
3
1
4
2
5
3
6
4
7
5
In [25]:
bob='the once was a cat print from.....'
In [26]:
for ss in range(0,len(bob)):
    print(bob[ss])
t
h
e
 
o
n
c
e
 
w
a
s
 
a
 
c
a
t
 
p
r
i
n
t
 
f
r
o
m
.
.
.
.
.
In [27]:
plot(List1,List2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-756d3d6a806f> in <module>
----> 1 plot(List1,List2)

NameError: name 'plot' is not defined
In [ ]:
 

Plottting and Packages

In [28]:
import matplotlib.pyplot as plt
In [29]:
plt.plot(List1,List2)
Out[29]:
[<matplotlib.lines.Line2D at 0x7fa858039650>]
In [30]:
plt.scatter(List1, List2)
Out[30]:
<matplotlib.collections.PathCollection at 0x7fa898d5a4d0>
In [33]:
plt.delaxes?
Signature: plt.delaxes(ax=None)
Docstring:
Remove the `Axes` *ax* (defaulting to the current axes) from its figure.

A KeyError is raised if the axes doesn't exist.
File:      /opt/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py
Type:      function
In [34]:
List2= [3,4,4,5,6,6]
In [35]:
plt.plot(List1,List2)
Out[35]:
[<matplotlib.lines.Line2D at 0x7fa8bc22b2d0>]
In [41]:
plt.plot(List1,List2,".-") #x-,o-
Out[41]:
[<matplotlib.lines.Line2D at 0x7fa8a9fdf790>]

Lets Check our Compound interest

Something something $ A(t)=P(1+\frac{r}{n})^{nt} $

$$ A(t)=P(1+\frac{r}{n})^{nt} $$

The tale of two approaches, first lets set up the x and then the y points and plot them

In [43]:
P = 13000
r=0.067
n=12
In [44]:
X_vals = []
Y_vals=[]
for count in range(0,100):
    X_vals.append(count)
    Y_point = P*(1+r/n)**(n*count)
    Y_vals.append(Y_point)
In [ ]:
 
In [47]:
plt.plot(X_vals,Y_vals)
Out[47]:
[<matplotlib.lines.Line2D at 0x7fa8aa091810>]
In [48]:
plt.plot(X_vals,Y_vals,c='red')
plt.title("Funky Growth rate")
Out[48]:
Text(0.5, 1.0, 'Funky Growth rate')
In [49]:
plt.plot(X_vals,Y_vals,c='red')
plt.title("Funky Growth rate")
plt.show()
In [50]:
%config InlineBackend.figure_format='retina'
In [51]:
plt.plot(X_vals,Y_vals,c='red')
plt.title("Funky Growth rate")
plt.show()
In [52]:
plt.plot(X_vals,Y_vals,c='red')
plt.title("Funky Growth rate")
plt.xlabel('Time in Years')
plt.ylabel('Money in Dollars')
plt.show()

Choice number two, NUMPY WAY!!!

In [53]:
import numpy as np
In [54]:
X_vals= np.arange(0,100)
In [55]:
X_vals
Out[55]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [56]:
Y_vals = P*(1+r/n)**(n*X_vals)
In [57]:
plt.plot(X_vals,Y_vals,c='red')
plt.title("Funky Growth rate")
plt.xlabel('Time in Years')
plt.ylabel('Money in Dollars')
plt.show()

Lets make a definition

In [58]:
def Compound_interest(t, P, r, n):
    return P*(1+r/n)**(n*t)
In [59]:
Compound_interest(1,1000,0.03,12)
Out[59]:
1030.4159569135068