-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab6.py
More file actions
111 lines (91 loc) · 2.25 KB
/
Lab6.py
File metadata and controls
111 lines (91 loc) · 2.25 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 8 19:20:14 2019
Lab 6
@author: Cory Holt
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
##################################
##### TIME ARRAY DEFINITION ######
##################################
steps = 1e-5
tmin = 0
tmax = 2
t = np.arange(tmin,tmax+steps,steps)
##################################
########## PART 1 ################
def u(t):
y = np.zeros((len(t)))
for i in range(len(t)):
if t[i] < 0:
y[i] = 0
else:
y[i] = 1
return y
def toRad(deg):
return ((deg * np.pi)/180)
def y(t):
y = (np.exp(-6*t)-.5*np.exp(-4*t)+.5)*u(t)
return y
numH = [1,6,12]
demH = [1,10,24]
tout, yout = sig.step((numH,demH), T=t)
plt.figure(figsize=(12,8))
plt.subplot(2,1,1)
plt.plot(t,y(t))
plt.title('Step Response of Differential Equation')
plt.ylabel('Hand-Solved Response')
plt.grid(True)
plt.subplot(2,1,2)
plt.plot(tout,yout)
plt.ylabel('Scipy Step Response')
plt.grid(True)
plt.xlabel('t')
plt.show()
demH = [1,10,24,0]
R, P, K = sig.residue(numH,demH)
print("R,P,K for part 1:\n")
print(R)
print(P)
print(K)
##################################
########## PART 2 ################
##################################
##### TIME ARRAY DEFINITION ######
##################################
steps = 1e-5
tmin = 0
tmax = 4.5
t = np.arange(tmin,tmax+steps,steps)
##################################
dem2 = [1,18,218,2036,9085,25250,0]
num2 = [25250]
R2,P2,K2 = sig.residue(num2,dem2)
print("R,P,K for part 2:\n")
print(R2)
print(P2)
print(K2)
def cosMethod(R,P,t):
y = 0
for i in range(len(R)):
y += (abs(R[i])*np.exp(np.real(P[i])*t)*np.cos(np.imag(P[i]*t)+np.angle
(R[i]))*u(t))
return y
y2 = cosMethod(R2,P2,t)
numStep = [25250]
demStep = [1,18,218,2036,9085,25250]
tout,yout = sig.step((numStep,demStep), T = t)
plt.figure(figsize=(12,8))
plt.subplot(2,1,1)
plt.plot(t,y2)
plt.title('Step Response of Much More Complex Differential Equation')
plt.ylabel('Cosine Method')
plt.grid(True)
plt.subplot(2,1,2)
plt.plot(tout,yout)
plt.ylabel('Scipy Step Response')
plt.grid(True)
plt.xlabel('t')
plt.show()