-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alternating-Voltage.py
80 lines (66 loc) · 2.48 KB
/
Alternating-Voltage.py
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
import matplotlib.pyplot as plt
import math
class voltageGraph:
def __init__(self, frequency, peakVolatge):
self.frequency = frequency
self.peakVolatge = peakVolatge
self.angularFrequency = (2 * math.pi) * self.frequency
def startPlot(self):
timeLimitPrecision = 3
self.recommendedTimeLimit = round(10 * (1 / self.frequency), timeLimitPrecision)
while self.recommendedTimeLimit == 0 and timeLimitPrecision < 7:
self.recommendedTimeLimit = round(
10 * (1 / self.frequency), timeLimitPrecision
)
timeLimitPrecision += 1
if timeLimitPrecision >= 7:
print(
f"\n\n\n\n\n\nFrequency is too high. Restarting the Program \n\n\n\n\n\n"
)
return False
print(f"\n \033[1m Recommended Time Limit: \033[0m {self.recommendedTimeLimit}")
self.timeLimit = float(input("Enter: Time Limit (s)=> "))
print(
f"\n \033[1m Recommended Time Step \033[0m {round(self.recommendedTimeLimit * 10**(-3), timeLimitPrecision + 3)}"
)
self.timeStep = float(input("Enter: Time Step => "))
self.plot()
def voltage(self, atTime):
return self.peakVolatge * math.sin(self.angularFrequency * atTime)
def plot(self):
currentTime = 0
tillTime = self.timeLimit
timeStep = self.timeStep
print(
"\n\n\nThere might be some precision difference in your calculations and the result\n\n\n"
)
timeValues = []
voltageValues = []
while currentTime <= tillTime:
timeValues.append(currentTime)
voltageValues.append(self.voltage(currentTime))
currentTime += timeStep
plt.plot(
timeValues,
voltageValues,
label="Alternating Voltage (V)",
linestyle="-",
color="orange",
)
plt.plot(timeValues, [0] * len(timeValues), color="black", label="Time (s)")
plt.xlabel("Time (s)")
plt.ylabel("Voltage (V)")
plt.legend()
plt.show()
plt.close()
print(
"\n\n\n\n \033[1m Alternating Voltage.\033[0m\n\nUSE SI UNITS ONLY. (Metre, Newton, Second, Hertz, Volts) \n\n\n\n"
)
def initiateObject():
v1 = voltageGraph(
float(input("Enter: Frequency (Hz) => ")),
float(input("Enter: Peak Voltage (V)=> ")),
)
if v1.startPlot() == False:
return initiateObject()
initiateObject()