In [2]:
import matplotlib.pyplot as plt 
import numpy as np
import math 
In [3]:
t = np.linspace(0,20,2000) #betwixt 0,4, 2k points
def a(m,k,g): # to test +ve / 0s
    return (g**2 - 4*m*k)
In [4]:
def x(m,k,g,t):
    return math.exp(((-g-math.sqrt(abs(g**2-4*m*k)))/2*m)*t) #taking the -ve sqrt,
xv = np.vectorize(x) # ? should learn about python 

+ve Case

this is the overdamped case,

In [5]:
print('a is', a(0.1,0.1,1)) # +ve
O = xv(0.1,0.1,1,t)
print(O)
plt.plot(t, O)
a is 0.96
[1.         0.9990101  0.99802117 ... 0.13837123 0.13823426 0.13809742]
Out[5]:
[<matplotlib.lines.Line2D at 0x1fd3530fbc8>]

-ve case

I've missed something here:

since a is -ve, we have our i back - ... meaning some oscillation, should figure how to plot this

In [6]:
print('a is', a(1,1,0)) #-ve
N = xv(1,1,0,t)
print(N)
plt.plot(t, N)
a is -4
[1.00000000e+00 9.90044881e-01 9.80188866e-01 ... 2.10281273e-09
 2.08187898e-09 2.06115362e-09]
Out[6]:
[<matplotlib.lines.Line2D at 0x1fd396ce2c8>]

0 case

this represents critical damping

In [7]:
print('a is', a(1,1,2)) #0
Z = xv(1,1,2,t)
print(Z)
plt.plot(t, Z)
a is 0
[1.00000000e+00 9.90044881e-01 9.80188866e-01 ... 2.10281273e-09
 2.08187898e-09 2.06115362e-09]
Out[7]:
[<matplotlib.lines.Line2D at 0x1fd3974b208>]
In [8]:
plt.plot(t,N,Z)
Out[8]:
[<matplotlib.lines.Line2D at 0x1fd397c7c08>,
 <matplotlib.lines.Line2D at 0x1fd397bb908>]

c)

particular

In [9]:
freq = np.linspace(0,2,2000)
A = 1 / (-freq*freq+freq*0.1j+1) # imaginary numbs in py: num_j 0.1j here, fancy!
plt.plot(freq,np.absolute(A)) # np can pull real (absolute?) or phase (angle) from imaginary
Out[9]:
[<matplotlib.lines.Line2D at 0x1fd3983a648>]
In [8]:
plt.plot(freq,np.angle(A))
Out[8]:
[<matplotlib.lines.Line2D at 0x1b6ac4a89c8>]
In [9]:
A = [[2,-1],
     [-1,2]] #numpy matricies: row-order nd arrays
print(A)
[[2, -1], [-1, 2]]
In [10]:
D = np.linalg.eig(A)
print(D)
print(math.sqrt(1/2)) #looks like,
(array([3., 1.]), array([[ 0.70710678,  0.70710678],
       [-0.70710678,  0.70710678]]))
0.7071067811865476

3.3

to start, I'm going to hit this and plot it,

In [11]:
def filter(alpha, sig, result):
    for i in range(sig.size-1) :
        result[i] = alpha*result[i - 1] + (1-alpha)*sig[i] #easier to not-step underneath arr,
In [12]:
series = 100
S = np.zeros(series)
S[10] = 1
S[16] = 1
R = np.zeros(series)
filter(0.7, S, R)
plt.plot(S)
plt.plot(R)
Out[12]:
[<matplotlib.lines.Line2D at 0x1b6abdb2348>]

ode4

In [ ]: